diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..af96048 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,19 @@ +# Auto detect text files and perform LF normalization +* text=auto + +*.cs text diff=csharp +*.java text diff=java +*.html text diff=html +*.py text diff=python +*.pl text diff=perl +*.pm text diff=perl +*.css text +*.js text +*.sql text + +*.sh text eol=lf + +*.bat text eol=crlf +*.cmd text eol=crlf +*.csproj text merge=union eol=crlf +*.sln text merge=union eol=crlf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f89c90a --- /dev/null +++ b/.gitignore @@ -0,0 +1,81 @@ +# Java +hs_err_pid* + +# Git +*.orij +*.rej + +# SVN +.svn +.revision + +# Eclipse +.metadata +.classpath +.eclipse/ +.idea/ +.project +.revision/ +.settings/ +.externalToolBuilders/ +local.properties +.recommenders +*.launch +build/ +target/ +out/ + +# Intellij +*.ipr +*.iws +*.iml + +# NetBeans +*~.nib +nbbuild/ +dist/ +nbdist/ +.ndb-gradle/ +.cproject +.buildpath + +# Other +*~ +logs/ +*.out +*.log +*.bak +*.tmp +tmp/** +tmp/**/* +.DS_Store +.gradle +*.patch +*.swp + +# C +tags +.cproject +.project +obj +src/c/core.* +src/c/TEST-*.txt +src/c/*.la +src/c/*.lo +src/c/*.o +src/c/generated/ +src/java/generated/ +src/java/lib/ant-eclipse-* +src/java/lib/ivy-* +src/c/Makefile.in +src/c/aclocal.m4 +src/c/autom4te.cache/ +src/c/compile +src/c/config.guess +src/c/config.h.in +src/c/config.sub +src/c/configure +src/c/depcomp +src/c/install-sh +src/c/ltmain.sh +src/c/missing 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..1ae3351 --- /dev/null +++ b/NOTICE.txt @@ -0,0 +1,5 @@ +Apache ZooKeeper +Copyright 2009-2018 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/README_packaging.txt b/README_packaging.txt new file mode 100644 index 0000000..48b6918 --- /dev/null +++ b/README_packaging.txt @@ -0,0 +1,65 @@ +README file for Packaging Notes + +Requirement +----------- + +gcc, cppunit and python-setuptools are required to build +C and python bindings. + +On RHEL machine: + +yum install cppunit +yum install python-setuptools + +On Ubuntu: + +apt-get --install cppunit +apt-get --install python-setuptools + +Package build command +--------------------- + +Command to build Debian package: ant deb +Command to build RPM Package: ant rpm + +rpm and deb packages are generated and placed in: + +build/zookeeper*.[rpm|deb] +build/contrib/**.[rpm|deb] + +Default package file structure layout + + /usr/bin - User executable + /usr/sbin - System executable + /usr/libexec - Configuration boot trap script + /usr/lib - Native libraries + /usr/share/doc/zookeeper - Documents + /usr/share/zookeeper - Project files + /usr/share/zookeeper/template/conf - Configuration template files + /etc/zookeeper - Configuration files + /etc/init.d/zookeeper - OS startup script + +Source file structure layout +--------------------- + +src/packages/update-zookeeper-env.sh + - setup environment variables and symlink $PREFIX/etc/zookeeper to + /etc/zookeeper. + - This script is designed to run in post installation, and pre-remove + phase of ZooKeeper package. + - Run update-zookeeper-env.sh -h to get a list of supported parameters. + +src/packages/template + - Standard configuration template + +src/packages/deb + Meta data for creating Debian package + +src/packages/deb/init.d + Daemon start/stop script for Debian flavor of Linux + +src/packages/rpm + Meta data for creating RPM package + +src/packages/rpm/init.d + Daemon start/stop script for Redhat flavor of Linux 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..38ee2e8 --- /dev/null +++ b/bin/zkCleanup.sh @@ -0,0 +1,51 @@ +#!/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 POSTIX 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/.*=//')" + +if [ "x$ZOODATALOGDIR" = "x" ] +then +"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS \ + org.apache.zookeeper.server.PurgeTxnLog "$ZOODATADIR" $* +else +"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -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..0ffa030 --- /dev/null +++ b/bin/zkCli.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.ZooKeeperMain +call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% %* + +endlocal + diff --git a/bin/zkCli.sh b/bin/zkCli.sh new file mode 100644 index 0000000..992a913 --- /dev/null +++ b/bin/zkCli.sh @@ -0,0 +1,41 @@ +#!/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 POSTIX 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" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -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..fbc017f --- /dev/null +++ b/bin/zkEnv.cmd @@ -0,0 +1,52 @@ +@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%.. +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. + 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..43cbc22 --- /dev/null +++ b/bin/zkEnv.sh @@ -0,0 +1,115 @@ +#!/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 + +ZOOBINDIR="${ZOOBINDIR:-/usr/bin}" +ZOOKEEPER_PREFIX="${ZOOBINDIR}/.." + +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="." +fi + +if [ "x${ZOO_LOG4J_PROP}" = "x" ] +then + ZOO_LOG4J_PROP="INFO,CONSOLE" +fi + +if [ "$JAVA_HOME" != "" ]; then + JAVA="$JAVA_HOME/bin/java" +else + JAVA=java +fi + +#add the zoocfg dir to classpath +CLASSPATH="$ZOOCFGDIR:$CLASSPATH" + +for i in "$ZOOBINDIR"/../src/java/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 [ -e "${ZOOKEEPER_PREFIX}"/share/zookeeper/zookeeper-*.jar ]; 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 + +#make it work for developers +CLASSPATH="$ZOOBINDIR/../build/classes:$CLASSPATH" + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +#echo "CLASSPATH=$CLASSPATH" \ No newline at end of file diff --git a/bin/zkServer.cmd b/bin/zkServer.cmd new file mode 100644 index 0000000..6b4cf02 --- /dev/null +++ b/bin/zkServer.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.quorum.QuorumPeerMain +echo on +call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %* + +endlocal diff --git a/bin/zkServer.sh b/bin/zkServer.sh new file mode 100644 index 0000000..51dbe9f --- /dev/null +++ b/bin/zkServer.sh @@ -0,0 +1,225 @@ +#!/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 POSTIX 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 + 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 +if [ -z "$ZOOPIDFILE" ]; then + ZOO_DATADIR="$($GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')" + 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_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out" + +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 0 + fi + fi + nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -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 + echo STARTED + 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[@]}" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" + ;; +print-cmd) + echo "\"$JAVA\" -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -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 -9 $(cat "$ZOOPIDFILE") + rm "$ZOOPIDFILE" + echo STOPPED + fi + exit 0 + ;; +upgrade) + shift + echo "upgrading the servers to 3.*" + "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@} + echo "Upgrading ... " + ;; +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 + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \ + $clientPortAddress $clientPort srvr 2> /dev/null \ + | $GREP Mode` + if [ "x$STAT" = "x" ] + then + echo "Error contacting service. It is probably not running." + exit 1 + else + echo $STAT + exit 0 + fi + ;; +*) + echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2 + +esac diff --git a/bin/zkTxnLogToolkit.cmd b/bin/zkTxnLogToolkit.cmd new file mode 100644 index 0000000..362dc44 --- /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/build.xml b/build.xml new file mode 100644 index 0000000..8017e2e --- /dev/null +++ b/build.xml @@ -0,0 +1,1993 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ') +} + + +function SwitchMenu(obj, thePath) +{ +var open = 'url("'+thePath + 'images/chapter_open.gif")'; +var close = 'url("'+thePath + 'images/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/docs/skin/images/README.txt b/docs/skin/images/README.txt new file mode 100644 index 0000000..e0932f4 --- /dev/null +++ b/docs/skin/images/README.txt @@ -0,0 +1 @@ +The images in this directory are used if the current skin lacks them. diff --git a/docs/skin/images/add.jpg b/docs/skin/images/add.jpg new file mode 100644 index 0000000..06831ee Binary files /dev/null and b/docs/skin/images/add.jpg differ diff --git a/docs/skin/images/apache-thanks.png b/docs/skin/images/apache-thanks.png new file mode 100644 index 0000000..c0bea09 Binary files /dev/null and b/docs/skin/images/apache-thanks.png differ diff --git a/docs/skin/images/built-with-cocoon.gif b/docs/skin/images/built-with-cocoon.gif new file mode 100644 index 0000000..0b38f78 Binary files /dev/null and b/docs/skin/images/built-with-cocoon.gif differ diff --git a/docs/skin/images/built-with-forrest-button.png b/docs/skin/images/built-with-forrest-button.png new file mode 100644 index 0000000..4a787ab Binary files /dev/null and b/docs/skin/images/built-with-forrest-button.png differ diff --git a/docs/skin/images/chapter.gif b/docs/skin/images/chapter.gif new file mode 100644 index 0000000..d3d8245 Binary files /dev/null and b/docs/skin/images/chapter.gif differ diff --git a/docs/skin/images/chapter_open.gif b/docs/skin/images/chapter_open.gif new file mode 100644 index 0000000..eecce18 Binary files /dev/null and b/docs/skin/images/chapter_open.gif differ diff --git a/docs/skin/images/current.gif b/docs/skin/images/current.gif new file mode 100644 index 0000000..fd82c08 Binary files /dev/null and b/docs/skin/images/current.gif differ diff --git a/docs/skin/images/error.png b/docs/skin/images/error.png new file mode 100644 index 0000000..b4fe06e Binary files /dev/null and b/docs/skin/images/error.png differ diff --git a/docs/skin/images/external-link.gif b/docs/skin/images/external-link.gif new file mode 100644 index 0000000..ff2f7b2 Binary files /dev/null and b/docs/skin/images/external-link.gif differ diff --git a/docs/skin/images/fix.jpg b/docs/skin/images/fix.jpg new file mode 100644 index 0000000..1d6820b Binary files /dev/null and b/docs/skin/images/fix.jpg differ diff --git a/docs/skin/images/forrest-credit-logo.png b/docs/skin/images/forrest-credit-logo.png new file mode 100644 index 0000000..8a63e42 Binary files /dev/null and b/docs/skin/images/forrest-credit-logo.png differ diff --git a/docs/skin/images/hack.jpg b/docs/skin/images/hack.jpg new file mode 100644 index 0000000..f38d50f Binary files /dev/null and b/docs/skin/images/hack.jpg differ diff --git a/docs/skin/images/header_white_line.gif b/docs/skin/images/header_white_line.gif new file mode 100644 index 0000000..369cae8 Binary files /dev/null and b/docs/skin/images/header_white_line.gif differ diff --git a/docs/skin/images/info.png b/docs/skin/images/info.png new file mode 100644 index 0000000..2e53447 Binary files /dev/null and b/docs/skin/images/info.png differ diff --git a/docs/skin/images/instruction_arrow.png b/docs/skin/images/instruction_arrow.png new file mode 100644 index 0000000..0fbc724 Binary files /dev/null and b/docs/skin/images/instruction_arrow.png differ diff --git a/docs/skin/images/label.gif b/docs/skin/images/label.gif new file mode 100644 index 0000000..c83a389 Binary files /dev/null and b/docs/skin/images/label.gif differ diff --git a/docs/skin/images/page.gif b/docs/skin/images/page.gif new file mode 100644 index 0000000..a144d32 Binary files /dev/null and b/docs/skin/images/page.gif differ diff --git a/docs/skin/images/pdfdoc.gif b/docs/skin/images/pdfdoc.gif new file mode 100644 index 0000000..ec13eb5 Binary files /dev/null and b/docs/skin/images/pdfdoc.gif differ diff --git a/docs/skin/images/poddoc.png b/docs/skin/images/poddoc.png new file mode 100644 index 0000000..a393df7 Binary files /dev/null and b/docs/skin/images/poddoc.png differ diff --git a/docs/skin/images/printer.gif b/docs/skin/images/printer.gif new file mode 100644 index 0000000..a8d0d41 Binary files /dev/null and b/docs/skin/images/printer.gif differ diff --git a/docs/skin/images/rc-b-l-15-1body-2menu-3menu.png b/docs/skin/images/rc-b-l-15-1body-2menu-3menu.png new file mode 100644 index 0000000..cdb460a Binary files /dev/null and b/docs/skin/images/rc-b-l-15-1body-2menu-3menu.png differ diff --git a/docs/skin/images/rc-b-r-15-1body-2menu-3menu.png b/docs/skin/images/rc-b-r-15-1body-2menu-3menu.png new file mode 100644 index 0000000..3eff254 Binary files /dev/null and b/docs/skin/images/rc-b-r-15-1body-2menu-3menu.png differ diff --git a/docs/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png b/docs/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png new file mode 100644 index 0000000..b175f27 Binary files /dev/null and b/docs/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png differ diff --git a/docs/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png b/docs/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png new file mode 100644 index 0000000..e9f4440 Binary files /dev/null and b/docs/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png differ diff --git a/docs/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png b/docs/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png new file mode 100644 index 0000000..f1e015b Binary files /dev/null and b/docs/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png differ diff --git a/docs/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png b/docs/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png new file mode 100644 index 0000000..e9f4440 Binary files /dev/null and b/docs/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png differ diff --git a/docs/skin/images/rc-t-r-15-1body-2menu-3menu.png b/docs/skin/images/rc-t-r-15-1body-2menu-3menu.png new file mode 100644 index 0000000..29388b5 Binary files /dev/null and b/docs/skin/images/rc-t-r-15-1body-2menu-3menu.png differ diff --git a/docs/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png b/docs/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png new file mode 100644 index 0000000..944ed73 Binary files /dev/null and b/docs/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png differ diff --git a/docs/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png b/docs/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png new file mode 100644 index 0000000..c4d4a8c Binary files /dev/null and b/docs/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png differ diff --git a/docs/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png b/docs/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png new file mode 100644 index 0000000..944ed73 Binary files /dev/null and b/docs/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png differ diff --git a/docs/skin/images/remove.jpg b/docs/skin/images/remove.jpg new file mode 100644 index 0000000..8c9b9ef Binary files /dev/null and b/docs/skin/images/remove.jpg differ diff --git a/docs/skin/images/rss.png b/docs/skin/images/rss.png new file mode 100644 index 0000000..f0796ac Binary files /dev/null and b/docs/skin/images/rss.png differ diff --git a/docs/skin/images/spacer.gif b/docs/skin/images/spacer.gif new file mode 100644 index 0000000..35d42e8 Binary files /dev/null and b/docs/skin/images/spacer.gif differ diff --git a/docs/skin/images/success.png b/docs/skin/images/success.png new file mode 100644 index 0000000..96fcfea Binary files /dev/null and b/docs/skin/images/success.png differ diff --git a/docs/skin/images/txtdoc.png b/docs/skin/images/txtdoc.png new file mode 100644 index 0000000..bf8b374 Binary files /dev/null and b/docs/skin/images/txtdoc.png differ diff --git a/docs/skin/images/update.jpg b/docs/skin/images/update.jpg new file mode 100644 index 0000000..beb9207 Binary files /dev/null and b/docs/skin/images/update.jpg differ diff --git a/docs/skin/images/valid-html401.png b/docs/skin/images/valid-html401.png new file mode 100644 index 0000000..3855210 Binary files /dev/null and b/docs/skin/images/valid-html401.png differ diff --git a/docs/skin/images/vcss.png b/docs/skin/images/vcss.png new file mode 100644 index 0000000..9b2f596 Binary files /dev/null and b/docs/skin/images/vcss.png differ diff --git a/docs/skin/images/warning.png b/docs/skin/images/warning.png new file mode 100644 index 0000000..b81b2ce Binary files /dev/null and b/docs/skin/images/warning.png differ diff --git a/docs/skin/images/xmldoc.gif b/docs/skin/images/xmldoc.gif new file mode 100644 index 0000000..c92d9b9 Binary files /dev/null and b/docs/skin/images/xmldoc.gif differ diff --git a/docs/skin/menu.js b/docs/skin/menu.js new file mode 100644 index 0000000..06ea471 --- /dev/null +++ b/docs/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/docs/skin/note.txt b/docs/skin/note.txt new file mode 100644 index 0000000..d34c8db --- /dev/null +++ b/docs/skin/note.txt @@ -0,0 +1,50 @@ +Notes for developer: + +--Legend------------------- +TODO -> blocker +DONE -> blocker +ToDo -> enhancement bug +done -> enhancement bug + +--Issues------------------- +- the corner images should be rendered through svg with the header color. +-> DONE +-> ToDo: get rid of the images and use only divs! + +- the menu points should be displayed "better". +-> DONE +-- Use the krysalis-site menu approach for the overall menu display. +-> DONE +-- Use the old lenya innermenu approch to further enhance the menu . +-> DONE + +- the content area needs some attention. +-> DONE +-- introduce the heading scheme from krysalis () +-> DONE +-> ToDo: make box with round corners +-> done: make underlined with variable border height +-> ToDo: make underline with bottom round corner +-- introduce the toc for each html-page +-> DONE +-- introduce the external-link-images. +-> DONE + +- the publish note should be where now only a border is. +Like
+-> DONE +, but make it configurable. +-> DONE +- footer needs some attention +-> DONE +-- the footer do not have the color profile! Enable it! +-> DONE +-- the footer should as well contain a feedback link. +See http://issues.apache.org/eyebrowse/ReadMsg?listName=forrest-user@xml.apache.org&msgNo=71 +-> DONE + +- introduce credits alternativ location +-> DONE + +- border for published / breadtrail / menu /tab divs +-> ToDo \ No newline at end of file diff --git a/docs/skin/print.css b/docs/skin/print.css new file mode 100644 index 0000000..aaa9931 --- /dev/null +++ b/docs/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/docs/skin/profile.css b/docs/skin/profile.css new file mode 100644 index 0000000..eefaa88 --- /dev/null +++ b/docs/skin/profile.css @@ -0,0 +1,168 @@ + + +/* ==================== 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 } +} + +a.external { + padding: 0 20px 0px 0px; + display:inline; + background-repeat: no-repeat; + background-position: center right; + background-image: url(images/external-link.gif); +} + +#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 .menupagetitle { background-color: #CFDCED; + color: #000000;} + +#menu { border-color: #999999;} +#menu .menupagetitle { 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;} + +.code { border-color: #a5b6c6;} + +#footer { background-color: #E5E4D9;} +/* extra-css */ + + p.quote { + margin-left: 2em; + padding: .5em; + background-color: #f0f0f0; + font-family: monospace; + } + + pre.code { + margin-left: 0em; + padding: 0.5em; + background-color: #f0f0f0; + font-family: monospace; + } + + + + \ No newline at end of file diff --git a/docs/skin/prototype.js b/docs/skin/prototype.js new file mode 100644 index 0000000..ed7d920 --- /dev/null +++ b/docs/skin/prototype.js @@ -0,0 +1,1257 @@ +/* Prototype JavaScript framework, version 1.4.0_pre4 + * (c) 2005 Sam Stephenson + * + * THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff + * against the source tree, available from the Prototype darcs repository. + * + * Prototype is freely distributable under the terms of an MIT-style license. + * + * For details, see the Prototype web site: http://prototype.conio.net/ + * +/*--------------------------------------------------------------------------*/ + +var Prototype = { + Version: '1.4.0_pre4', + + emptyFunction: function() {}, + K: function(x) {return x} +} + +var Class = { + create: function() { + return function() { + this.initialize.apply(this, arguments); + } + } +} + +var Abstract = new Object(); + +Object.extend = function(destination, source) { + for (property in source) { + destination[property] = source[property]; + } + return destination; +} + +Function.prototype.bind = function(object) { + var __method = this; + return function() { + return __method.apply(object, arguments); + } +} + +Function.prototype.bindAsEventListener = function(object) { + var __method = this; + return function(event) { + return __method.call(object, event || window.event); + } +} + +Number.prototype.toColorPart = function() { + var digits = this.toString(16); + if (this < 16) return '0' + digits; + return digits; +} + +var Try = { + these: function() { + var returnValue; + + for (var i = 0; i < arguments.length; i++) { + var lambda = arguments[i]; + try { + returnValue = lambda(); + break; + } catch (e) {} + } + + return returnValue; + } +} + +/*--------------------------------------------------------------------------*/ + +var PeriodicalExecuter = Class.create(); +PeriodicalExecuter.prototype = { + initialize: function(callback, frequency) { + this.callback = callback; + this.frequency = frequency; + this.currentlyExecuting = false; + + this.registerCallback(); + }, + + registerCallback: function() { + setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + onTimerEvent: function() { + if (!this.currentlyExecuting) { + try { + this.currentlyExecuting = true; + this.callback(); + } finally { + this.currentlyExecuting = false; + } + } + } +} + +/*--------------------------------------------------------------------------*/ + +function $() { + var elements = new Array(); + + for (var i = 0; i < arguments.length; i++) { + var element = arguments[i]; + if (typeof element == 'string') + element = document.getElementById(element); + + if (arguments.length == 1) + return element; + + elements.push(element); + } + + return elements; +} + +if (!Array.prototype.push) { + Array.prototype.push = function() { + var startLength = this.length; + for (var i = 0; i < arguments.length; i++) + this[startLength + i] = arguments[i]; + return this.length; + } +} + +if (!Function.prototype.apply) { + // Based on code from http://www.youngpup.net/ + Function.prototype.apply = function(object, parameters) { + var parameterStrings = new Array(); + if (!object) object = window; + if (!parameters) parameters = new Array(); + + for (var i = 0; i < parameters.length; i++) + parameterStrings[i] = 'parameters[' + i + ']'; + + object.__apply__ = this; + var result = eval('object.__apply__(' + + parameterStrings.join(', ') + ')'); + object.__apply__ = null; + + return result; + } +} + +Object.extend(String.prototype, { + stripTags: function() { + return this.replace(/<\/?[^>]+>/gi, ''); + }, + + escapeHTML: function() { + var div = document.createElement('div'); + var text = document.createTextNode(this); + div.appendChild(text); + return div.innerHTML; + }, + + unescapeHTML: function() { + var div = document.createElement('div'); + div.innerHTML = this.stripTags(); + return div.childNodes[0].nodeValue; + }, + + parseQuery: function() { + var str = this; + if (str.substring(0,1) == '?') { + str = this.substring(1); + } + var result = {}; + var pairs = str.split('&'); + for (var i = 0; i < pairs.length; i++) { + var pair = pairs[i].split('='); + result[pair[0]] = pair[1]; + } + return result; + } +}); + + +var _break = new Object(); +var _continue = new Object(); + +var Enumerable = { + each: function(iterator) { + var index = 0; + try { + this._each(function(value) { + try { + iterator(value, index++); + } catch (e) { + if (e != _continue) throw e; + } + }); + } catch (e) { + if (e != _break) throw e; + } + }, + + all: function(iterator) { + var result = true; + this.each(function(value, index) { + if (!(result &= (iterator || Prototype.K)(value, index))) + throw _break; + }); + return result; + }, + + any: function(iterator) { + var result = true; + this.each(function(value, index) { + if (result &= (iterator || Prototype.K)(value, index)) + throw _break; + }); + return result; + }, + + collect: function(iterator) { + var results = []; + this.each(function(value, index) { + results.push(iterator(value, index)); + }); + return results; + }, + + detect: function (iterator) { + var result; + this.each(function(value, index) { + if (iterator(value, index)) { + result = value; + throw _break; + } + }); + return result; + }, + + findAll: function(iterator) { + var results = []; + this.each(function(value, index) { + if (iterator(value, index)) + results.push(value); + }); + return results; + }, + + grep: function(pattern, iterator) { + var results = []; + this.each(function(value, index) { + var stringValue = value.toString(); + if (stringValue.match(pattern)) + results.push((iterator || Prototype.K)(value, index)); + }) + return results; + }, + + include: function(object) { + var found = false; + this.each(function(value) { + if (value == object) { + found = true; + throw _break; + } + }); + return found; + }, + + inject: function(memo, iterator) { + this.each(function(value, index) { + memo = iterator(memo, value, index); + }); + return memo; + }, + + invoke: function(method) { + var args = $A(arguments).slice(1); + return this.collect(function(value) { + return value[method].apply(value, args); + }); + }, + + max: function(iterator) { + var result; + this.each(function(value, index) { + value = (iterator || Prototype.K)(value, index); + if (value >= (result || value)) + result = value; + }); + return result; + }, + + min: function(iterator) { + var result; + this.each(function(value, index) { + value = (iterator || Prototype.K)(value, index); + if (value <= (result || value)) + result = value; + }); + return result; + }, + + partition: function(iterator) { + var trues = [], falses = []; + this.each(function(value, index) { + ((iterator || Prototype.K)(value, index) ? + trues : falses).push(value); + }); + return [trues, falses]; + }, + + pluck: function(property) { + var results = []; + this.each(function(value, index) { + results.push(value[property]); + }); + return results; + }, + + reject: function(iterator) { + var results = []; + this.each(function(value, index) { + if (!iterator(value, index)) + results.push(value); + }); + return results; + }, + + sortBy: function(iterator) { + return this.collect(function(value, index) { + return {value: value, criteria: iterator(value, index)}; + }).sort(function(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }).pluck('value'); + }, + + toArray: function() { + return this.collect(Prototype.K); + }, + + zip: function() { + var iterator = Prototype.K, args = $A(arguments); + if (typeof args.last() == 'function') + iterator = args.pop(); + + var collections = [this].concat(args).map($A); + return this.map(function(value, index) { + iterator(value = collections.pluck(index)); + return value; + }); + } +} + +Object.extend(Enumerable, { + map: Enumerable.collect, + find: Enumerable.detect, + select: Enumerable.findAll, + member: Enumerable.include, + entries: Enumerable.toArray +}); + +$A = Array.from = function(iterable) { + var results = []; + for (var i = 0; i < iterable.length; i++) + results.push(iterable[i]); + return results; +} + +Object.extend(Array.prototype, { + _each: function(iterator) { + for (var i = 0; i < this.length; i++) + iterator(this[i]); + }, + + first: function() { + return this[0]; + }, + + last: function() { + return this[this.length - 1]; + } +}); + +Object.extend(Array.prototype, Enumerable); + + +var Ajax = { + getTransport: function() { + return Try.these( + function() {return new ActiveXObject('Msxml2.XMLHTTP')}, + function() {return new ActiveXObject('Microsoft.XMLHTTP')}, + function() {return new XMLHttpRequest()} + ) || false; + } +} + +Ajax.Base = function() {}; +Ajax.Base.prototype = { + setOptions: function(options) { + this.options = { + method: 'post', + asynchronous: true, + parameters: '' + } + Object.extend(this.options, options || {}); + }, + + responseIsSuccess: function() { + return this.transport.status == undefined + || this.transport.status == 0 + || (this.transport.status >= 200 && this.transport.status < 300); + }, + + responseIsFailure: function() { + return !this.responseIsSuccess(); + } +} + +Ajax.Request = Class.create(); +Ajax.Request.Events = + ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; + +Ajax.Request.prototype = Object.extend(new Ajax.Base(), { + initialize: function(url, options) { + this.transport = Ajax.getTransport(); + this.setOptions(options); + this.request(url); + }, + + request: function(url) { + var parameters = this.options.parameters || ''; + if (parameters.length > 0) parameters += '&_='; + + try { + if (this.options.method == 'get') + url += '?' + parameters; + + this.transport.open(this.options.method, url, + this.options.asynchronous); + + if (this.options.asynchronous) { + this.transport.onreadystatechange = this.onStateChange.bind(this); + setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10); + } + + this.setRequestHeaders(); + + var body = this.options.postBody ? this.options.postBody : parameters; + this.transport.send(this.options.method == 'post' ? body : null); + + } catch (e) { + } + }, + + setRequestHeaders: function() { + var requestHeaders = + ['X-Requested-With', 'XMLHttpRequest', + 'X-Prototype-Version', Prototype.Version]; + + if (this.options.method == 'post') { + requestHeaders.push('Content-type', + 'application/x-www-form-urlencoded'); + + /* Force "Connection: close" for Mozilla browsers to work around + * a bug where XMLHttpReqeuest sends an incorrect Content-length + * header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType) + requestHeaders.push('Connection', 'close'); + } + + if (this.options.requestHeaders) + requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); + + for (var i = 0; i < requestHeaders.length; i += 2) + this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); + }, + + onStateChange: function() { + var readyState = this.transport.readyState; + if (readyState != 1) + this.respondToReadyState(this.transport.readyState); + }, + + respondToReadyState: function(readyState) { + var event = Ajax.Request.Events[readyState]; + + if (event == 'Complete') + (this.options['on' + this.transport.status] + || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] + || Prototype.emptyFunction)(this.transport); + + (this.options['on' + event] || Prototype.emptyFunction)(this.transport); + + /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ + if (event == 'Complete') + this.transport.onreadystatechange = Prototype.emptyFunction; + } +}); + +Ajax.Updater = Class.create(); +Ajax.Updater.ScriptFragment = '(?:)((\n|.)*?)(?:<\/script>)'; + +Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { + initialize: function(container, url, options) { + this.containers = { + success: container.success ? $(container.success) : $(container), + failure: container.failure ? $(container.failure) : + (container.success ? null : $(container)) + } + + this.transport = Ajax.getTransport(); + this.setOptions(options); + + var onComplete = this.options.onComplete || Prototype.emptyFunction; + this.options.onComplete = (function() { + this.updateContent(); + onComplete(this.transport); + }).bind(this); + + this.request(url); + }, + + updateContent: function() { + var receiver = this.responseIsSuccess() ? + this.containers.success : this.containers.failure; + + var match = new RegExp(Ajax.Updater.ScriptFragment, 'img'); + var response = this.transport.responseText.replace(match, ''); + var scripts = this.transport.responseText.match(match); + + if (receiver) { + if (this.options.insertion) { + new this.options.insertion(receiver, response); + } else { + receiver.innerHTML = response; + } + } + + if (this.responseIsSuccess()) { + if (this.onComplete) + setTimeout((function() {this.onComplete( + this.transport)}).bind(this), 10); + } + + if (this.options.evalScripts && scripts) { + match = new RegExp(Ajax.Updater.ScriptFragment, 'im'); + setTimeout((function() { + for (var i = 0; i < scripts.length; i++) + eval(scripts[i].match(match)[1]); + }).bind(this), 10); + } + } +}); + +Ajax.PeriodicalUpdater = Class.create(); +Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { + initialize: function(container, url, options) { + this.setOptions(options); + this.onComplete = this.options.onComplete; + + this.frequency = (this.options.frequency || 2); + this.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.onComplete = undefined; + clearTimeout(this.timer); + (this.onComplete || Ajax.emptyFunction).apply(this, arguments); + }, + + updateComplete: function(request) { + if (this.options.decay) { + this.decay = (request.responseText == this.lastText ? + this.decay * this.options.decay : 1); + + this.lastText = request.responseText; + } + this.timer = setTimeout(this.onTimerEvent.bind(this), + this.decay * this.frequency * 1000); + }, + + onTimerEvent: function() { + this.updater = new Ajax.Updater(this.container, this.url, this.options); + } +}); + +document.getElementsByClassName = function(className) { + var children = document.getElementsByTagName('*') || document.all; + var elements = new Array(); + + for (var i = 0; i < children.length; i++) { + var child = children[i]; + var classNames = child.className.split(' '); + for (var j = 0; j < classNames.length; j++) { + if (classNames[j] == className) { + elements.push(child); + break; + } + } + } + + return elements; +} + +/*--------------------------------------------------------------------------*/ + +if (!window.Element) { + var Element = new Object(); +} + +Object.extend(Element, { + toggle: function() { + for (var i = 0; i < arguments.length; i++) { + var element = $(arguments[i]); + element.style.display = + (element.style.display == 'none' ? '' : 'none'); + } + }, + + hide: function() { + for (var i = 0; i < arguments.length; i++) { + var element = $(arguments[i]); + element.style.display = 'none'; + } + }, + + show: function() { + for (var i = 0; i < arguments.length; i++) { + var element = $(arguments[i]); + element.style.display = ''; + } + }, + + remove: function(element) { + element = $(element); + element.parentNode.removeChild(element); + }, + + getHeight: function(element) { + element = $(element); + return element.offsetHeight; + }, + + hasClassName: function(element, className) { + element = $(element); + if (!element) + return; + var a = element.className.split(' '); + for (var i = 0; i < a.length; i++) { + if (a[i] == className) + return true; + } + return false; + }, + + addClassName: function(element, className) { + element = $(element); + Element.removeClassName(element, className); + element.className += ' ' + className; + }, + + removeClassName: function(element, className) { + element = $(element); + if (!element) + return; + var newClassName = ''; + var a = element.className.split(' '); + for (var i = 0; i < a.length; i++) { + if (a[i] != className) { + if (i > 0) + newClassName += ' '; + newClassName += a[i]; + } + } + element.className = newClassName; + }, + + // removes whitespace-only text node children + cleanWhitespace: function(element) { + var element = $(element); + for (var i = 0; i < element.childNodes.length; i++) { + var node = element.childNodes[i]; + if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) + Element.remove(node); + } + } +}); + +var Toggle = new Object(); +Toggle.display = Element.toggle; + +/*--------------------------------------------------------------------------*/ + +Abstract.Insertion = function(adjacency) { + this.adjacency = adjacency; +} + +Abstract.Insertion.prototype = { + initialize: function(element, content) { + this.element = $(element); + this.content = content; + + if (this.adjacency && this.element.insertAdjacentHTML) { + this.element.insertAdjacentHTML(this.adjacency, this.content); + } else { + this.range = this.element.ownerDocument.createRange(); + if (this.initializeRange) this.initializeRange(); + this.fragment = this.range.createContextualFragment(this.content); + this.insertContent(); + } + } +} + +var Insertion = new Object(); + +Insertion.Before = Class.create(); +Insertion.Before.prototype = Object.extend(new Abstract.Insertion('beforeBegin'), { + initializeRange: function() { + this.range.setStartBefore(this.element); + }, + + insertContent: function() { + this.element.parentNode.insertBefore(this.fragment, this.element); + } +}); + +Insertion.Top = Class.create(); +Insertion.Top.prototype = Object.extend(new Abstract.Insertion('afterBegin'), { + initializeRange: function() { + this.range.selectNodeContents(this.element); + this.range.collapse(true); + }, + + insertContent: function() { + this.element.insertBefore(this.fragment, this.element.firstChild); + } +}); + +Insertion.Bottom = Class.create(); +Insertion.Bottom.prototype = Object.extend(new Abstract.Insertion('beforeEnd'), { + initializeRange: function() { + this.range.selectNodeContents(this.element); + this.range.collapse(this.element); + }, + + insertContent: function() { + this.element.appendChild(this.fragment); + } +}); + +Insertion.After = Class.create(); +Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), { + initializeRange: function() { + this.range.setStartAfter(this.element); + }, + + insertContent: function() { + this.element.parentNode.insertBefore(this.fragment, + this.element.nextSibling); + } +}); + +var Field = { + clear: function() { + for (var i = 0; i < arguments.length; i++) + $(arguments[i]).value = ''; + }, + + focus: function(element) { + $(element).focus(); + }, + + present: function() { + for (var i = 0; i < arguments.length; i++) + if ($(arguments[i]).value == '') return false; + return true; + }, + + select: function(element) { + $(element).select(); + }, + + activate: function(element) { + $(element).focus(); + $(element).select(); + } +} + +/*--------------------------------------------------------------------------*/ + +var Form = { + serialize: function(form) { + var elements = Form.getElements($(form)); + var queryComponents = new Array(); + + for (var i = 0; i < elements.length; i++) { + var queryComponent = Form.Element.serialize(elements[i]); + if (queryComponent) + queryComponents.push(queryComponent); + } + + return queryComponents.join('&'); + }, + + getElements: function(form) { + var form = $(form); + var elements = new Array(); + + for (tagName in Form.Element.Serializers) { + var tagElements = form.getElementsByTagName(tagName); + for (var j = 0; j < tagElements.length; j++) + elements.push(tagElements[j]); + } + return elements; + }, + + getInputs: function(form, typeName, name) { + var form = $(form); + var inputs = form.getElementsByTagName('input'); + + if (!typeName && !name) + return inputs; + + var matchingInputs = new Array(); + for (var i = 0; i < inputs.length; i++) { + var input = inputs[i]; + if ((typeName && input.type != typeName) || + (name && input.name != name)) + continue; + matchingInputs.push(input); + } + + return matchingInputs; + }, + + disable: function(form) { + var elements = Form.getElements(form); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + element.blur(); + element.disabled = 'true'; + } + }, + + enable: function(form) { + var elements = Form.getElements(form); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + element.disabled = ''; + } + }, + + focusFirstElement: function(form) { + var form = $(form); + var elements = Form.getElements(form); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + if (element.type != 'hidden' && !element.disabled) { + Field.activate(element); + break; + } + } + }, + + reset: function(form) { + $(form).reset(); + } +} + +Form.Element = { + serialize: function(element) { + var element = $(element); + var method = element.tagName.toLowerCase(); + var parameter = Form.Element.Serializers[method](element); + + if (parameter) + return encodeURIComponent(parameter[0]) + '=' + + encodeURIComponent(parameter[1]); + }, + + getValue: function(element) { + var element = $(element); + var method = element.tagName.toLowerCase(); + var parameter = Form.Element.Serializers[method](element); + + if (parameter) + return parameter[1]; + } +} + +Form.Element.Serializers = { + input: function(element) { + switch (element.type.toLowerCase()) { + case 'submit': + case 'hidden': + case 'password': + case 'text': + return Form.Element.Serializers.textarea(element); + case 'checkbox': + case 'radio': + return Form.Element.Serializers.inputSelector(element); + } + return false; + }, + + inputSelector: function(element) { + if (element.checked) + return [element.name, element.value]; + }, + + textarea: function(element) { + return [element.name, element.value]; + }, + + select: function(element) { + var value = ''; + if (element.type == 'select-one') { + var index = element.selectedIndex; + if (index >= 0) + value = element.options[index].value || element.options[index].text; + } else { + value = new Array(); + for (var i = 0; i < element.length; i++) { + var opt = element.options[i]; + if (opt.selected) + value.push(opt.value || opt.text); + } + } + return [element.name, value]; + } +} + +/*--------------------------------------------------------------------------*/ + +var $F = Form.Element.getValue; + +/*--------------------------------------------------------------------------*/ + +Abstract.TimedObserver = function() {} +Abstract.TimedObserver.prototype = { + initialize: function(element, frequency, callback) { + this.frequency = frequency; + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + this.registerCallback(); + }, + + registerCallback: function() { + setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + onTimerEvent: function() { + var value = this.getValue(); + if (this.lastValue != value) { + this.callback(this.element, value); + this.lastValue = value; + } + } +} + +Form.Element.Observer = Class.create(); +Form.Element.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.Observer = Class.create(); +Form.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { + getValue: function() { + return Form.serialize(this.element); + } +}); + +/*--------------------------------------------------------------------------*/ + +Abstract.EventObserver = function() {} +Abstract.EventObserver.prototype = { + 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() { + var elements = Form.getElements(this.element); + for (var i = 0; i < elements.length; i++) + this.registerCallback(elements[i]); + }, + + registerCallback: function(element) { + if (element.type) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + element.target = this; + element.prev_onclick = element.onclick || Prototype.emptyFunction; + element.onclick = function() { + this.prev_onclick(); + this.target.onElementEvent(); + } + break; + case 'password': + case 'text': + case 'textarea': + case 'select-one': + case 'select-multiple': + element.target = this; + element.prev_onchange = element.onchange || Prototype.emptyFunction; + element.onchange = function() { + this.prev_onchange(); + this.target.onElementEvent(); + } + break; + } + } + } +} + +Form.Element.EventObserver = Class.create(); +Form.Element.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.EventObserver = Class.create(); +Form.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { + getValue: function() { + return Form.serialize(this.element); + } +}); + + +if (!window.Event) { + var Event = new Object(); +} + +Object.extend(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, + + element: function(event) { + return event.target || event.srcElement; + }, + + isLeftClick: function(event) { + return (((event.which) && (event.which == 1)) || + ((event.button) && (event.button == 1))); + }, + + pointerX: function(event) { + return event.pageX || (event.clientX + + (document.documentElement.scrollLeft || document.body.scrollLeft)); + }, + + pointerY: function(event) { + return event.pageY || (event.clientY + + (document.documentElement.scrollTop || document.body.scrollTop)); + }, + + stop: function(event) { + if (event.preventDefault) { + event.preventDefault(); + event.stopPropagation(); + } else { + event.returnValue = false; + } + }, + + // find the first node with the given tagName, starting from the + // node the event was triggered on; traverses the DOM upwards + findElement: function(event, tagName) { + var element = Event.element(event); + while (element.parentNode && (!element.tagName || + (element.tagName.toUpperCase() != tagName.toUpperCase()))) + element = element.parentNode; + return element; + }, + + observers: false, + + _observeAndCache: function(element, name, observer, useCapture) { + if (!this.observers) this.observers = []; + if (element.addEventListener) { + this.observers.push([element, name, observer, useCapture]); + element.addEventListener(name, observer, useCapture); + } else if (element.attachEvent) { + this.observers.push([element, name, observer, useCapture]); + element.attachEvent('on' + name, observer); + } + }, + + unloadCache: function() { + if (!Event.observers) return; + for (var i = 0; i < Event.observers.length; i++) { + Event.stopObserving.apply(this, Event.observers[i]); + Event.observers[i][0] = null; + } + Event.observers = false; + }, + + observe: function(element, name, observer, useCapture) { + var element = $(element); + useCapture = useCapture || false; + + if (name == 'keypress' && + ((/Konqueror|Safari|KHTML/.test(navigator.userAgent)) + || element.attachEvent)) + name = 'keydown'; + + this._observeAndCache(element, name, observer, useCapture); + }, + + stopObserving: function(element, name, observer, useCapture) { + var element = $(element); + useCapture = useCapture || false; + + if (name == 'keypress' && + ((/Konqueror|Safari|KHTML/.test(navigator.userAgent)) + || element.detachEvent)) + name = 'keydown'; + + if (element.removeEventListener) { + element.removeEventListener(name, observer, useCapture); + } else if (element.detachEvent) { + element.detachEvent('on' + name, observer); + } + } +}); + +/* prevent memory leaks in IE */ +Event.observe(window, 'unload', Event.unloadCache, false); + +var Position = { + + // set to true if needed, warning: firefox performance problems + // NOT neeeded for page scrolling, only if draggable contained in + // scrollable elements + includeScrollOffsets: false, + + // must be called before calling withinIncludingScrolloffset, every time the + // page is scrolled + prepare: function() { + this.deltaX = window.pageXOffset + || document.documentElement.scrollLeft + || document.body.scrollLeft + || 0; + this.deltaY = window.pageYOffset + || document.documentElement.scrollTop + || document.body.scrollTop + || 0; + }, + + realOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + element = element.parentNode; + } while (element); + return [valueL, valueT]; + }, + + cumulativeOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + return [valueL, valueT]; + }, + + // caches x/y coordinate pair to use with overlap + within: function(element, x, y) { + if (this.includeScrollOffsets) + return this.withinIncludingScrolloffsets(element, x, y); + this.xcomp = x; + this.ycomp = y; + this.offset = this.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 = this.realOffset(element); + + this.xcomp = x + offsetcache[0] - this.deltaX; + this.ycomp = y + offsetcache[1] - this.deltaY; + this.offset = this.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); + }, + + // within must be called directly before + 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; + }, + + clone: function(source, target) { + source = $(source); + target = $(target); + target.style.position = 'absolute'; + var offsets = this.cumulativeOffset(source); + target.style.top = offsets[1] + 'px'; + target.style.left = offsets[0] + 'px'; + target.style.width = source.offsetWidth + 'px'; + target.style.height = source.offsetHeight + 'px'; + } +} diff --git a/docs/skin/screen.css b/docs/skin/screen.css new file mode 100644 index 0000000..221cbe5 --- /dev/null +++ b/docs/skin/screen.css @@ -0,0 +1,587 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You under the Apache License, Version 2.0 +* (the "License"); you may not use this file except in compliance with +* the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT 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("images/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; + text-align: right; + color: white; + background-color: #000000; + z-index:0; + background-image: url(images/rc-t-l-5-1header-2searchbox-3searchbox.png); + background-repeat: no-repeat; + background-position: top left; + bottom: -1px; /* compensate for IE rendering issue */ +} + +#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; +} +/* background: #CFDCED url("images/tab-right.gif") no-repeat right top;*/ +#tabs li { + float: left; + background-image: url(images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png); + background-repeat: no-repeat; + background-position: top right; + background-color: #000000; + margin: 0 3px 0 0; + padding: 0; +} + +/*background: url("images/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; + background-image: url(images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png); + background-repeat: no-repeat; + background-position: top left; + 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; + background-image: url(images/rc-t-r-5-1header-2tab-selected-3tab-selected.png); + background-repeat: no-repeat; + background-position: top right; +} +#top .header .current a { + font-weight: bold; + padding-bottom: 5px; + color: white; + background-image: url(images/rc-t-l-5-1header-2tab-selected-3tab-selected.png); + background-repeat: no-repeat; + background-position: top left; +} +#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('images/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; +} +/** +* Round corner +*/ +#roundtop { + background-image: url(images/rc-t-r-15-1body-2menu-3menu.png); + background-repeat: no-repeat; + background-position: top right; +} + +#roundbottom { + background-image: url(images/rc-b-r-15-1body-2menu-3menu.png); + background-repeat: no-repeat; + background-position: top right; +} + +img.corner { + width: 15px; + height: 15px; + border: none; + display: block !important; +} + +.roundtopsmall { + background-image: url(images/rc-t-r-5-1header-2searchbox-3searchbox.png); + background-repeat: no-repeat; + background-position: top right; +} + +#roundbottomsmall { + background-image: url(images/rc-b-r-5-1header-2tab-selected-3tab-selected.png); + background-repeat: no-repeat; + background-position: top right; +} + +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; + top:-18px; + left:10px; + z-index: 20; + background-color: #f90; + font-size : 70%; + +} + +.menutitle { + cursor:pointer; + padding: 3px 12px; + margin-left: 10px; + background-image: url('images/chapter.gif'); + background-repeat: no-repeat; + background-position: center left; + font-weight : bold; + + +} + +.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('images/page.gif'); + background-repeat: no-repeat; + background-position: center left; + font-weight : normal; + margin-left: 10px; +} + +#menu .menupage { + margin: 2px 0px 1px 10px; + padding: 0px 3px 0px 12px; + background-image: url('images/page.gif'); + background-repeat: no-repeat; + background-position: center left; + font-style : normal; +} +#menu .menupagetitle { + padding: 0px 0px 0px 1px; + font-style : normal; + border-style: solid; + border-width: 1px; + margin-right: 10px; + +} +#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 #credit { + text-align: center; +} +#menu #credit2 { + text-align: center; + padding: 3px 3px 3px 3px; + background-color: #ffffff; +} +#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('images/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('../images/instruction_arrow.png'); + list-style-position: outside; + margin-left: 2em; +} \ No newline at end of file diff --git a/docs/zookeeperAdmin.html b/docs/zookeeperAdmin.html new file mode 100644 index 0000000..d02522d --- /dev/null +++ b/docs/zookeeperAdmin.html @@ -0,0 +1,2231 @@ + + + + + + + +ZooKeeper Administrator's Guide + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Administrator's Guide

+

A Guide to Deployment and Administration

+ + + + + + + + + +

Deployment

+
+

This section contains information about deploying Zookeeper and + covers these topics:

+ +

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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Support Matrix
Operating SystemClientServerNative ClientContrib
GNU/LinuxDevelopment and ProductionDevelopment and ProductionDevelopment and ProductionDevelopment and Production
SolarisDevelopment and ProductionDevelopment and ProductionNot SupportedNot Supported
FreeBSDDevelopment and ProductionDevelopment and ProductionNot SupportedNot Supported
WindowsDevelopment and ProductionDevelopment and ProductionNot SupportedNot Supported
Mac OS XDevelopment OnlyDevelopment OnlyNot SupportedNot 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.6 or greater (JDK 6 or + greater). 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 + , 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 setting 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 +

    + +
  2. + + +
  3. + +

    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.

    + +
  4. + + +
  5. + +

    Install the ZooKeeper Server Package. It can be downloaded + from: +

    + +

    + + + http://zookeeper.apache.org/releases.html + + +

    + +
  6. + + +
  7. + +

    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. 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. 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.

    +
  8. + + +
  9. +

    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.

    + +
  10. + + +
  11. + +

    If your configuration file is set up, you can start a + ZooKeeper server:

    + + +

    +$ java -cp zookeeper.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf \ + org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg + +

    + + +

    QuorumPeerMain starts a ZooKeeper server, + JMX + management beans are also registered which allows + management through a JMX management console. + The ZooKeeper JMX + document 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.

    + + +
  12. + + +
  13. + +

    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 +

    + +
  14. + +
+ +

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 section of the ZooKeeper Getting Started + Guide.

+

For information on installing the client side libraries, refer to + the Bindings + section of the ZooKeeper + Programmer's Guide.

+
+ + + +

Administration

+
+

This section contains information about running and maintaining + ZooKeeper and covers these topics:

+ + +

Designing a ZooKeeper Deployment

+

The reliablity 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.

    + +
  2. + +
  3. +

    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.

    + +
  4. + +
+

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 resourses 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 + 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 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 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 <count> 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.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf org.apache.zookeeper.server.PurgeTxnLog <dataDir> <snapDir> -n <count>
+

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 + below.

+ +

Debug Log Cleanup (log4j)

+

See the section on 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 or + SMF + (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.

+ +

Monitoring

+

The ZooKeeper service can be monitored in one of two + primary ways; 1) the command port through the use of 4 letter words and 2) JMX. See the appropriate section for + your environment/requirements.

+ +

Logging

+

ZooKeeper uses log4j version 1.2 as + its logging infrastructure. 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, see + Log4j Default Initialization Procedure + 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.

+ +

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.

+
+ + +
+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 + effect 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 snaphots.

+
+
Note
+
+ +

Having a dedicated log device has a large impact on + throughput and stable latencies. It is highly recommened 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).

+
+ + +
+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.

+
+ + +
+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.

+
+ + +
+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"

+
+ +
+ +

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 "0" corresponds + to the original UDP-based version, "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. Currently, algorithm 3 is the default

+
+
Note
+
+ +

The implementations of leader election 0, 1, and 2 are now + deprecated . We have the intention + of removing them in the next release, at which point only the + FastLeaderElection will be available. +

+ +
+
+
+ + +
+initLimit +
+
+

(No Java system property)

+

Amount of time, in ticks (see 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.

+
+ + +
+leaderServes +
+
+

(Java system property: zookeeper.leaderServes)

+

Leader accepts client connections. Default value is "yes". + The leader machine coordinates updates. For higher update + throughput at thes 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. The leader election port is only necessary if electionAlg + is 1, 2, or 3 (default). If electionAlg is 0, then the second port is not + necessary. If you want to test multiple servers on a single machine, then + different ports can be used for each server.

+
+ + +
+syncLimit +
+
+

(No Java system property)

+

Amount of time, in ticks (see 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 + +

+
+ + +
+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 + +

+
+ + +
+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.

+ +
+
+
+ + +
+4lw.commands.whitelist +
+
+

(Java system property: zookeeper.4lw.commands.whitelist)

+

+New in 3.4.10: + This property contains a list of comma separated + Four Letter Words commands. It is introduced + to provide fine grained control over the set of commands ZooKeeper can execute, + so users can turn off certain commands if necessary. + By default it contains all supported four letter word commands except "wchp" and "wchc", + if the property is not specified. If the property is specified, then only commands listed + in the whitelist are enabled. +

+

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
+              
+

Users can also use asterisk option so they 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=*
+              
+
+ + +
+ipReachableTimeout +
+
+

(Java system property: zookeeper.ipReachableTimeout)

+

+New in 3.4.11: + Set this timeout value for IP addresses reachable checking when hostname is resolved, as mesured in + milliseconds. + By default, ZooKeeper will use the first IP address of the hostname(without any reachable checking). + When zookeeper.ipReachableTimeout is set(larger than 0), ZooKeeper will will try to pick up the first + IP address which is reachable. This is done by calling Java API InetAddress.isReachable(long timeout) + function, in which this timeout value is used. If none of such reachable IP address can be found, the + first IP address of the hostname will be used anyway. +

+
+ + +
+tcpKeepAlive +
+
+

(Java system property: zookeeper.tcpKeepAlive)

+

+New in 3.4.11: + 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. +

+
+ + +
+

+ +

Authentication & Authorization Options

+

The options in this section allow control over + authentication/authorization performed by the service.

+
+ +
+zookeeper.DigestAuthenticationProvider.superDigest +
+
+

(Java system property only: 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:<password>". Provide the + generated "super:<data>" 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:<password>". 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.

+
+ + +
+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.

+
+ + +
+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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Trace Mask Bit Values
0b0000000000Unused, reserved for future use.
0b0000000010Logs client requests, excluding ping + requests.
0b0000000100Unused, reserved for future use.
0b0000001000Logs client ping requests.
0b0000010000Logs packets received from the quorum peer that is + the current leader, excluding ping requests.
0b0000100000Logs addition, removal and validation of client + sessions.
0b0001000000Logs delivery of watch events to client + sessions.
0b0010000000Logs ping packets received from the quorum peer + that is the current leader.
0b0100000000Unused, reserved for future use.
0b1000000000Unused, 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
+              
+
+ +
+ +

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 default is + 0xfffff, or just under 1M. If this option is changed, the system + property must be set on all servers and clients otherwise + problems will arise. This is really a sanity check. ZooKeeper is + designed to store data on the order of kilobytes in size.

+
+ + +
+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.

+
+ + +
+ +

Communication using the Netty framework

+

+New in + 3.4: Netty + 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. +

+

Prior to version 3.4 ZooKeeper has always used NIO + directly, however in versions 3.4 and later Netty is + supported as an option to NIO (replaces). NIO continues to + be the default, however Netty based communication can be + used in place of NIO by setting the environment variable + "zookeeper.serverCnxnFactory" to + "org.apache.zookeeper.server.NettyServerCnxnFactory". You + have the option of setting this on either the client(s) or + server(s), typically you would want to set this on both, + however that is at your discretion. +

+

+ TBD - tuning options for netty - currently there are none that are netty specific but we should add some. Esp around max bound on the number of reader worker threads netty creates. +

+

+ TBD - how to manage encryption +

+

+ TBD - how to manage certificates +

+ +

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.

+
+ +
+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. This + only works on the leader.

+
+ + +
+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.

+
+ + +
+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
+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
+zk_fsync_threshold_exceed_count	0
+
+

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
+
+ +
+

Here's an example of the ruok + command:

+
$ echo ruok | nc 127.0.0.1 5111
+imok
+
+ +

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 files in it:

+
    + +
  • + +

    +myid - contains a single integer in + human readable ASCII text that represents the server id.

    + +
  • + + +
  • + +

    +snapshot.<zxid> - 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 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 trouble shooting.

+

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 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

+

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.

+ +

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 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 should mitigate it.

+
+ + +
+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-tennant installations see the section + detailing ZooKeeper "chroot" support, this can be very useful + when deploying many applications/services interfacing to a + single ZooKeeper cluster.

+
+ +

+ +

+
+ +
 
+
+ + + diff --git a/docs/zookeeperAdmin.pdf b/docs/zookeeperAdmin.pdf new file mode 100644 index 0000000..8d0028e Binary files /dev/null and b/docs/zookeeperAdmin.pdf differ diff --git a/docs/zookeeperHierarchicalQuorums.html b/docs/zookeeperHierarchicalQuorums.html new file mode 100644 index 0000000..5ebde0a --- /dev/null +++ b/docs/zookeeperHierarchicalQuorums.html @@ -0,0 +1,276 @@ + + + + + + + +Introduction to hierarchical quorums + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

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/docs/zookeeperHierarchicalQuorums.pdf b/docs/zookeeperHierarchicalQuorums.pdf new file mode 100644 index 0000000..fd20251 Binary files /dev/null and b/docs/zookeeperHierarchicalQuorums.pdf differ diff --git a/docs/zookeeperInternals.html b/docs/zookeeperInternals.html new file mode 100644 index 0000000..00e907d --- /dev/null +++ b/docs/zookeeperInternals.html @@ -0,0 +1,805 @@ + + + + + + + +ZooKeeper Internals + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Internals

+ + + + + + + +

Introduction

+
+

This document contains information on the inner workings of ZooKeeper. + So far, it discusses these topics: +

+ +
+ + + +

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, it will be eventually delivered by all servers.

+
+ + +
+ +Total order + +
+
+

If a message is +delivered before message b by one server, a will be delivered before b by all +servers. If a and b are delivered messages, either a will be delivered before b +or b will be delivered before a.

+
+ + +
+ +Causal order + +
+
+

+If a message b is sent after a message a has been delivered by the sender of b, +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 we achieve consensus in the presence of failures +we use timeouts. However, we rely on times for liveness not for correctness. So, +if timeouts stop working (clocks malfunction for example) 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 correspond 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 it has two parts represent the zxid 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 +included all the proposals that really have been committed? The answer is yes. +Below, we make clear why.) +

+ +

Leader Activation

+

+Leader activation includes leader election. We currently have two leader election +algorithms in ZooKeeper: LeaderElection and FastLeaderElection (AuthFastLeaderElection +is a variant of FastLeaderElection that uses UDP and allows servers to perform a simple +form of authentication to avoid IP spoofing). ZooKeeper messaging doesn't care about the +exact method of electing a leader has 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 amoung 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 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 you 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), fter 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 have 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 COMMITED.

    +
  • + +
+

+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.

+

+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 proposals +message when that proposal is committed.

    +
  • + + +
+ +

Summary

+

So there you go. Why does it work? Specifically, why does is 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 uncommited 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. +

+
+ + + +

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 as an abstraction layer for logging. +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 section +of the ZooKeeper Administrator's Guide. + + +

+ +

Developer Guidelines

+

Please follow the +slf4j manual when creating log statements within code. +Also read the +FAQ on 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.

    +
  2. + +
  3. +

    WARN level designates potentially harmful situations.

    +
  4. + +
  5. +

    INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

    +
  6. + +
  7. +

    DEBUG Level designates fine-grained informational events that are most useful to debug an application.

    +
  8. + +
  9. +

    TRACE Level designates finer-grained informational events than the DEBUG.

    +
  10. + +
+

+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/docs/zookeeperInternals.pdf b/docs/zookeeperInternals.pdf new file mode 100644 index 0000000..69cadc4 Binary files /dev/null and b/docs/zookeeperInternals.pdf differ diff --git a/docs/zookeeperJMX.html b/docs/zookeeperJMX.html new file mode 100644 index 0000000..c82f5f8 --- /dev/null +++ b/docs/zookeeperJMX.html @@ -0,0 +1,479 @@ + + + + + + + +ZooKeeper JMX + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper JMX

+ + + + + + + +

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 page to get started with JMX. +

+

See the + JMX Management Guide 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 initalization 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 + 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 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. +

+
+ + + +

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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MBeans, their names and description
MBeanMBean Object NameDescription
QuorumReplicatedServer_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|RemotePeerreplica.<#>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).
LeaderElectionLeaderElectionRepresents a ZooKeeper cluster leader election which is + in progress. Provides information about the election, such as + when it started.
LeaderLeaderIndicates 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.
FollowerFollowerIndicates 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.
DataTreeInMemoryDataTreeStatistics 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<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.
+

This table details JMX for a standalone server. Typically + standalone is only used in development situations.

+ + + +MBeans, their names and description + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MBeans, their names and description
MBeanMBean Object NameDescription
ZooKeeperServerStandaloneServer_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).
DataTreeInMemoryDataTreeStatistics 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/docs/zookeeperJMX.pdf b/docs/zookeeperJMX.pdf new file mode 100644 index 0000000..316bd66 Binary files /dev/null and b/docs/zookeeperJMX.pdf differ diff --git a/docs/zookeeperObservers.html b/docs/zookeeperObservers.html new file mode 100644 index 0000000..42f9d16 --- /dev/null +++ b/docs/zookeeperObservers.html @@ -0,0 +1,366 @@ + + + + + + + +ZooKeeper Observers + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Observers

+ + + + + + + +

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. +

+
+ + + +

Example use cases

+
+

+ Two example use cases for Observers are listed below. In fact, wherever + you wish to scale the numbe 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/docs/zookeeperObservers.pdf b/docs/zookeeperObservers.pdf new file mode 100644 index 0000000..843e860 Binary files /dev/null and b/docs/zookeeperObservers.pdf differ diff --git a/docs/zookeeperOtherInfo.html b/docs/zookeeperOtherInfo.html new file mode 100644 index 0000000..24b594c --- /dev/null +++ b/docs/zookeeperOtherInfo.html @@ -0,0 +1,230 @@ + + + + + + + +ZooKeeper + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper

+
+ +
+ + + + + + +

Other Info

+
+

currently empty

+
+ +

+ +

+
+ +
 
+
+ + + diff --git a/docs/zookeeperOtherInfo.pdf b/docs/zookeeperOtherInfo.pdf new file mode 100644 index 0000000..bbefa13 Binary files /dev/null and b/docs/zookeeperOtherInfo.pdf differ diff --git a/docs/zookeeperOver.html b/docs/zookeeperOver.html new file mode 100644 index 0000000..0968d3a --- /dev/null +++ b/docs/zookeeperOver.html @@ -0,0 +1,704 @@ + + + + + + + +ZooKeeper + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper

+ + + + + + + +

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 hierarchal namespace which is organized similarly to a standard + file system. The name space 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 acheive 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 sets of hosts called an ensemble.

+ + + + + + + +
ZooKeeper Service
+ + + +
+

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 name space 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 name space is identified by a + path.

+ + + + + + + +
ZooKeeper's Hierarchical Namespace
+ + + +
+ +

Nodes and ephemeral nodes

+

Unlike is 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. Ephemeral nodes are useful when you + want to implement [tbd].

+ +

Conditional updates and watches

+

ZooKeeper supports the concept of watches. + Clients can set a watch on a znodes. 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. And if the + connection between the client and one of the Zoo Keeper servers is + broken, the client will receive a local notification. These can be used + to [tbd].

+ +

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.

    + +
  • + +
+
    + +
  • + +

    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.

    + +
  • + +
+

For more information on these, and how they can be used, see + [tbd] +

+ +

Simple API

+

One of the design goals of ZooKeeper is provide 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

+
+ +
+

For a more in-depth discussion on these, and how they can be used + to implement higher level operations, please refer to + [tbd] +

+ +

Implementation

+

+ZooKeeper Components 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 components.

+ + + + + + + +
ZooKeeper Components
+ + + +
+

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 irequests. 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. Some + distributed applications have used it to: [tbd: add uses from + white paper and video presentation.] For more information, see + [tbd] +

+ +

Performance

+

ZooKeeper is designed to be highly performant. 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.) 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
+ + + +
+

The figure ZooKeeper Throughput as the Read-Write Ratio Varies 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.

+
+
+

Benchmarks also indicate that it is reliable, too. Reliability in the Presence of Errors shows how a deployment responds to + various failures. The events marked in the figure are the + following:

+
    + +
  1. + +

    Failure and recovery of a follower

    + +
  2. + + +
  3. + +

    Failure and recovery of a different follower

    + +
  4. + + +
  5. + +

    Failure of the leader

    + +
  6. + + +
  7. + +

    Failure and recovery of two followers

    + +
  8. + + +
  9. + +

    Failure of another leader

    + +
  10. + +
+ +

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
+ + + +
+

The 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 + + 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 + + for more information. +

+
+ +

+ +

+
+ +
 
+
+ + + diff --git a/docs/zookeeperOver.pdf b/docs/zookeeperOver.pdf new file mode 100644 index 0000000..7ce6705 Binary files /dev/null and b/docs/zookeeperOver.pdf differ diff --git a/docs/zookeeperProgrammers.html b/docs/zookeeperProgrammers.html new file mode 100644 index 0000000..b134409 --- /dev/null +++ b/docs/zookeeperProgrammers.html @@ -0,0 +1,2285 @@ + + + + + + + +ZooKeeper Programmer's Guide + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Programmer's Guide

+

Developing Distributed Applications that use ZooKeeper

+ + + + + + + + + +

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 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 next four sections provide practical programming + information. These are:

+ +

The book concludes with an appendix containing links to other + useful, ZooKeeper-related information.

+

Most of 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 chaptes on + the ZooKeeper Data Model and ZooKeeper Basic Operations. Also, + the Simple Programmming + Example [tbd] is helpful for understanding the basic + structure of a ZooKeeper client application.

+
+ + + +

The ZooKeeper Data Model

+
+

ZooKeeper has a hierarchal name space, 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 - \u0019 and \u007F + - \u009F.

    + +
  • + + +
  • + +

    The following characters are not allowed: \ud800 -uF8FFF, + \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. For more + information see... )[tbd...] +

+
+
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 refer 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.

+ +
+
+

A znode is the main abstraction a programmer needs to be aware of. Znodes 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.

+ +

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 usually 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.

+ +

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. "<path>0000000001". See + Queue + Recipe 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 "<path>-2147483648").

+ +

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 of 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 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:

+

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)

    +
  2. + +
  3. +

    .... client is partitioned from the + cluster

    +
  4. + +
  5. +

    'disconnected' : client has lost connectivity + with the cluster

    +
  6. + +
  7. +

    .... time elapses, after 'timeout' period the + cluster expires the session, nothing is seen by client as it is + disconnected from cluster

    +
  8. + +
  9. +

    .... time elapses, the client regains network + level connectivity with the cluster

    +
  10. + +
  11. +

    'expired' : eventually the client reconnects to + the cluster, it is then notified of the + expiration

    +
  12. + +
+

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

    +
  2. + +
  3. +

    The ZooKeeper client disconnects from a server when there + are pending operations to that server, i.e., there is a pending asynchronous call. +

    +
  4. + +
+

+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).

+
+ + + +

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.

+ +

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.

    + +
  • + +
+ +

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

+
    + +
  • + +

    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 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:id, + where scheme is a the authentication scheme + that the id corresponds to. For + example, ip:172.16.16.1 is an id for a + host with the address 172.16.16.1.

+

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 clients 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.)

+ +

Builtin ACL Schemes

+

ZooKeeeper has the following built in schemes:

+
    + +
  • +

    +world has a + single id, anyone, that represents + anyone.

    +
  • + + +
  • +

    +auth doesn't + use any id, represents any authenticated + user.

    +
  • + + +
  • +

    +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.

    +
  • + + +
+ +

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.

+
    +
  • + +

    +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/src/c/src/cli.c + for an example of a C client implementation

+ +
+
+
+#include <string.h>
+#include <errno.h>
+
+#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 assocatied 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. +

+
+ + + +

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.

+
+ + +
+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.)

    + +
  2. + + +
  3. + +

    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.

    + +
  4. + +
+
+ + +
+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 + 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 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. + [tbd:..]).

+
+ +
+ +
+
+
+ + + +

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 accross 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 the completion + callback is 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.

+ +

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/src/c.

    + +
  2. + + +
  3. + +

    Change directory to the.../trunk/src/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.

    + +
  4. + + +
  5. + +

    If you are building from a project source package, + unzip/untar the source tarball and cd to the + zookeeper-x.x.x/src/c directory.

    + +
  6. + + +
  7. + +

    Run ./configure <your-options> 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.

    + +
    +
    + +
  8. + + +
  9. + +

    Run make or make + install to build the libraries and install them.

    + +
  10. + + +
  11. + +

    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 +

    + +
  12. + +
+ +

Building Your Own C Client

+

In order to be able to use the ZooKeeper API in your application + you have to remember to

+
    + +
  1. + +

    Include ZooKeeper header: #include + <zookeeper/zookeeper.h>

    + +
  2. + + +
  3. + +

    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 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.

    + +
  4. + +
+
+
Note
+
+

+ See .../trunk/src/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:

+ + +

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

+

+ +

Read Operations

+

+ +

Write Operations

+

+ +

Handling Watches

+

+ +

Miscelleaneous ZooKeeper Operations

+

+
+ + + +

Program Structure, with Simple Example

+
+

+[tbd] +

+
+ + + +

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.

    + +
  2. + + +
  3. + +

    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.

    + +
  4. + + +
  5. + +

    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.

    + +
  6. + + +
  7. + +

    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.

    + +
  8. + + +
  9. + +

    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.

    + +
  10. + +
+
+ + + + + +Links to Other Information + + +

Outside the formal documentation, there're several other sources of + information for ZooKeeper developers.

+ + +
+ +
+ZooKeeper Whitepaper [tbd: find url] + +
+
+

The definitive discussion of ZooKeeper design and performance, + by Yahoo! Research

+
+ + +
+API Reference [tbd: find url] + +
+
+

The complete reference to the ZooKeeper API

+
+ + +
+ +ZooKeeper + Talk at the Hadoup Summit 2008 + +
+
+

A video introduction to ZooKeeper, by Benjamin Reed of Yahoo! + Research

+
+ + +
+ +Barrier and + Queue Tutorial + +
+
+

The excellent Java tutorial by Flavio Junqueira, implementing + simple barriers and producer-consumer queues using ZooKeeper.

+
+ + +
+ +ZooKeeper + - A Reliable, Scalable Distributed Coordination System + +
+
+

An article by Todd Hoff (07/15/2008)

+
+ + +
+ +ZooKeeper Recipes + +
+
+

Pseudo-level discussion of the implementation of various + synchronization solutions with ZooKeeper: Event Handles, Queues, + Locks, and Two-phase Commits.

+
+ + +
+ +[tbd] + +
+
+

Any other good sources anyone can think of...

+
+ +
+ +
+ +

+ +

+
+ +
 
+
+ + + diff --git a/docs/zookeeperProgrammers.pdf b/docs/zookeeperProgrammers.pdf new file mode 100644 index 0000000..cb1db8c Binary files /dev/null and b/docs/zookeeperProgrammers.pdf differ diff --git a/docs/zookeeperQuotas.html b/docs/zookeeperQuotas.html new file mode 100644 index 0000000..597e9e1 --- /dev/null +++ b/docs/zookeeperQuotas.html @@ -0,0 +1,290 @@ + + + + + + + +ZooKeeper Quota's Guide + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Quota's Guide

+

A Guide to Deployment and Administration

+
+
+ +
+
+ + + + + +

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. +

+

+$ 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) + and -b (for bytes).

+

The ZooKeeper quota are stored in ZooKeeper itself in /zookeeper/quota. To disable other people from + changing the quota's set the ACL for /zookeeper/quota such that only admins are able to read and write to it. +

+ +

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/docs/zookeeperQuotas.pdf b/docs/zookeeperQuotas.pdf new file mode 100644 index 0000000..1455fb9 Binary files /dev/null and b/docs/zookeeperQuotas.pdf differ diff --git a/docs/zookeeperReconfig.pdf b/docs/zookeeperReconfig.pdf new file mode 100644 index 0000000..fbf2237 Binary files /dev/null and b/docs/zookeeperReconfig.pdf differ diff --git a/docs/zookeeperStarted.html b/docs/zookeeperStarted.html new file mode 100644 index 0000000..361b334 --- /dev/null +++ b/docs/zookeeperStarted.html @@ -0,0 +1,629 @@ + + + + + + + +ZooKeeper Getting Started Guide + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

ZooKeeper Getting Started Guide

+ + + + + + + +

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.

+ +

Pre-requisites

+

See + System Requirements in the Admin guide.

+ +

Download

+

To get a ZooKeeper distribution, download a recent + + stable 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 + 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.

+ +

Managing ZooKeeper Storage

+

For long running production systems ZooKeeper storage must + be managed externally (dataDir and logs). See the section on + 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 host:port cmd args
+        get path [watch]
+        ls path [watch]
+        set path data [version]
+        delquota [-n|-b] path
+        quit
+        printwatches on|off
+        createpath data acl
+        stat path [watch]
+        listquota path
+        history
+        setAcl path acl
+        getAcl path
+        sync path
+        redo cmdno
+        addauth scheme auth
+        delete path [version]
+        setquota -n|-b val 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, continue with the rest of this document and see the Programmer's Guide.

+ +

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 for + sample code using of 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 is 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 milleseconds 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 dataDirs and + distinct clientPorts 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.

+ +
+
+ +

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.

    + +
  • + + +
  • + +

    +[tbd: what is the other config param?] +

    + +
  • + +
+
+ +

+ +

+
+ +
 
+
+ + + diff --git a/docs/zookeeperStarted.pdf b/docs/zookeeperStarted.pdf new file mode 100644 index 0000000..95d7fcf Binary files /dev/null and b/docs/zookeeperStarted.pdf differ diff --git a/docs/zookeeperTutorial.html b/docs/zookeeperTutorial.html new file mode 100644 index 0000000..e9c0646 --- /dev/null +++ b/docs/zookeeperTutorial.html @@ -0,0 +1,937 @@ + + + + + + + +Programming with ZooKeeper - A basic tutorial + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + +
+
+
+
+ +
+ + +
+ +
+ +   +
+ + + + + +
+ +

Programming with ZooKeeper - A basic tutorial

+ + + + + + + +

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<String> 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 reponsability 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 the root node). Upon reception of a notification, +it checks once more whether the root node has any child.

+
+        /**
+         * 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<String> 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 estructure thata group 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 necessary 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<String> 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
+
+ +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<String> 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<String> 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<String> 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/docs/zookeeperTutorial.pdf b/docs/zookeeperTutorial.pdf new file mode 100644 index 0000000..5b3faf6 Binary files /dev/null and b/docs/zookeeperTutorial.pdf differ diff --git a/ivy.xml b/ivy.xml new file mode 100644 index 0000000..8e5f198 --- /dev/null +++ b/ivy.xml @@ -0,0 +1,155 @@ + + + + + + + + ZooKeeper + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ivysettings.xml b/ivysettings.xml new file mode 100644 index 0000000..1d06c40 --- /dev/null +++ b/ivysettings.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/LICENSE.txt b/src/LICENSE.txt new file mode 100644 index 0000000..867ac6d --- /dev/null +++ b/src/LICENSE.txt @@ -0,0 +1,216 @@ + + 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 jline 0.9.94, which is available under the +2-clause BSD License. For details, see a copy of the license in +lib/jline-0.9.94.LICENSE.txt + +This distribution bundles SLF4J 1.6.1, which is available under the MIT +License. For details, see a copy of the license in +lib/slf4j-1.6.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/src/NOTICE.txt b/src/NOTICE.txt new file mode 100644 index 0000000..08388e8 --- /dev/null +++ b/src/NOTICE.txt @@ -0,0 +1,100 @@ +Apache ZooKeeper +Copyright 2009-2018 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +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/ diff --git a/src/c/CMakeLists.txt b/src/c/CMakeLists.txt new file mode 100644 index 0000000..e7dfbeb --- /dev/null +++ b/src/c/CMakeLists.txt @@ -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. + +cmake_minimum_required(VERSION 3.6) + +project(zookeeper VERSION 3.4.13) +set(email user@zookeeper.apache.org) +set(description "zookeeper C client") + +# general options +include_directories(include tests generated ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) +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) + if(WIN32) + # Note that the generator expression ensures that `/MTd` is used when Debug + # configurations are built. + add_compile_options(/MT$<$:d>) + endif() +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}) + +# 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_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_SOURCE_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_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) + +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_link_libraries(zookeeper PUBLIC + hashtable + $<$:rt> # clock_gettime + $<$:ws2_32>) # Winsock 2.0 + +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/TestClientRetry.cc + tests/TestOperations.cc + tests/TestMulti.cc + tests/TestWatchers.cc + tests/TestClient.cc) + +if(WANT_SYNCAPI) + list(APPEND test_sources tests/PthreadMocks.cc) +endif() + +if(WANT_CPPUNIT) + add_executable(zktest ${test_sources}) + target_compile_definitions(zktest + PRIVATE -DZKSERVER_CMD="${CMAKE_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_SOURCE_DIR}/../.." + "CLASSPATH=$CLASSPATH:$CLOVER_HOME/lib/clover.jar") +endif() diff --git a/src/c/ChangeLog b/src/c/ChangeLog new file mode 100644 index 0000000..c85a6c0 --- /dev/null +++ b/src/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 doesn’t automatically terminate on AUTH_FAILURE and + SESSION_EXPIRED events. + + * all session events should be processed on the completion thread. + + * PING operation doesn’t 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/src/c/INSTALL b/src/c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/src/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/src/c/LICENSE b/src/c/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/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/src/c/Makefile.am b/src/c/Makefile.am new file mode 100644 index 0000000..5993098 --- /dev/null +++ b/src/c/Makefile.am @@ -0,0 +1,128 @@ +# need this for Doxygen integration +include $(top_srcdir)/aminclude.am + +if SOLARIS + SOLARIS_CPPFLAGS = -D_POSIX_PTHREAD_SEMANTICS + SOLARIS_LIB_LDFLAGS = -lnsl -lsocket +endif +AM_CPPFLAGS = -I${srcdir}/include -I${srcdir}/tests -I${srcdir}/generated $(SOLARIS_CPPFLAGS) +AM_CFLAGS = -Wall -Werror +AM_CXXFLAGS = -Wall $(USEIPV6) + +# Additional flags for coverage testing (if enabled) +if ENABLEGCOV + AM_CFLAGS += -fprofile-arcs -ftest-coverage + AM_LDFLAGS = -lgcov +endif + +LIB_LDFLAGS = -no-undefined -version-info 2 $(SOLARIS_LIB_LDFLAGS) + +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/zookeeper_log.h src/zk_log.c src/zk_hashtable.h src/zk_hashtable.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 + +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 + +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 + +TEST_SOURCES = tests/TestDriver.cc tests/LibCMocks.cc tests/LibCSymTable.cc \ + tests/MocksBase.cc tests/ZKMocks.cc tests/Util.cc tests/ThreadingUtil.cc \ + tests/TestClientRetry.cc \ + tests/TestOperations.cc tests/TestZookeeperInit.cc \ + tests/TestZookeeperClose.cc tests/TestClient.cc \ + tests/TestMulti.cc tests/TestWatchers.cc + + +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 +nodist_zktest_st_SOURCES = $(TEST_SOURCES) +zktest_st_LDADD = libzkst.la libhashtable.la $(CPPUNIT_LIBS) +zktest_st_CXXFLAGS = -DUSE_STATIC_LIB $(CPPUNIT_CFLAGS) $(USEIPV6) $(SOLARIS_CPPFLAGS) +zktest_st_LDFLAGS = -static-libtool-libs $(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) + 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 = -static-libtool-libs $(SYMBOL_WRAPPERS_MT) $(SOLARIS_LIB_LDFLAGS) +endif + +run-check: check + ./zktest-st $(TEST_OPTIONS) +if WANT_SYNCAPI + ./zktest-mt $(TEST_OPTIONS) +endif + +clean-local: clean-check + $(RM) $(DX_CLEANFILES) + +clean-check: + $(RM) $(nodist_zktest_st_OBJECTS) $(nodist_zktest_mt_OBJECTS) diff --git a/src/c/NOTICE.txt b/src/c/NOTICE.txt new file mode 100644 index 0000000..dcd9d53 --- /dev/null +++ b/src/c/NOTICE.txt @@ -0,0 +1,41 @@ +Apache ZooKeeper +Copyright 2009-2018 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/src/c/README b/src/c/README new file mode 100644 index 0000000..5115845 --- /dev/null +++ b/src/c/README @@ -0,0 +1,156 @@ + 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 src/c. Skip to step 3. +2) unzip/untar the source tarball and cd to the zookeeper-x.x.x/src/c directory +3) change directory to src/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 run-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 + -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 + +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/src/c/acinclude.m4 b/src/c/acinclude.m4 new file mode 100644 index 0000000..d0041d8 --- /dev/null +++ b/src/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 fo 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/src/c/aminclude.am b/src/c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/src/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/src/c/c-doc.Doxyfile b/src/c/c-doc.Doxyfile new file mode 100644 index 0000000..e9029cb --- /dev/null +++ b/src/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/src/c/cmake_config.h.in b/src/c/cmake_config.h.in new file mode 100644 index 0000000..55efd8a --- /dev/null +++ b/src/c/cmake_config.h.in @@ -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 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 + +/* 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/src/c/configure.ac b/src/c/configure.ac new file mode 100644 index 0000000..2483d38 --- /dev/null +++ b/src/c/configure.ac @@ -0,0 +1,165 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zookeeper C client],3.4.13,[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 + AM_PATH_CPPUNIT(1.10.2) +fi + +if test "$CALLER" = "ANT" ; then +CPPUNIT_CFLAGS="$CPPUNIT_CFLAGS -DZKSERVER_CMD=\"\\\"${base_dir}/src/c/tests/zkServer.sh\\\"\"" +else +CPPUNIT_CFLAGS="$CPPUNIT_CFLAGS -DZKSERVER_CMD=\"\\\"./tests/zkServer.sh\\\"\"" +AC_CHECK_FILES([generated/zookeeper.jute.c 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"]) + +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 + +# 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/src/c/include/proto.h b/src/c/include/proto.h new file mode 100644 index 0000000..bce408e --- /dev/null +++ b/src/c/include/proto.h @@ -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. + */ +#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_CLOSE_OP -11 +#define ZOO_SETAUTH_OP 100 +#define ZOO_SETWATCHES_OP 101 + +#ifdef __cplusplus +} +#endif + +#endif /*PROTO_H_*/ diff --git a/src/c/include/recordio.h b/src/c/include/recordio.h new file mode 100644 index 0000000..eed5f99 --- /dev/null +++ b/src/c/include/recordio.h @@ -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. + */ +#ifndef __RECORDIO_H__ +#define __RECORDIO_H__ + +#include +#include /* for int64_t */ +#ifdef WIN32 +#include "winconfig.h" +#define STRUCT_INITIALIZER(l,r) r +#else +#define STRUCT_INITIALIZER(l,r) .l = r +#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/src/c/include/winconfig.h b/src/c/include/winconfig.h new file mode 100644 index 0000000..c273a93 --- /dev/null +++ b/src/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/src/c/include/zookeeper.h b/src/c/include/zookeeper.h new file mode 100644 index 0000000..aee865b --- /dev/null +++ b/src/c/include/zookeeper.h @@ -0,0 +1,1589 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 + +#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 */ + + /** 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 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); + +/** + * \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 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 exists. The Create Flags affect the creation of nodes. + * If ZOO_EPHEMERAL flag is set, the node will automatically get removed if the + * client session goes away. If the ZOO_SEQUENCE flag is set, 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 flags this parameter can be set to 0 for normal create or an OR + * of the Create Flags + * \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 flags, + string_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 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 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); + +/** + * \brief create a node synchronously. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exists. The Create Flags affect the creation of nodes. + * If ZOO_EPHEMERAL flag is set, the node will automatically get removed if the + * client session goes away. If the ZOO_SEQUENCE flag is set, a unique + * monotonically increasing sequence number is appended to the path name. + * + * \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 flags this parameter can be set to 0 for normal create or an OR + * of the Create Flags + * \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 flags, + char *path_buffer, int path_buffer_len); + +/** + * \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 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); + +#ifdef __cplusplus +} +#endif + +#endif /*ZOOKEEPER_H_*/ diff --git a/src/c/include/zookeeper_log.h b/src/c/include/zookeeper_log.h new file mode 100644 index 0000000..e5917cb --- /dev/null +++ b/src/c/include/zookeeper_log.h @@ -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. + */ + +#ifndef ZK_LOG_H_ +#define ZK_LOG_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern ZOOAPI ZooLogLevel logLevel; +#define LOGSTREAM getLogStream() + +#define LOG_ERROR(x) if(logLevel>=ZOO_LOG_LEVEL_ERROR) \ + log_message(ZOO_LOG_LEVEL_ERROR,__LINE__,__func__,format_log_message x) +#define LOG_WARN(x) if(logLevel>=ZOO_LOG_LEVEL_WARN) \ + log_message(ZOO_LOG_LEVEL_WARN,__LINE__,__func__,format_log_message x) +#define LOG_INFO(x) if(logLevel>=ZOO_LOG_LEVEL_INFO) \ + log_message(ZOO_LOG_LEVEL_INFO,__LINE__,__func__,format_log_message x) +#define LOG_DEBUG(x) if(logLevel==ZOO_LOG_LEVEL_DEBUG) \ + log_message(ZOO_LOG_LEVEL_DEBUG,__LINE__,__func__,format_log_message x) + +ZOOAPI void log_message(ZooLogLevel curLevel, int line,const char* funcName, + const char* message); + +ZOOAPI const char* format_log_message(const char* format,...); + +FILE* getLogStream(); + +#ifdef __cplusplus +} +#endif + +#endif /*ZK_LOG_H_*/ diff --git a/src/c/include/zookeeper_version.h b/src/c/include/zookeeper_version.h new file mode 100644 index 0000000..518f7e6 --- /dev/null +++ b/src/c/include/zookeeper_version.h @@ -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. + */ +#ifndef ZOOKEEPER_VERSION_H_ +#define ZOOKEEPER_VERSION_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZOO_MAJOR_VERSION 3 +#define ZOO_MINOR_VERSION 4 +#define ZOO_PATCH_VERSION 12 + +#ifdef __cplusplus +} +#endif + +#endif /* ZOOKEEPER_VERSION_H_ */ diff --git a/src/c/src/cli.c b/src/c/src/cli.c new file mode 100644 index 0000000..ef32a10 --- /dev/null +++ b/src/c/src/cli.c @@ -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. + */ + +/** + * 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 +#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) +#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 char cmd[1024]; +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_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_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, " 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, " 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 (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++; + if (async) { + rc = zoo_aset(zh, line, ptr, strlen(ptr), -1, my_stat_completion, + strdup(line)); + } else { + struct Stat stat; + rc = zoo_set2(zh, line, ptr, strlen(ptr), -1, &stat); + } + 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 ")) { + int flags = 0; + line += 7; + if (line[0] == '+') { + line++; + if (line[0] == 'e') { + flags |= ZOO_EPHEMERAL; + line++; + } + if (line[0] == 's') { + flags |= ZOO_SEQUENCE; + line++; + } + line++; + } + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + fprintf(stderr, "Creating [%s] node\n", line); +// { +// 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)); +// } + rc = zoo_acreate(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, flags, + 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; + } + if (async) { + rc = zoo_adelete(zh, line, -1, my_void_completion, strdup(line)); + } else { + rc = zoo_delete(zh, line, -1); + } + 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); + } +} + +int main(int argc, char **argv) { +#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; + FILE *fh; + + if (argc < 2) { + fprintf(stderr, + "USAGE %s zookeeper_host_list [clientid_file|cmd:(ls|ls2|create|od|...)]\n", + argv[0]); + fprintf(stderr, + "Version: ZooKeeper cli (c client) version %d.%d.%d\n", + ZOO_MAJOR_VERSION, + ZOO_MINOR_VERSION, + ZOO_PATCH_VERSION); + return 2; + } + if (argc > 2) { + if(strncmp("cmd:",argv[2],4)==0){ + size_t cmdlen = strlen(argv[2]); + if (cmdlen > sizeof(cmd)) { + fprintf(stderr, + "Command length %zu exceeds max length of %zu\n", + cmdlen, + sizeof(cmd)); + return 2; + } + strncpy(cmd, argv[2]+4, sizeof(cmd)); + batchMode=1; + fprintf(stderr,"Batch mode: %s\n",cmd); + }else{ + clientIdFile = argv[2]; + 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 + verbose = 0; + zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); + zoo_deterministic_conn_order(1); // enable deterministic order + hostPort = argv[1]; + zh = zookeeper_init(hostPort, watcher, 30000, &myid, 0, 0); + if (!zh) { + return errno; + } + +#ifdef YCA + if(zoo_add_auth(zh,"yca",p,strlen(p),0,0)!=ZOK) + return 2; +#endif + +#ifdef THREADED + while(!shutdownThisThing) { + 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"); + 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 (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/src/c/src/hashtable/LICENSE.txt b/src/c/src/hashtable/LICENSE.txt new file mode 100644 index 0000000..674a624 --- /dev/null +++ b/src/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/src/c/src/hashtable/hashtable.c b/src/c/src/hashtable/hashtable.c new file mode 100644 index 0000000..763357e --- /dev/null +++ b/src/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,hash(h,k)); + 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/src/c/src/hashtable/hashtable.h b/src/c/src/hashtable/hashtable.h new file mode 100644 index 0000000..cbead18 --- /dev/null +++ b/src/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/src/c/src/hashtable/hashtable_itr.c b/src/c/src/hashtable/hashtable_itr.c new file mode 100644 index 0000000..defac69 --- /dev/null +++ b/src/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/src/c/src/hashtable/hashtable_itr.h b/src/c/src/hashtable/hashtable_itr.h new file mode 100644 index 0000000..30379c7 --- /dev/null +++ b/src/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/src/c/src/hashtable/hashtable_private.h b/src/c/src/hashtable/hashtable_private.h new file mode 100644 index 0000000..3e95f60 --- /dev/null +++ b/src/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/src/c/src/load_gen.c b/src/c/src/load_gen.c new file mode 100644 index 0000000..546d5d1 --- /dev/null +++ b/src/c/src/load_gen.c @@ -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. + */ + +#include +#include "zookeeper_log.h" +#include +#ifdef THREADED +#include +#endif +#include +#include + +static zhandle_t *zh; + +static int shutdownThisThing=0; + +// ***************************************************************************** +// +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){ + 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(("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(("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(SOCKET sock, char* message) +{ + LOG_ERROR(("%s. %d",message, WSAGetLastError())); + closesocket (sock); + return -1; +} + +//--create socket pair for interupting selects. +int create_socket_pair(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(("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(lst,"Error trying to set socket option."); + } + if (bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr)) == SOCKET_ERROR){ + return handle_error(lst,"Error trying to bind socket."); + } + if (listen(lst,1) == SOCKET_ERROR){ + return handle_error(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(lst, "Error while connecting to socket."); + } + if ((fds[1]=accept(lst,0,0)) == INVALID_SOCKET){ + closesocket(fds[0]); + return handle_error(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(("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(("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(adaptor_threads->self_pipe) == -1){ + LOG_ERROR(("Can't make a socket.")); +#else + if(pipe(adaptor_threads->self_pipe)==-1) { + LOG_ERROR(("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); + // 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(("started IO thread")); + fds[0].fd=adaptor_threads->self_pipe[0]; + fds[0].events=POLLIN; + while(!zh->close_requested) { + 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, efds; + struct adaptor_threads *adaptor_threads = zh->adaptor_priv; + api_prolog(zh); + notify_thread_ready(zh); + LOG_DEBUG(("started IO thread")); + FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); + while(!zh->close_requested) { + struct timeval tv; + SOCKET fd; + SOCKET maxfd=adaptor_threads->self_pipe[0]; + int interest; + 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); + } + } + FD_SET( adaptor_threads->self_pipe[0] ,&rfds ); + rc = select((int)maxfd, &rfds, &wfds, &efds, &tv); + if (fd != -1) + { + 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)){} + } +#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(("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(("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(("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 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/src/c/src/recordio.c b/src/c/src/recordio.c new file mode 100644 index 0000000..0fcf48e --- /dev/null +++ b/src/c/src/recordio.c @@ -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. + */ + +#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 = { STRUCT_INITIALIZER (start_record ,ia_start_record), + STRUCT_INITIALIZER (end_record ,ia_end_record), STRUCT_INITIALIZER (start_vector , ia_start_vector), + STRUCT_INITIALIZER (end_vector ,ia_end_vector), STRUCT_INITIALIZER (deserialize_Bool , ia_deserialize_bool), + STRUCT_INITIALIZER (deserialize_Int ,ia_deserialize_int), + STRUCT_INITIALIZER (deserialize_Long , ia_deserialize_long) , + STRUCT_INITIALIZER (deserialize_Buffer, ia_deserialize_buffer), + STRUCT_INITIALIZER (deserialize_String, ia_deserialize_string) }; + +static struct oarchive oa_default = { STRUCT_INITIALIZER (start_record , oa_start_record), + STRUCT_INITIALIZER (end_record , oa_end_record), STRUCT_INITIALIZER (start_vector , oa_start_vector), + STRUCT_INITIALIZER (end_vector , oa_end_vector), STRUCT_INITIALIZER (serialize_Bool , oa_serialize_bool), + STRUCT_INITIALIZER (serialize_Int , oa_serialize_int), + STRUCT_INITIALIZER (serialize_Long , oa_serialize_long) , + STRUCT_INITIALIZER (serialize_Buffer , oa_serialize_buffer), + STRUCT_INITIALIZER (serialize_String , 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/src/c/src/st_adaptor.c b/src/c/src/st_adaptor.c new file mode 100644 index 0000000..23573b3 --- /dev/null +++ b/src/c/src/st_adaptor.c @@ -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. + */ + +#ifndef DLL_EXPORT +# 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; +} +struct sync_completion *alloc_sync_completion(void) +{ + return (struct sync_completion*)calloc(1, sizeof(struct sync_completion)); +} +int wait_sync_completion(struct sync_completion *sc) +{ + return 0; +} + +void free_sync_completion(struct sync_completion *sc) +{ + free(sc); +} + +void notify_sync_completion(struct sync_completion *sc) +{ +} + +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 enter_critical(zhandle_t* zh) +{ + return 0; +} + +int leave_critical(zhandle_t* zh) +{ + return 0; +} diff --git a/src/c/src/winport.c b/src/c/src/winport.c new file mode 100644 index 0000000..3592ea1 --- /dev/null +++ b/src/c/src/winport.c @@ -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. + */ + +#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(); +} + +#endif //WIN32 + + + diff --git a/src/c/src/winport.h b/src/c/src/winport.h new file mode 100644 index 0000000..da6028c --- /dev/null +++ b/src/c/src/winport.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. + */ + +/** + * 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(); +#endif //WIN32 + + + +#endif //WINPORT_H_ diff --git a/src/c/src/zk_adaptor.h b/src/c/src/zk_adaptor.h new file mode 100644 index 0000000..b990779 --- /dev/null +++ b/src/c/src/zk_adaptor.h @@ -0,0 +1,276 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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" + +/* 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 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 44 +/* connect request */ +struct connect_req { + int32_t protocolVersion; + int64_t lastZxidSeen; + int32_t timeOut; + int64_t sessionId; + int32_t passwd_len; + char passwd[16]; +}; + +/* 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]; +}; + +#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 +#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 { +#ifdef WIN32 + SOCKET fd; /* the descriptor used to talk to zookeeper */ +#else + int fd; /* the descriptor used to talk to zookeeper */ +#endif + char *hostname; /* the hostname of zookeeper */ + struct sockaddr_storage *addrs; /* the addresses that correspond to the hostname */ + int addrs_count; /* The number of addresses in the addrs array */ + watcher_fn watcher; /* the registered watcher */ + struct timeval last_recv; /* The time that the last message was received */ + struct timeval last_send; /* The time that the last message was sent */ + struct timeval last_ping; /* The time that the last PING was sent */ + struct timeval next_deadline; /* The time of the next deadline */ + int recv_timeout; /* The maximum amount of time that can go by without + receiving anything from the zookeeper server */ + buffer_list_t *input_buffer; /* the current buffer being read in */ + buffer_head_t to_process; /* The buffers that have been read and are ready to be processed. */ + buffer_head_t to_send; /* The packets queued to send */ + completion_head_t sent_requests; /* The outstanding requests */ + completion_head_t completions_to_process; /* completions that are ready to run */ + int connect_index; /* The index of the address to connect to */ + clientid_t client_id; + long long last_zxid; + int outstanding_sync; /* Number of outstanding synchronous requests */ + 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[40]; /* the true size of primer_storage */ + volatile int state; + void *context; + auth_list_head_t auth_h; /* authentication data list */ + /* 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; + + 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; +}; + + +int adaptor_init(zhandle_t *zh); +void adaptor_finish(zhandle_t *zh); +void adaptor_destroy(zhandle_t *zh); +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); +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); + +// 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/src/c/src/zk_hashtable.c b/src/c/src/zk_hashtable.c new file mode 100644 index 0000000..0efc5aa --- /dev/null +++ b/src/c/src/zk_hashtable.c @@ -0,0 +1,337 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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)); + } + } +} diff --git a/src/c/src/zk_hashtable.h b/src/c/src/zk_hashtable.h new file mode 100644 index 0000000..31109c1 --- /dev/null +++ b/src/c/src/zk_hashtable.h @@ -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. + */ + +#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; + +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); + 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); + +#ifdef __cplusplus +} +#endif + +#endif /*ZK_HASHTABLE_H_*/ diff --git a/src/c/src/zk_log.c b/src/c/src/zk_log.c new file mode 100644 index 0000000..6b4fdfa --- /dev/null +++ b/src/c/src/zk_log.c @@ -0,0 +1,180 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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* getLogStream(){ + 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(ZooLogLevel curLevel,int line,const char* funcName, + const char* message) +{ + static const char* dbgLevelStr[]={"ZOO_INVALID","ZOO_ERROR","ZOO_WARN", + "ZOO_INFO","ZOO_DEBUG"}; + static pid_t pid=0; +#ifdef WIN32 + char timebuf [TIME_NOW_BUF_SIZE]; +#endif + if(pid==0)pid=getpid(); +#ifndef THREADED + // pid_t is long on Solaris + fprintf(LOGSTREAM, "%s:%ld:%s@%s@%d: %s\n", time_now(get_time_buffer()),(long)pid, + dbgLevelStr[curLevel],funcName,line,message); +#else +#ifdef WIN32 + fprintf(LOGSTREAM, "%s:%d(0x%lx):%s@%s@%d: %s\n", time_now(timebuf),pid, + (unsigned long int)(pthread_self().thread_id), + dbgLevelStr[curLevel],funcName,line,message); +#else + fprintf(LOGSTREAM, "%s:%ld(0x%lx):%s@%s@%d: %s\n", time_now(get_time_buffer()),(long)pid, + (unsigned long int)pthread_self(), + dbgLevelStr[curLevel],funcName,line,message); +#endif +#endif + fflush(LOGSTREAM); +} + +const char* format_log_message(const char* format,...) +{ + va_list va; + char* buf=get_format_log_buffer(); + if(!buf) + return "format_log_message: Unable to allocate memory buffer"; + + va_start(va,format); + vsnprintf(buf, FORMAT_LOG_BUF_SIZE-1,format,va); + va_end(va); + return buf; +} + +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/src/c/src/zookeeper.c b/src/c/src/zookeeper.c new file mode 100644 index 0000000..c087864 --- /dev/null +++ b/src/c/src/zookeeper.c @@ -0,0 +1,3767 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 WIN32 +#define random rand /* replace POSIX random with Windows rand */ +#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_EPHEMERAL = 1 << 0; +const int ZOO_SEQUENCE = 1 << 1; + +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; +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 ASSOCIATING_STATE_DEF: + return "ZOO_ASSOCIATING_STATE"; + case CONNECTED_STATE_DEF: + return "ZOO_CONNECTED_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 + +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; + 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; +} 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); +static const char* format_current_endpoint_info(zhandle_t* zh); + +/* deserialize forward declarations */ +static void deserialize_response(int type, int xid, int failed, int rc, completion_list_t *cptr, struct iarchive *ia); +static int deserialize_multi(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 completion_list_t* create_completion_entry(int xid, int completion_type, + const void *dc, const void *data, watcher_registration_t* wo, + completion_head_t *clist); +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 __attribute__((unused)) void print_completion_queue(zhandle_t *zh); + +static void *SYNCHRONOUS_MARKER = (void*)&SYNCHRONOUS_MARKER; +static int isValidPath(const char* path, const int flags); + +#ifdef _WINDOWS +static int zookeeper_send(SOCKET s, const char* buf, int len) +#else +static ssize_t zookeeper_send(int s, const void* buf, size_t len) +#endif +{ +#ifdef __linux__ + return send(s, buf, len, MSG_NOSIGNAL); +#else + return send(s, buf, len, 0); +#endif +} + +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; +} + +/** + * 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 (zh->hostname != 0) { + free(zh->hostname); + zh->hostname = NULL; + } + if (zh->fd != -1) { + close(zh->fd); + zh->fd = -1; + zh->state = 0; + } + if (zh->addrs != 0) { + free(zh->addrs); + zh->addrs = NULL; + } + + if (zh->chroot != 0) { + free(zh->chroot); + zh->chroot = NULL; + } + + 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); +} + +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); +#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 + +/** + * fill in the addrs array of the zookeeper servers in the zhandle. after filling + * them in, we will permute them for load balancing. + */ +int getaddrs(zhandle_t *zh) +{ + char *hosts = strdup(zh->hostname); + char *host; + char *strtok_last; + struct sockaddr_storage *addr; + int i; + int rc; + int alen = 0; /* the allocated length of the addrs array */ + + zh->addrs_count = 0; + if (zh->addrs) { + free(zh->addrs); + zh->addrs = 0; + } + if (!hosts) { + LOG_ERROR(("out of memory")); + errno=ENOMEM; + return ZSYSTEMERROR; + } + zh->addrs = 0; + host=strtok_r(hosts, ",", &strtok_last); + while(host) { + char *port_spec = strrchr(host, ':'); + char *end_port_spec; + int port; + if (!port_spec) { + LOG_ERROR(("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(("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(("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 (zh->addrs_count == alen) { + alen += 16; + zh->addrs = realloc(zh->addrs, sizeof(*zh->addrs)*alen); + if (zh->addrs == 0) { + LOG_ERROR(("out of memory")); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + } + addr = &zh->addrs[zh->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(("skipping unknown address family %x for %s", + addr->ss_family, zh->hostname)); + } + } + 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(("Win32 message: %s\n", gai_strerror(rc))); +#else + LOG_ERROR(("getaddrinfo: %s\n", strerror(errno))); +#endif + rc=ZSYSTEMERROR; + goto fail; + } + } + + for (res = res0; res; res = res->ai_next) { + // Expand address list if needed + if (zh->addrs_count == alen) { + void *tmpaddr; + alen += 16; + tmpaddr = realloc(zh->addrs, sizeof(*zh->addrs)*alen); + if (tmpaddr == 0) { + LOG_ERROR(("out of memory")); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + zh->addrs=tmpaddr; + } + + // Copy addrinfo into address list + addr = &zh->addrs[zh->addrs_count]; + switch (res->ai_family) { + case AF_INET: +#if defined(AF_INET6) + case AF_INET6: +#endif + memcpy(addr, res->ai_addr, res->ai_addrlen); + ++zh->addrs_count; + break; + default: + LOG_WARN(("skipping unknown address family %x for %s", + res->ai_family, zh->hostname)); + break; + } + } + + freeaddrinfo(res0); + + host = strtok_r(0, ",", &strtok_last); + } +#endif + } + free(hosts); + + if(!disable_conn_permute){ + setup_random(); + /* Permute */ + for (i = zh->addrs_count - 1; i > 0; --i) { + long int j = random()%(i+1); + if (i != j) { + struct sockaddr_storage t = zh->addrs[i]; + zh->addrs[i] = zh->addrs[j]; + zh->addrs[j] = t; + } + } + } + return ZOK; +fail: + if (zh->addrs) { + free(zh->addrs); + zh->addrs=0; + } + if (hosts) { + free(hosts); + } + 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, addr, addr_len)==-1) { + return NULL; + } + return addr; +} + +static void log_env() { + 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(("Client environment:zookeeper.version=%s", PACKAGE_STRING)); + +#ifdef HAVE_GETHOSTNAME + gethostname(buf, sizeof(buf)); + LOG_INFO(("Client environment:host.name=%s", buf)); +#else + LOG_INFO(("Client environment:host.name=")); +#endif + +#ifdef HAVE_SYS_UTSNAME_H + uname(&utsname); + LOG_INFO(("Client environment:os.name=%s", utsname.sysname)); + LOG_INFO(("Client environment:os.arch=%s", utsname.release)); + LOG_INFO(("Client environment:os.version=%s", utsname.version)); +#else + LOG_INFO(("Client environment:os.name=")); + LOG_INFO(("Client environment:os.arch=")); + LOG_INFO(("Client environment:os.version=")); +#endif + +#ifdef HAVE_GETLOGIN + LOG_INFO(("Client environment:user.name=%s", getlogin())); +#else + LOG_INFO(("Client environment:user.name=")); +#endif + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R) + uid = getuid(); + if (!getpwuid_r(uid, &pw, buf, sizeof(buf), &pwp)) { + LOG_INFO(("Client environment:user.home=%s", pw.pw_dir)); + } else { + LOG_INFO(("Client environment:user.home=")); + } +#else + LOG_INFO(("Client environment:user.home=")); +#endif + +#ifdef HAVE_GETCWD + if (!getcwd(buf, sizeof(buf))) { + LOG_INFO(("Client environment:user.dir=")); + } else { + LOG_INFO(("Client environment:user.dir=%s", buf)); + } +#else + LOG_INFO(("Client environment:user.dir=")); +#endif +} + +/** + * Create a zookeeper handle associated with the given host and port. + */ +zhandle_t *zookeeper_init(const char *host, watcher_fn watcher, + int recv_timeout, const clientid_t *clientid, void *context, int flags) +{ + int errnosave = 0; + zhandle_t *zh = NULL; + char *index_chroot = NULL; + + log_env(); +#ifdef WIN32 + if (Win32WSAStartup()){ + LOG_ERROR(("Error initializing ws2_32.dll")); + return 0; + } +#endif + LOG_INFO(("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 = calloc(1, sizeof(*zh)); + if (!zh) { + return 0; + } + zh->fd = -1; + zh->state = NOTCONNECTED_STATE_DEF; + zh->context = context; + zh->recv_timeout = recv_timeout; + 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(getaddrs(zh)!=0) { + goto abort; + } + zh->connect_index = 0; + if (clientid) { + memcpy(&zh->client_id, clientid, sizeof(zh->client_id)); + } else { + memset(&zh->client_id, 0, sizeof(zh->client_id)); + } + 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(); + + if (adaptor_init(zh) == -1) { + goto abort; + } + + return zh; +abort: + errnosave=errno; + destroy(zh); + free(zh); + errno=errnosave; + return 0; +} + +/** + * 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(("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 + */ +#ifdef WIN32 +static int send_buffer(SOCKET fd, buffer_list_t *buff) +#else +static int send_buffer(int fd, buffer_list_t *buff) +#endif +{ + 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(fd, b + off, sizeof(nlen) - off); + if (rc == -1) { +#ifndef _WINDOWS + if (errno != EAGAIN) { +#else + if (WSAGetLastError() != WSAEWOULDBLOCK) { +#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(fd, buff->buffer + off, len - off); + if (rc == -1) { +#ifndef _WINDOWS + if (errno != EAGAIN) { +#else + if (WSAGetLastError() != WSAEWOULDBLOCK) { +#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 + */ +#ifdef WIN32 +static int recv_buffer(SOCKET fd, buffer_list_t *buff) +#else +static int recv_buffer(int fd, buffer_list_t *buff) +#endif +{ + int off = buff->curr_offset; + int rc = 0; + //fprintf(LOGSTREAM, "rc = %d, off = %d, line %d\n", rc, off, __LINE__); + + /* if buffer is less than 4, we are reading in the length */ + if (off < 4) { + char *buffer = (char*)&(buff->len); + rc = recv(fd, buffer+off, sizeof(int)-off, 0); + //fprintf(LOGSTREAM, "rc = %d, off = %d, line %d\n", rc, off, __LINE__); + switch(rc) { + case 0: + errno = EHOSTDOWN; + case -1: +#ifndef _WINDOWS + if (errno == EAGAIN) { +#else + if (WSAGetLastError() == WSAEWOULDBLOCK) { +#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 = recv(fd, buff->buffer+off, buff->len-off, 0); + switch(rc) { + case 0: + errno = EHOSTDOWN; + case -1: +#ifndef _WINDOWS + if (errno == EAGAIN) { +#else + if (WSAGetLastError() == WSAEWOULDBLOCK) { +#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) { + struct sync_completion + *sc = (struct sync_completion*)cptr->data; + sc->rc = reason; + notify_sync_completion(sc); + zh->outstanding_sync--; + destroy_completion_entry(cptr); + } 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); + } + } + } + if (zoo_lock_auth(zh) == 0) { + 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; + } +} + +static void handle_error(zhandle_t *zh,int rc) +{ + close(zh->fd); + if (is_unrecoverable(zh)) { + LOG_DEBUG(("Calling a watcher for a ZOO_SESSION_EVENT and the state=%s", + state2String(zh->state))); + PROCESS_SESSION_EVENT(zh, zh->state); + } else if (zh->state == ZOO_CONNECTED_STATE) { + LOG_DEBUG(("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); + zh->fd = -1; + zh->connect_index++; + if (!is_unrecoverable(zh)) { + zh->state = 0; + } + if (process_async(zh->outstanding_sync)) { + process_completions(zh); + } +} + +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(ZOO_LOG_LEVEL_ERROR,line,__func__, + format_log_message("Socket [%s] zk retcode=%d, errno=%d(%s): %s", + format_current_endpoint_info(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(("Authentication scheme %s failed. Connection closed.", + zh->auth_h.auth->scheme)); + } + else { + LOG_INFO(("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 = { STRUCT_INITIALIZER(xid , AUTH_XID), STRUCT_INITIALIZER(type , 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(("Sending all auth info request to %s", format_current_endpoint_info(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(("Sending auth info request to %s",format_current_endpoint_info(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 = { STRUCT_INITIALIZER(xid , SET_WATCHES_XID), STRUCT_INITIALIZER(type , ZOO_SETWATCHES_OP)}; + struct SetWatches req; + int rc; + + req.relativeZxid = zh->last_zxid; + 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); + + // 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(("Sending set watches request to %s",format_current_endpoint_info(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)); + + return 0; +} + + static int deserialize_prime_response(struct prime_struct *req, char* buffer){ + //this should be the order of deserialization + int offset = 0; + memcpy(&req->len, buffer + offset, sizeof(req->len)); + offset = offset + sizeof(req->len); + + req->len = ntohl(req->len); + memcpy(&req->protocolVersion, buffer + offset, sizeof(req->protocolVersion)); + offset = offset + sizeof(req->protocolVersion); + + req->protocolVersion = ntohl(req->protocolVersion); + memcpy(&req->timeOut, buffer + offset, sizeof(req->timeOut)); + offset = offset + sizeof(req->timeOut); + + req->timeOut = ntohl(req->timeOut); + memcpy(&req->sessionId, buffer + offset, sizeof(req->sessionId)); + offset = offset + sizeof(req->sessionId); + + req->sessionId = zoo_htonll(req->sessionId); + memcpy(&req->passwd_len, buffer + offset, sizeof(req->passwd_len)); + offset = offset + sizeof(req->passwd_len); + + req->passwd_len = ntohl(req->passwd_len); + memcpy(req->passwd, buffer + offset, sizeof(req->passwd)); + 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; + req.protocolVersion = 0; + req.sessionId = zh->client_id.client_id; + 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; + 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; + /* 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); + + int send_ping(zhandle_t* zh) + { + int rc; + struct oarchive *oa = create_buffer_oarchive(); + struct RequestHeader h = { STRUCT_INITIALIZER(xid ,PING_XID), STRUCT_INITIALIZER (type , ZOO_PING_OP) }; + + rc = serialize_RequestHeader(oa, "header", &h); + enter_critical(zh); + gettimeofday(&zh->last_ping, 0); + 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); +} + +#ifdef WIN32 +int zookeeper_interest(zhandle_t *zh, SOCKET *fd, int *interest, + struct timeval *tv) +{ + + ULONG nonblocking_flag = 1; +#else +int zookeeper_interest(zhandle_t *zh, int *fd, int *interest, + struct timeval *tv) +{ +#endif + struct timeval now; + if(zh==0 || fd==0 ||interest==0 || tv==0) + return ZBADARGUMENTS; + if (is_unrecoverable(zh)) + return ZINVALIDSTATE; + gettimeofday(&now, 0); + if(zh->next_deadline.tv_sec!=0 || zh->next_deadline.tv_usec!=0){ + int time_left = calculate_interval(&zh->next_deadline, &now); + if (time_left > 10) + LOG_WARN(("Exceeded deadline by %dms", time_left)); + } + api_prolog(zh); + *fd = zh->fd; + *interest = 0; + tv->tv_sec = 0; + tv->tv_usec = 0; + if (*fd == -1) { + if (zh->connect_index == zh->addrs_count) { + /* Wait a bit before trying again so that we don't spin */ + zh->connect_index = 0; + }else { + int rc; +#ifdef WIN32 + char enable_tcp_nodelay = 1; +#else + int enable_tcp_nodelay = 1; +#endif + int ssoresult; + + zh->fd = socket(zh->addrs[zh->connect_index].ss_family, SOCK_STREAM, 0); + if (zh->fd < 0) { + return api_epilog(zh,handle_socket_error_msg(zh,__LINE__, + ZSYSTEMERROR, "socket() call failed")); + } + ssoresult = setsockopt(zh->fd, IPPROTO_TCP, TCP_NODELAY, &enable_tcp_nodelay, sizeof(enable_tcp_nodelay)); + if (ssoresult != 0) { + LOG_WARN(("Unable to set TCP_NODELAY, operation latency may be effected")); + } +#ifdef WIN32 + ioctlsocket(zh->fd, FIONBIO, &nonblocking_flag); +#else + fcntl(zh->fd, F_SETFL, O_NONBLOCK|fcntl(zh->fd, F_GETFL, 0)); +#endif +#if defined(AF_INET6) + if (zh->addrs[zh->connect_index].ss_family == AF_INET6) { + rc = connect(zh->fd, (struct sockaddr*) &zh->addrs[zh->connect_index], sizeof(struct sockaddr_in6)); + } else { +#else + LOG_DEBUG(("[zk] connect()\n")); + { +#endif + rc = connect(zh->fd, (struct sockaddr*) &zh->addrs[zh->connect_index], sizeof(struct sockaddr_in)); +#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 + } + 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) + zh->state = ZOO_CONNECTING_STATE; + else + return api_epilog(zh,handle_socket_error_msg(zh,__LINE__, + ZCONNECTIONLOSS,"connect() call failed")); + } else { + if((rc=prime_connection(zh))!=0) + return api_epilog(zh,rc); + + LOG_INFO(("Initiated connection to server [%s]", + format_endpoint_info(&zh->addrs[zh->connect_index]))); + } + } + *fd = zh->fd; + *tv = get_timeval(zh->recv_timeout/3); + zh->last_recv = now; + zh->last_send = now; + zh->last_ping = now; + } + if (zh->fd != -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) { + // 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->addrs[zh->connect_index]), + -recv_to)); + + } + // We only allow 1/3 of our timeout time to expire before sending + // a PING + if (zh->state==ZOO_CONNECTED_STATE) { + send_to = zh->recv_timeout/3 - idle_send; + if (send_to <= 0) { + if (zh->sent_requests.head==0) { +// LOG_DEBUG(("Sending PING to %s (exceeded idle by %dms)", +// format_current_endpoint_info(zh),-send_to)); + int rc=send_ping(zh); + if (rc < 0){ + LOG_ERROR(("failed to send PING request (zk retcode=%d)",rc)); + return api_epilog(zh,rc); + } + } + send_to = zh->recv_timeout/3; + } + } + // choose the lesser value as the timeout + *tv = get_timeval(recv_to < send_to? 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 && (zh->state == ZOO_CONNECTED_STATE)) + || zh->state == ZOO_CONNECTING_STATE) { + *interest |= ZOOKEEPER_WRITE; + } + } + return api_epilog(zh,ZOK); +} + +static int check_events(zhandle_t *zh, int events) +{ + if (zh->fd == -1) + return ZINVALIDSTATE; + if ((events&ZOOKEEPER_WRITE)&&(zh->state == ZOO_CONNECTING_STATE)) { + int rc, error; + socklen_t len = sizeof(error); + rc = getsockopt(zh->fd, 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(("initiated connection to server [%s]", + format_endpoint_info(&zh->addrs[zh->connect_index]))); + 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->fd, zh->input_buffer); + if (rc < 0) { + return handle_socket_error_msg(zh, __LINE__,ZCONNECTIONLOSS, + "failed while receiving a server response"); + } + if (rc > 0) { + gettimeofday(&zh->last_recv, 0); + 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->client_id.client_id; + 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 = ZOO_CONNECTED_STATE; + LOG_INFO(("session establishment complete on server [%s], sessionId=%#llx, negotiated timeout=%d", + format_endpoint_info(&zh->addrs[zh->connect_index]), + newid, zh->recv_timeout)); + /* 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(("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, ZOO_CONNECTED_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; +} + +static __attribute__((unused)) void print_completion_queue(zhandle_t *zh) +{ + completion_list_t* cptr; + + if(logLevelsent_requests.head==0) { + fprintf(LOGSTREAM,"empty\n"); + return; + } + + cptr=zh->sent_requests.head; + while(cptr){ + fprintf(LOGSTREAM,"%d,",cptr->xid); + cptr=cptr->next; + } + fprintf(LOGSTREAM,"end\n"); +} + +//#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(("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(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); + cptr->c.watcher_result = collectWatchers(zh, ZOO_SESSION_EVENT, ""); + 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; +} + +static void process_sync_completion( + completion_list_t *cptr, + struct sync_completion *sc, + struct iarchive *ia, + zhandle_t *zh) +{ + LOG_DEBUG(("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_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(cptr->xid, cptr, ia); + break; + default: + LOG_DEBUG(("Unsupported completion type=%d", cptr->c.type)); + break; + } +} + +static int deserialize_multi(int xid, completion_list_t *cptr, struct iarchive *ia) +{ + int rc = 0; + completion_head_t *clist = &cptr->c.clist; + struct MultiHeader mhdr = { STRUCT_INITIALIZER(type , 0), STRUCT_INITIALIZER(done , 0), STRUCT_INITIALIZER(err , 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(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(int type, int xid, int failed, int rc, completion_list_t *cptr, struct iarchive *ia) +{ + switch (type) { + case COMPLETION_DATA: + LOG_DEBUG(("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(("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(("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(("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(("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; + memset(&res, 0, sizeof(res)); + deserialize_CreateResponse(ia, "reply", &res); + cptr->c.string_result(rc, res.path, cptr->data); + deallocate_CreateResponse(&res); + } + break; + case COMPLETION_ACLLIST: + LOG_DEBUG(("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(("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(("Calling COMPLETION_MULTI for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc)); + rc = deserialize_multi(xid, cptr, ia); + assert(cptr->c.void_result); + cptr->c.void_result(rc, cptr->data); + break; + default: + LOG_DEBUG(("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(("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(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; + 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{ + gettimeofday(&zh->socket_readable,0); + } +} + +static void checkResponseLatency(zhandle_t* zh) +{ + int delay; + struct timeval now; + + if(zh->socket_readable.tv_sec==0) + return; + + gettimeofday(&now,0); + delay=calculate_interval(&zh->socket_readable, &now); + if(delay>20) + LOG_DEBUG(("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.zxid > 0) { + zh->last_zxid = hdr.zxid; + } else { + // fprintf(stderr, "Got %#x for %#x\n", hdr.zxid, hdr.xid); + } + + 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(("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(("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(WATCHER_EVENT_XID,-1,0,0,0,0); + c->buffer = bptr; + c->c.watcher_result = collectWatchers(zh, type, path); + + // 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(("Processing SET_WATCHES")); + free_buffer(bptr); + } else if (hdr.xid == AUTH_XID){ + LOG_DEBUG(("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(("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(("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)); + } + + activateWatcher(zh, cptr->watcher, rc); + + if (cptr->c.void_result != SYNCHRONOUS_MARKER) { + LOG_DEBUG(("Queueing asynchronous response")); + cptr->buffer = bptr; + queue_completion(&zh->completions_to_process, cptr, 0); + } else { + struct sync_completion + *sc = (struct sync_completion*)cptr->data; + sc->rc = rc; + + process_sync_completion(cptr, sc, ia, zh); + + notify_sync_completion(sc); + free_buffer(bptr); + zh->outstanding_sync--; + destroy_completion_entry(cptr); + } + } + + 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 void destroy_watcher_registration(watcher_registration_t* wo){ + if(wo!=0){ + free((void*)wo->path); + free(wo); + } +} + +static completion_list_t* create_completion_entry(int xid, int completion_type, + const void *dc, const void *data,watcher_registration_t* wo, completion_head_t *clist) +{ + completion_list_t *c = calloc(1,sizeof(completion_list_t)); + if (!c) { + LOG_ERROR(("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_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; + + return c; +} + +static void destroy_completion_entry(completion_list_t* c){ + if(c!=0){ + destroy_watcher_registration(c->watcher); + 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(xid, completion_type, dc, + data, wo, clist); + 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_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); +} + +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(zh->state==ZOO_CONNECTED_STATE){ + struct oarchive *oa; + struct RequestHeader h = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type , ZOO_CLOSE_OP)}; + LOG_INFO(("Closing zookeeper sessionId=%#llx to [%s]\n", + zh->client_id.client_id,format_current_endpoint_info(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) { + rc = ZMARSHALLINGERROR; + goto finish; + } + + /* 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); + }else{ + LOG_INFO(("Freeing zookeeper resources for sessionId=%#llx\n", + zh->client_id.client_id)); + rc = ZOK; + } + +finish: + destroy(zh); + adaptor_destroy(zh); + free(zh); +#ifdef WIN32 + Win32WSACleanup(); +#endif + return rc; +} + +static int isValidPath(const char* path, const int flags) { + 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] == '/' && !(flags & ZOO_SEQUENCE)) + 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) && !(flags & ZOO_SEQUENCE)) + || path[i+1] == '/')) { + return 0; + } + } else if (c == '.') { + if ((path[i-1] == '/') && (((i + 1 == len) && !(flags & ZOO_SEQUENCE)) + || 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 flags, + char **path_out, const char *path) +{ + assert(path_out); + + *path_out = prepend_string(zh, path); + if (zh == NULL || !isValidPath(*path_out, flags)) { + 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 flags, + char **path_out, const char *path, + int32_t *watch_out, uint32_t watch) +{ + int rc = Request_path_init(zh, flags, 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 = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type ,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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER(xid , get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 flags) +{ + int rc; + assert(req); + rc = Request_path_init(zh, flags, &req->path, path); + assert(req); + if (rc != ZOK) { + return rc; + } + req->flags = flags; + 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; +} + +int zoo_acreate(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int flags, + string_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type ,ZOO_CREATE_OP) }; + struct CreateRequest req; + + int rc = CreateRequest_init(zh, &req, + path, value, valuelen, acl_entries, flags); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_CreateRequest(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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER (xid ,get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER( xid, get_xid()), STRUCT_INITIALIZER (type ,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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER (xid , get_xid()), STRUCT_INITIALIZER(type ,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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER(xid ,get_xid()), STRUCT_INITIALIZER (type , 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(("Sending request xid=%#x for path [%s] to %s",h.xid,path, + format_current_endpoint_info(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 = { STRUCT_INITIALIZER(xid, get_xid()), STRUCT_INITIALIZER(type, ZOO_MULTI_OP) }; + struct MultiHeader mh = { STRUCT_INITIALIZER(type, -1), STRUCT_INITIALIZER(done, 1), STRUCT_INITIALIZER(err, -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 = { STRUCT_INITIALIZER(type, op->type), STRUCT_INITIALIZER(done, 0), STRUCT_INITIALIZER(err, -1) }; + rc = rc < 0 ? rc : serialize_MultiHeader(oa, "multiheader", &mh); + + switch(op->type) { + 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(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(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(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(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(("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(("Sending multi request xid=%#x with %d subrequests to %s", + h.xid, index, format_current_endpoint_info(zh))); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + + return (rc < 0) ? ZMARSHALLINGERROR : ZOK; +} + +void zoo_create_op_init(zoo_op_t *op, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int flags, + char *path_buffer, int path_buffer_len) +{ + assert(op); + op->type = 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 = flags; + 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; +} + +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; +} + +/* 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 + gettimeofday(&started,0); + // 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&& zh->state == ZOO_CONNECTED_STATE) { + if(timeout!=0){ + int elapsed; + struct timeval now; + gettimeofday(&now,0); + 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, &pollSet); + // Poll the socket + rc = select((int)(zh->fd)+1, NULL, &pollSet, NULL, &wait); +#else + struct pollfd fds; + fds.fd = zh->fd; + 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->fd, 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); + gettimeofday(&zh->last_send, 0); + 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 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"; + } + 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(zh->state == ZOO_CONNECTED_STATE || 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[128]; + char addrstr[128]; + void *inaddr; +#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; + } else { +#endif + inaddr=&((struct sockaddr_in*)ep)->sin_addr; + port=((struct sockaddr_in*)ep)->sin_port; +#if defined(AF_INET6) + } +#endif +#ifdef WIN32 + addrstring = inet_ntoa (*(struct in_addr*)inaddr); + sprintf(buf,"%s:%d",addrstring,ntohs(port)); +#else + inet_ntop(ep->ss_family,inaddr,addrstr,sizeof(addrstr)-1); + sprintf(buf,"%s:%d",addrstr,ntohs(port)); +#endif + return buf; +} + +static const char* format_current_endpoint_info(zhandle_t* zh) +{ + return format_endpoint_info(&zh->addrs[zh->connect_index]); +} + +void zoo_deterministic_conn_order(int yesOrNo) +{ + disable_conn_permute=yesOrNo; +} + +/*---------------------------------------------------------------------------* + * SYNC API + *---------------------------------------------------------------------------*/ +int zoo_create(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int flags, + 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(zh, path, value, valuelen, acl, flags, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + } + 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_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; +} diff --git a/src/c/tests/CollectionUtil.h b/src/c/tests/CollectionUtil.h new file mode 100644 index 0000000..dd34811 --- /dev/null +++ b/src/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/src/c/tests/CppAssertHelper.h b/src/c/tests/CppAssertHelper.h new file mode 100644 index 0000000..3926f51 --- /dev/null +++ b/src/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/src/c/tests/LibCMocks.cc b/src/c/tests/LibCMocks.cc new file mode 100644 index 0000000..5db4553 --- /dev/null +++ b/src/c/tests/LibCMocks.cc @@ -0,0 +1,333 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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" + +#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){ + if (!Mock_socket::mock_) + 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; + diff --git a/src/c/tests/LibCMocks.h b/src/c/tests/LibCMocks.h new file mode 100644 index 0000000..5b07cda --- /dev/null +++ b/src/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/src/c/tests/LibCSymTable.cc b/src/c/tests/LibCSymTable.cc new file mode 100644 index 0000000..5378579 --- /dev/null +++ b/src/c/tests/LibCSymTable.cc @@ -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. + */ + +#include "LibCSymTable.h" + +#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 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/src/c/tests/LibCSymTable.h b/src/c/tests/LibCSymTable.h new file mode 100644 index 0000000..2f7e0c2 --- /dev/null +++ b/src/c/tests/LibCSymTable.h @@ -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. + */ + +#ifndef LIBCSYMTABLE_H_ +#define LIBCSYMTABLE_H_ + +#include +#include +#include +#include +#include +#include +#include + +#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 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/src/c/tests/MocksBase.cc b/src/c/tests/MocksBase.cc new file mode 100644 index 0000000..60b2c75 --- /dev/null +++ b/src/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/src/c/tests/MocksBase.h b/src/c/tests/MocksBase.h new file mode 100644 index 0000000..5b54251 --- /dev/null +++ b/src/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/src/c/tests/PthreadMocks.cc b/src/c/tests/PthreadMocks.cc new file mode 100644 index 0000000..490cebf --- /dev/null +++ b/src/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/src/c/tests/PthreadMocks.h b/src/c/tests/PthreadMocks.h new file mode 100644 index 0000000..8db8815 --- /dev/null +++ b/src/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/src/c/tests/TestClient.cc b/src/c/tests/TestClient.cc new file mode 100644 index 0000000..154fbed --- /dev/null +++ b/src/c/tests/TestClient.cc @@ -0,0 +1,1135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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" + +struct buff_struct_2 { + int32_t len; + int32_t off; + char *buffer; +}; + +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(testAsyncWatcherAutoReset); + CPPUNIT_TEST(testDeserializeString); + CPPUNIT_TEST(testFirstServerDown); +#ifdef THREADED + CPPUNIT_TEST(testNullData); +#ifdef ZOO_IPV6_ENABLED + CPPUNIT_TEST(testIPV6); +#endif + 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); +#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(const char *hp, watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + + 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)); + } + + /** 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 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 create_completion_fn(int rc, const char* value, const void *data) { + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + 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)); + + // [ZOOKEEPER-800] zoo_add_auth should return ZINVALIDSTATE if + // the connection is closed. + zhandle_t *zk2 = zookeeper_init(hostPorts, NULL, 10000, 0, NULL, 0); + sleep(1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, zookeeper_close(zk2)); + CPPUNIT_ASSERT_EQUAL(0, zoo_state(zk2)); // 0 ==> ZOO_CLOSED_STATE + rc = zoo_add_auth(zk2, "digest", "pat:passwd", 10, voidCompletion, (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZINVALIDSTATE, rc); + + 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 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); + } + + 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)); + } + + 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/2; 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, 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); + } + } +}; + +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); diff --git a/src/c/tests/TestClientRetry.cc b/src/c/tests/TestClientRetry.cc new file mode 100644 index 0000000..41d5179 --- /dev/null +++ b/src/c/tests/TestClientRetry.cc @@ -0,0 +1,273 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 "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; + 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; +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_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, "export 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/src/c/tests/TestDriver.cc b/src/c/tests/TestDriver.cc new file mode 100644 index 0000000..d60db69 --- /dev/null +++ b/src/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 +#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; + 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_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. +#ifdef THREADED + 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); +#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(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 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-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); diff --git a/src/c/tests/TestOperations.cc b/src/c/tests/TestOperations.cc new file mode 100644 index 0000000..27d9270 --- /dev/null +++ b/src/c/tests/TestOperations.cc @@ -0,0 +1,710 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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); +#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_; + }; +#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)); + } + +#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 "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/src/c/tests/TestZookeeperClose.cc b/src/c/tests/TestZookeeperClose.cc new file mode 100644 index 0000000..edefa66 --- /dev/null +++ b/src/c/tests/TestZookeeperClose.cc @@ -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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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)); + // 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=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)); + // 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); + + 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)); + // 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)); + // 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)); + 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)); + 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)); + 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)); + 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/src/c/tests/TestZookeeperInit.cc b/src/c/tests/TestZookeeperInit.cc new file mode 100644 index 0000000..eadf41d --- /dev/null +++ b/src/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 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!=0); + CPPUNIT_ASSERT(zh->fd == -1); + CPPUNIT_ASSERT(zh->hostname!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs_count); + CPPUNIT_ASSERT_EQUAL(EXPECTED_HOST,string(zh->hostname)); + CPPUNIT_ASSERT(zh->state == NOTCONNECTED_STATE_DEF); + CPPUNIT_ASSERT(zh->context == (void*)1); + CPPUNIT_ASSERT_EQUAL(EXPECTED_RECV_TIMEOUT,zh->recv_timeout); + CPPUNIT_ASSERT(zh->watcher == watcher); + CPPUNIT_ASSERT(zh->connect_index==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 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(int i=0;iaddrs_count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[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 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(int i=0;iaddrs_count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[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 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(int i=0;iaddrs_count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[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 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(int i=0;iaddrs_count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs[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/src/c/tests/ThreadingUtil.cc b/src/c/tests/ThreadingUtil.cc new file mode 100644 index 0000000..3b5170a --- /dev/null +++ b/src/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/src/c/tests/ThreadingUtil.h b/src/c/tests/ThreadingUtil.h new file mode 100644 index 0000000..9165412 --- /dev/null +++ b/src/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/src/c/tests/Util.cc b/src/c/tests/Util.cc new file mode 100644 index 0000000..2b9da84 --- /dev/null +++ b/src/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/src/c/tests/Util.h b/src/c/tests/Util.h new file mode 100644 index 0000000..01e21ce --- /dev/null +++ b/src/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(ZOO_LOG_LEVEL_DEBUG,__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); +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/src/c/tests/ZKMocks.cc b/src/c/tests/ZKMocks.cc new file mode 100644 index 0000000..69bea16 --- /dev/null +++ b/src/c/tests/ZKMocks.cc @@ -0,0 +1,519 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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){ + auto_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)); + 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)); + // 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 + auto_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; + zh->fd=ZookeeperServer::FD; + 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/src/c/tests/ZKMocks.h b/src/c/tests/ZKMocks.h new file mode 100644 index 0000000..fbcfc4f --- /dev/null +++ b/src/c/tests/ZKMocks.h @@ -0,0 +1,509 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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)) + { + memcpy(passwd,"1234567890123456",sizeof(passwd)); + } + int32_t protocolVersion; + int32_t timeOut; + int64_t sessionId; + int32_t passwd_len; + char passwd[16]; + 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 /dev/null +if [ $? -eq 0 ] +then + pid=`lsof -i :$ZOOPORT | grep LISTEN | awk '{print $2}'` + if [ -n "$pid" ] + then + $KILL -9 $pid + fi +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}"/src/java/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +CLASSPATH="$CLASSPATH:${CLOVER_HOME}/lib/clover.jar" + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +case $1 in +start|startClean) + if [ "x${base_dir}" == "x" ] + then + mkdir -p /tmp/zkdata + java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT /tmp/zkdata 3000 $ZKMAXCNXNS &> /tmp/zk.log & + pid=$! + echo -n $! > /tmp/zk.pid + else + mkdir -p "${base_dir}/build/tmp/zkdata" + java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT "${base_dir}/build/tmp/zkdata" 3000 $ZKMAXCNXNS &> "${base_dir}/build/tmp/zk.log" & + pid=$! + echo -n $pid > "${base_dir}/build/tmp/zk.pid" + fi + + # 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" 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 + + 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/src/contrib/build-contrib.xml b/src/contrib/build-contrib.xml new file mode 100644 index 0000000..276516e --- /dev/null +++ b/src/contrib/build-contrib.xml @@ -0,0 +1,248 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/build.xml b/src/contrib/build.xml new file mode 100644 index 0000000..7f7ba4f --- /dev/null +++ b/src/contrib/build.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/fatjar/README.txt b/src/contrib/fatjar/README.txt new file mode 100644 index 0000000..f8027ae --- /dev/null +++ b/src/contrib/fatjar/README.txt @@ -0,0 +1,2 @@ +This package contains build to create a fat zookeeper jar. You need to run ant to create the fat jar. +To run the fatjar you can use. java -jar zoookeeper-*fatjar.jar diff --git a/src/contrib/fatjar/build.xml b/src/contrib/fatjar/build.xml new file mode 100644 index 0000000..8935f39 --- /dev/null +++ b/src/contrib/fatjar/build.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/fatjar/conf/mainClasses b/src/contrib/fatjar/conf/mainClasses new file mode 100644 index 0000000..2b0fc83 --- /dev/null +++ b/src/contrib/fatjar/conf/mainClasses @@ -0,0 +1,10 @@ +::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 +quorumBench:org.apache.zookeeper.server.QuorumBenchmark:A benchmark of just the quorum protocol +abBench:org.apache.zookeeper.server.quorum.AtomicBroadcastBenchmark:A benchmark of just the atomic broadcast +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 diff --git a/src/contrib/fatjar/src/java/org/apache/zookeeper/util/FatJarMain.java b/src/contrib/fatjar/src/java/org/apache/zookeeper/util/FatJarMain.java new file mode 100644 index 0000000..bdf0eae --- /dev/null +++ b/src/contrib/fatjar/src/java/org/apache/zookeeper/util/FatJarMain.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.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; + +/** + * 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 HashMap cmds = new HashMap(); + static ArrayList 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/src/contrib/huebrowser/README b/src/contrib/huebrowser/README new file mode 100644 index 0000000..c03ea90 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/Makefile b/src/contrib/huebrowser/zkui/Makefile new file mode 100644 index 0000000..9c22d1c --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/setup.py b/src/contrib/huebrowser/zkui/setup.py new file mode 100644 index 0000000..68d1352 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/__init__.py b/src/contrib/huebrowser/zkui/src/zkui/__init__.py new file mode 100644 index 0000000..eccc881 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/forms.py b/src/contrib/huebrowser/zkui/src/zkui/forms.py new file mode 100644 index 0000000..6b1f178 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/models.py b/src/contrib/huebrowser/zkui/src/zkui/models.py new file mode 100644 index 0000000..a46696b --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/rest.py b/src/contrib/huebrowser/zkui/src/zkui/rest.py new file mode 100644 index 0000000..e4874a1 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/settings.py b/src/contrib/huebrowser/zkui/src/zkui/settings.py new file mode 100644 index 0000000..844c695 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/static/art/line_icons.png b/src/contrib/huebrowser/zkui/src/zkui/static/art/line_icons.png new file mode 100644 index 0000000..1da4a29 Binary files /dev/null and b/src/contrib/huebrowser/zkui/src/zkui/static/art/line_icons.png differ diff --git a/src/contrib/huebrowser/zkui/src/zkui/static/art/zkui.png b/src/contrib/huebrowser/zkui/src/zkui/static/art/zkui.png new file mode 100644 index 0000000..cb40df3 Binary files /dev/null and b/src/contrib/huebrowser/zkui/src/zkui/static/art/zkui.png differ diff --git a/src/contrib/huebrowser/zkui/src/zkui/static/bootstrap.js b/src/contrib/huebrowser/zkui/src/zkui/static/bootstrap.js new file mode 100644 index 0000000..8e3fbfb --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/static/css/zkui.css b/src/contrib/huebrowser/zkui/src/zkui/static/css/zkui.css new file mode 100644 index 0000000..c49f392 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/static/help/index.html b/src/contrib/huebrowser/zkui/src/zkui/static/help/index.html new file mode 100644 index 0000000..355c8cd --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/static/js/Source/Zkui/Zkui.js b/src/contrib/huebrowser/zkui/src/zkui/static/js/Source/Zkui/Zkui.js new file mode 100644 index 0000000..c8bf383 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/static/js/package.yml b/src/contrib/huebrowser/zkui/src/zkui/static/js/package.yml new file mode 100644 index 0000000..c2c07ad --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/stats.py b/src/contrib/huebrowser/zkui/src/zkui/stats.py new file mode 100644 index 0000000..48f35dd --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/clients.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/clients.mako new file mode 100644 index 0000000..2bee9a7 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/create.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/create.mako new file mode 100644 index 0000000..2a8b8cc --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/edit.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/edit.mako new file mode 100644 index 0000000..997bd07 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/index.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/index.mako new file mode 100644 index 0000000..567919d --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/shared_components.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/shared_components.mako new file mode 100644 index 0000000..f9a4589 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/tree.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/tree.mako new file mode 100644 index 0000000..c74c202 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/templates/view.mako b/src/contrib/huebrowser/zkui/src/zkui/templates/view.mako new file mode 100644 index 0000000..e046afc --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/urls.py b/src/contrib/huebrowser/zkui/src/zkui/urls.py new file mode 100644 index 0000000..f795f7e --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/utils.py b/src/contrib/huebrowser/zkui/src/zkui/utils.py new file mode 100644 index 0000000..fb01317 --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/views.py b/src/contrib/huebrowser/zkui/src/zkui/views.py new file mode 100644 index 0000000..64d926b --- /dev/null +++ b/src/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/src/contrib/huebrowser/zkui/src/zkui/windmilltests.py b/src/contrib/huebrowser/zkui/src/zkui/windmilltests.py new file mode 100644 index 0000000..ba44e26 --- /dev/null +++ b/src/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/src/contrib/loggraph/README.txt b/src/contrib/loggraph/README.txt new file mode 100644 index 0000000..1865fa5 --- /dev/null +++ b/src/contrib/loggraph/README.txt @@ -0,0 +1,69 @@ +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 src/contrib/loggraph/bin. 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/src/contrib/loggraph/bin/loggraph-dev.sh b/src/contrib/loggraph/bin/loggraph-dev.sh new file mode 100644 index 0000000..0b82efa --- /dev/null +++ b/src/contrib/loggraph/bin/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/src/contrib/loggraph/bin/loggraph.sh b/src/contrib/loggraph/bin/loggraph.sh new file mode 100644 index 0000000..381e5ad --- /dev/null +++ b/src/contrib/loggraph/bin/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/src/contrib/loggraph/build.xml b/src/contrib/loggraph/build.xml new file mode 100644 index 0000000..5be6970 --- /dev/null +++ b/src/contrib/loggraph/build.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/loggraph/ivy.xml b/src/contrib/loggraph/ivy.xml new file mode 100644 index 0000000..d6fa9d6 --- /dev/null +++ b/src/contrib/loggraph/ivy.xml @@ -0,0 +1,44 @@ + + + + + + + + ZooKeeper Graphing + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterException.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterException.java new file mode 100644 index 0000000..c0912fa --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterOp.java new file mode 100644 index 0000000..ee73283 --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterOp.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.graph; + +import java.util.ArrayList; +import org.apache.zookeeper.graph.filterops.*; + +public abstract class FilterOp { + protected ArrayList subOps; + protected ArrayList 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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterParser.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/FilterParser.java new file mode 100644 index 0000000..cf12e3a --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/JsonGenerator.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/JsonGenerator.java new file mode 100644 index 0000000..afaf3a1 --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/JsonGenerator.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.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; + +public class JsonGenerator { + private JSONObject root; + private HashSet 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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/Log4JEntry.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/Log4JEntry.java new file mode 100644 index 0000000..0edc146 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/Log4JSource.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/Log4JSource.java new file mode 100644 index 0000000..84a9d98 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogEntry.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogEntry.java new file mode 100644 index 0000000..a8252eb --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogEntry.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.graph; + +import java.io.Serializable; +import java.util.HashMap; + +public abstract class LogEntry implements Serializable { + private HashMap 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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogIterator.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogIterator.java new file mode 100644 index 0000000..9af440b --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogServer.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogServer.java new file mode 100644 index 0000000..5cffcdd --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogSkipList.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogSkipList.java new file mode 100644 index 0000000..e744442 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogSource.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/LogSource.java new file mode 100644 index 0000000..9845c7f --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/MeasureThroughput.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/MeasureThroughput.java new file mode 100644 index 0000000..1c83da7 --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/MeasureThroughput.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.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; + +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; + HashSet 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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/MergedLogSource.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/MergedLogSource.java new file mode 100644 index 0000000..bb789d3 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/RandomAccessFileReader.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/RandomAccessFileReader.java new file mode 100644 index 0000000..13a41a5 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/TransactionEntry.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/TransactionEntry.java new file mode 100644 index 0000000..33c7189 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/TxnLogSource.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/TxnLogSource.java new file mode 100644 index 0000000..809c455 --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/TxnLogSource.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.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.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()); + } + TxnHeader hdr = new TxnHeader(); + Record r = SerializeUtils.deserializeTxn(bytes, hdr); + + 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."); + } + TxnHeader hdr = new TxnHeader(); + Record r = SerializeUtils.deserializeTxn(bytes, hdr); + + 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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/AndOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/AndOp.java new file mode 100644 index 0000000..581bdaa --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/Arg.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/Arg.java new file mode 100644 index 0000000..4fda3cf --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/EqualsOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/EqualsOp.java new file mode 100644 index 0000000..409815a --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/GreaterThanOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/GreaterThanOp.java new file mode 100644 index 0000000..244dd3d --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/LessThanOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/LessThanOp.java new file mode 100644 index 0000000..b7d9e09 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/NotOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/NotOp.java new file mode 100644 index 0000000..d8ed757 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/NumberArg.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/NumberArg.java new file mode 100644 index 0000000..d6b584d --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/OrOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/OrOp.java new file mode 100644 index 0000000..d681589 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/StringArg.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/StringArg.java new file mode 100644 index 0000000..7345d3c --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/SymbolArg.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/SymbolArg.java new file mode 100644 index 0000000..077553b --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/XorOp.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/filterops/XorOp.java new file mode 100644 index 0000000..9e778b1 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/FileLoader.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/FileLoader.java new file mode 100644 index 0000000..67e8945 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/Fs.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/Fs.java new file mode 100644 index 0000000..e5b1a01 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/GraphData.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/GraphData.java new file mode 100644 index 0000000..fc10eb1 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/JsonServlet.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/JsonServlet.java new file mode 100644 index 0000000..910d44f --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/NumEvents.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/NumEvents.java new file mode 100644 index 0000000..5961a12 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/StaticContent.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/StaticContent.java new file mode 100644 index 0000000..d91acb6 --- /dev/null +++ b/src/contrib/loggraph/src/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/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/Throughput.java b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/Throughput.java new file mode 100644 index 0000000..341bf9e --- /dev/null +++ b/src/contrib/loggraph/src/java/org/apache/zookeeper/graph/servlets/Throughput.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.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 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; + HashSet 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/src/contrib/loggraph/web/org/apache/zookeeper/graph/log4j.properties b/src/contrib/loggraph/web/org/apache/zookeeper/graph/log4j.properties new file mode 100644 index 0000000..ab8960b --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/date.format.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/date.format.js new file mode 100644 index 0000000..5515009 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.bar.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.bar.js new file mode 100644 index 0000000..2f7212a --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.dot.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.dot.js new file mode 100644 index 0000000..2821e62 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.line.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.line.js new file mode 100644 index 0000000..eb56e59 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.pie.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.pie.js new file mode 100644 index 0000000..8d20374 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.raphael.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/g.raphael.js new file mode 100644 index 0000000..8e94c36 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load-big.gif b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load-big.gif new file mode 100644 index 0000000..ddb7ff1 Binary files /dev/null and b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load-big.gif differ diff --git a/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load.gif b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load.gif new file mode 100644 index 0000000..d0bce15 Binary files /dev/null and b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/load.gif differ diff --git a/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.css b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.css new file mode 100644 index 0000000..a84d90e --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.js new file mode 100644 index 0000000..87bb7d8 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.log.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.log.js new file mode 100644 index 0000000..551ea4b --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.server.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.server.js new file mode 100644 index 0000000..0a74b5c --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.session.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.session.js new file mode 100644 index 0000000..5a314d8 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.stats.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.stats.js new file mode 100644 index 0000000..0a8ac4f --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.ui.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/loggraph.ui.js new file mode 100644 index 0000000..819765a --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/main.html b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/main.html new file mode 100644 index 0000000..b9affe6 --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/raphael.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/raphael.js new file mode 100644 index 0000000..3740d0f --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/yui-min.js b/src/contrib/loggraph/web/org/apache/zookeeper/graph/resources/yui-min.js new file mode 100644 index 0000000..55f017d --- /dev/null +++ b/src/contrib/loggraph/web/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/src/contrib/monitoring/JMX-RESOURCES b/src/contrib/monitoring/JMX-RESOURCES new file mode 100644 index 0000000..1d1aa98 --- /dev/null +++ b/src/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/src/contrib/monitoring/README b/src/contrib/monitoring/README new file mode 100644 index 0000000..a2206e8 --- /dev/null +++ b/src/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_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 +zk_fsync_threshold_exceed_count 0 + +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/src/contrib/monitoring/cacti/README b/src/contrib/monitoring/cacti/README new file mode 100644 index 0000000..8188723 --- /dev/null +++ b/src/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/src/contrib/monitoring/check_zookeeper.py b/src/contrib/monitoring/check_zookeeper.py new file mode 100644 index 0000000..2c25afb --- /dev/null +++ b/src/contrib/monitoring/check_zookeeper.py @@ -0,0 +1,395 @@ +#! /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('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 + + 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 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/src/contrib/monitoring/ganglia/README b/src/contrib/monitoring/ganglia/README new file mode 100644 index 0000000..578adfe --- /dev/null +++ b/src/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/src/contrib/monitoring/ganglia/Screenshot.png b/src/contrib/monitoring/ganglia/Screenshot.png new file mode 100644 index 0000000..bc0e41d Binary files /dev/null and b/src/contrib/monitoring/ganglia/Screenshot.png differ diff --git a/src/contrib/monitoring/ganglia/modpython.conf b/src/contrib/monitoring/ganglia/modpython.conf new file mode 100644 index 0000000..5cd051a --- /dev/null +++ b/src/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/src/contrib/monitoring/ganglia/zookeeper.pyconf b/src/contrib/monitoring/ganglia/zookeeper.pyconf new file mode 100644 index 0000000..029c146 --- /dev/null +++ b/src/contrib/monitoring/ganglia/zookeeper.pyconf @@ -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. */ + +/* 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_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_fsync_threshold_exceed_count" } +} + diff --git a/src/contrib/monitoring/ganglia/zookeeper_ganglia.py b/src/contrib/monitoring/ganglia/zookeeper_ganglia.py new file mode 100644 index 0000000..b20f824 --- /dev/null +++ b/src/contrib/monitoring/ganglia/zookeeper_ganglia.py @@ -0,0 +1,210 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT 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 + +from StringIO import StringIO + +TIME_BETWEEN_QUERIES = 20 + +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') + if data: + return self._parse(data) + else: + data = self._send_cmd('stat') + return self._parse_stat(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 """ + 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 + +def metric_handler(name): + if time.time() - metric_handler.timestamp > 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': 'packets', + 'slope': 'positive' + }, + 'zk_packets_sent': { + 'units': 'packets', + 'slope': 'positive' + }, + '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_fsync_threshold_exceed_count': {'units': 'fsyncexceed'} + } + 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'}) + for d in ds: + print "%s=%s" % (d['name'], metric_handler(d['name'])) + + diff --git a/src/contrib/monitoring/nagios/README.txt b/src/contrib/monitoring/nagios/README.txt new file mode 100644 index 0000000..317ae14 --- /dev/null +++ b/src/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/src/contrib/monitoring/nagios/Screenshot-1.png b/src/contrib/monitoring/nagios/Screenshot-1.png new file mode 100644 index 0000000..2dc55c5 Binary files /dev/null and b/src/contrib/monitoring/nagios/Screenshot-1.png differ diff --git a/src/contrib/monitoring/nagios/Screenshot.png b/src/contrib/monitoring/nagios/Screenshot.png new file mode 100644 index 0000000..3219fea Binary files /dev/null and b/src/contrib/monitoring/nagios/Screenshot.png differ diff --git a/src/contrib/monitoring/nagios/hostgroups.cfg b/src/contrib/monitoring/nagios/hostgroups.cfg new file mode 100644 index 0000000..ea59a66 --- /dev/null +++ b/src/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/src/contrib/monitoring/nagios/services.cfg b/src/contrib/monitoring/nagios/services.cfg new file mode 100644 index 0000000..dde6ab7 --- /dev/null +++ b/src/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/src/contrib/monitoring/nagios/zookeeper.cfg b/src/contrib/monitoring/nagios/zookeeper.cfg new file mode 100644 index 0000000..ed4cfab --- /dev/null +++ b/src/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/src/contrib/monitoring/test.py b/src/contrib/monitoring/test.py new file mode 100644 index 0000000..3941291 --- /dev/null +++ b/src/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/src/contrib/rest/NOTICE.txt b/src/contrib/rest/NOTICE.txt new file mode 100644 index 0000000..2a92254 --- /dev/null +++ b/src/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/src/contrib/rest/README.txt b/src/contrib/rest/README.txt new file mode 100644 index 0000000..30f2e5d --- /dev/null +++ b/src/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/src/contrib/rest/SPEC.txt b/src/contrib/rest/SPEC.txt new file mode 100644 index 0000000..8c5f701 --- /dev/null +++ b/src/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/src/contrib/rest/build.xml b/src/contrib/rest/build.xml new file mode 100644 index 0000000..fb628bc --- /dev/null +++ b/src/contrib/rest/build.xml @@ -0,0 +1,183 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/rest/conf/keys/README b/src/contrib/rest/conf/keys/README new file mode 100644 index 0000000..085810a --- /dev/null +++ b/src/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/src/contrib/rest/conf/keys/rest.cer b/src/contrib/rest/conf/keys/rest.cer new file mode 100644 index 0000000..13e5aab Binary files /dev/null and b/src/contrib/rest/conf/keys/rest.cer differ diff --git a/src/contrib/rest/conf/keys/rest.jks b/src/contrib/rest/conf/keys/rest.jks new file mode 100644 index 0000000..539e8be Binary files /dev/null and b/src/contrib/rest/conf/keys/rest.jks differ diff --git a/src/contrib/rest/conf/log4j.properties b/src/contrib/rest/conf/log4j.properties new file mode 100644 index 0000000..21ba7e4 --- /dev/null +++ b/src/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/src/contrib/rest/conf/rest.properties b/src/contrib/rest/conf/rest.properties new file mode 100644 index 0000000..f0abb45 --- /dev/null +++ b/src/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/src/contrib/rest/ivy.xml b/src/contrib/rest/ivy.xml new file mode 100644 index 0000000..903d390 --- /dev/null +++ b/src/contrib/rest/ivy.xml @@ -0,0 +1,48 @@ + + + + + + + + ZooKeeper REST + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/rest/rest.sh b/src/contrib/rest/rest.sh new file mode 100644 index 0000000..daa8198 --- /dev/null +++ b/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/RestMain.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/RestMain.java new file mode 100644 index 0000000..954ad04 --- /dev/null +++ b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/RestMain.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.jersey; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; + +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(); + gws = new GrizzlyWebServer(cfg.getPort(), "/tmp/23cxv45345/2131xc2/", 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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/ZooKeeperService.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/ZooKeeperService.java new file mode 100644 index 0000000..21d27a9 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/Credentials.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/Credentials.java new file mode 100644 index 0000000..0730be5 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/Endpoint.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/Endpoint.java new file mode 100644 index 0000000..2a62782 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/HostPort.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/HostPort.java new file mode 100644 index 0000000..51a1bdd --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/HostPortSet.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/HostPortSet.java new file mode 100644 index 0000000..301a565 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/RestCfg.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/cfg/RestCfg.java new file mode 100644 index 0000000..93dd632 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/filters/HTTPBasicAuth.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/filters/HTTPBasicAuth.java new file mode 100644 index 0000000..49640b5 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZChildren.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZChildren.java new file mode 100644 index 0000000..b3fad55 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZChildrenJSON.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZChildrenJSON.java new file mode 100644 index 0000000..0dcece0 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZError.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZError.java new file mode 100644 index 0000000..e976ee0 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZPath.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZPath.java new file mode 100644 index 0000000..4d83717 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZSession.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZSession.java new file mode 100644 index 0000000..06ca9e5 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZStat.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/jaxb/ZStat.java new file mode 100644 index 0000000..af70d18 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/JAXBContextResolver.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/JAXBContextResolver.java new file mode 100644 index 0000000..0893586 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/KeeperExceptionMapper.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/KeeperExceptionMapper.java new file mode 100644 index 0000000..fdfc27b --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/RuntimeExceptionMapper.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/RuntimeExceptionMapper.java new file mode 100644 index 0000000..46f33bb --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/SessionsResource.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/SessionsResource.java new file mode 100644 index 0000000..0744604 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/ZErrorWriter.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/ZErrorWriter.java new file mode 100644 index 0000000..706ab89 --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/ZNodeResource.java b/src/contrib/rest/src/java/org/apache/zookeeper/server/jersey/resources/ZNodeResource.java new file mode 100644 index 0000000..77371ea --- /dev/null +++ b/src/contrib/rest/src/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/src/contrib/rest/src/python/README.txt b/src/contrib/rest/src/python/README.txt new file mode 100644 index 0000000..acc8ffb --- /dev/null +++ b/src/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/src/contrib/rest/src/python/demo_master_election.py b/src/contrib/rest/src/python/demo_master_election.py new file mode 100644 index 0000000..c0317c7 --- /dev/null +++ b/src/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/src/contrib/rest/src/python/demo_queue.py b/src/contrib/rest/src/python/demo_queue.py new file mode 100644 index 0000000..9ca4c64 --- /dev/null +++ b/src/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/src/contrib/rest/src/python/test.py b/src/contrib/rest/src/python/test.py new file mode 100644 index 0000000..363747a --- /dev/null +++ b/src/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/src/contrib/rest/src/python/zk_dump_tree.py b/src/contrib/rest/src/python/zk_dump_tree.py new file mode 100644 index 0000000..517d23b --- /dev/null +++ b/src/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/src/contrib/rest/src/python/zkrest.py b/src/contrib/rest/src/python/zkrest.py new file mode 100644 index 0000000..c009d5d --- /dev/null +++ b/src/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/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/Base.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/Base.java new file mode 100644 index 0000000..2d5f51a --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/Base.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.jersey; + +import java.io.ByteArrayInputStream; + +import junit.framework.TestCase; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +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 com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; + +/** + * Test stand-alone server. + * + */ +public class Base extends TestCase { + 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 { + super.setUp(); + + 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 { + super.tearDown(); + + 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/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/CreateTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/CreateTest.java new file mode 100644 index 0000000..49c3349 --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/CreateTest.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.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.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 { + LOG.info("STARTING " + getName()); + + 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); + } + assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedPath == null) { + return; + } + + ZPath zpath = cr.getEntity(ZPath.class); + if (sequence) { + assertTrue(zpath.path.startsWith(expectedPath.path)); + assertTrue(zpath.uri.startsWith(znodesr.path(path).toString())); + } else { + assertEquals(expectedPath, zpath); + 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) { + assertEquals(data, this.data); + } else { + assertTrue(new String(data) + " == " + new String(this.data), + Arrays.equals(data, this.data)); + } + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/DeleteTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/DeleteTest.java new file mode 100644 index 0000000..052239d --- /dev/null +++ b/src/contrib/rest/src/test/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.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); + assertEquals(expectedStatus, cr.getClientResponseStatus()); + + // use out-of-band method to verify + Stat stat = zk.exists(zpath, false); + assertNull(stat); + } + + @Test + public void testDelete() throws Exception { + LOG.info("STARTING " + getName()); + verify(MediaType.APPLICATION_OCTET_STREAM); + verify(MediaType.APPLICATION_JSON); + verify(MediaType.APPLICATION_XML); + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/ExistsTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/ExistsTest.java new file mode 100644 index 0000000..696ea95 --- /dev/null +++ b/src/contrib/rest/src/test/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.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) { + assertEquals(ClientResponse.Status.NO_CONTENT, + cr.getClientResponseStatus()); + } else { + assertEquals(expectedStatus, cr.getClientResponseStatus()); + } + } + + @Test + public void testExists() throws Exception { + LOG.info("STARTING " + getName()); + verify(MediaType.APPLICATION_OCTET_STREAM); + verify(MediaType.APPLICATION_JSON); + verify(MediaType.APPLICATION_XML); + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetChildrenTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetChildrenTest.java new file mode 100644 index 0000000..a046692 --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetChildrenTest.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.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.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 { + LOG.info("STARTING " + getName()); + + 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); + 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); + assertEquals(expectedChildren, zchildren.children); + assertEquals(znodesr.path(path).toString(), zchildren.uri); + 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); + assertEquals(expectedChildren, zchildren.children); + assertEquals(znodesr.path(path).toString(), zchildren.uri); + assertEquals(znodesr.path(path).toString() + "/{child}", + zchildren.child_uri_template); + } else { + fail("unknown accept type"); + } + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetTest.java new file mode 100644 index 0000000..f00946e --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/GetTest.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.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.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 { + LOG.info("STARTING " + getName()); + + 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); + assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedStat == null) { + return; + } + + ZStat zstat = cr.getEntity(ZStat.class); + assertEquals(expectedStat, zstat); + assertEquals(znodesr.path(path).toString(), zstat.uri); + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/RestTestSuite.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/RestTestSuite.java new file mode 100644 index 0000000..fc69caf --- /dev/null +++ b/src/contrib/rest/src/test/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/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/RootTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/RootTest.java new file mode 100644 index 0000000..a4f9b9e --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/RootTest.java @@ -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. + */ + +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.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 { + LOG.info("STARTING " + getName()); + + 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); + assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); + + ZPath zpath = cr.getEntity(ZPath.class); + assertEquals(new ZPath(path + name), zpath); + assertEquals(znodesr.path(path).toString(), zpath.uri); + + // use out-of-band method to verify + byte[] rdata = zk.getData(zpath.path, false, new Stat()); + assertTrue(new String(rdata) + " == " + new String(data), + Arrays.equals(rdata, data)); + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SessionTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SessionTest.java new file mode 100644 index 0000000..3e165c0 --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SessionTest.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.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.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); + assertEquals(ClientResponse.Status.CREATED, cr + .getClientResponseStatus()); + + return cr.getEntity(ZSession.class); + } + + @Test + public void testCreateNewSession() throws JSONException { + ZSession session = createSession(); + assertEquals(session.id.length(), 36); + + // use out-of-band method to verify + assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + } + + @Test + public void testSessionExpires() throws InterruptedException { + ZSession session = createSession("1"); + + // use out-of-band method to verify + assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + + // wait for the session to be closed + Thread.sleep(1500); + 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); + + assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + ClientResponse cr = b.delete(ClientResponse.class, null); + assertEquals(ClientResponse.Status.NO_CONTENT, + cr.getClientResponseStatus()); + + 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); + assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus()); + + Thread.sleep(1500); + assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + + Thread.sleep(1000); + 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); + assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); + + Stat stat = new Stat(); + zk.getData("/ephemeral-test", false, stat); + + ZooKeeper sessionZK = ZooKeeperService.getClient(CONTEXT_PATH, session.id); + assertEquals(stat.getEphemeralOwner(), sessionZK.getSessionId()); + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SetTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SetTest.java new file mode 100644 index 0000000..cbcfd21 --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/SetTest.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.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.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 { + LOG.info("STARTING " + getName()); + + 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)); + } + assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedStat == null) { + return; + } + + ZStat zstat = cr.getEntity(ZStat.class); + 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) { + fail((data == null ? null : new String(data)) + " == " + + (this.data == null ? null : new String(this.data))); + } else { + assertTrue(new String(data) + " == " + new String(this.data), + Arrays.equals(data, this.data)); + } + } +} diff --git a/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/WadlTest.java b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/WadlTest.java new file mode 100644 index 0000000..4009e6d --- /dev/null +++ b/src/contrib/rest/src/test/org/apache/zookeeper/server/jersey/WadlTest.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.jersey; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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); + assertTrue("Something wrong. Returned wadl length not > 0.", + serviceWadl.length() > 0); + } +} diff --git a/src/contrib/rest/src/test/zkServer.sh b/src/contrib/rest/src/test/zkServer.sh new file mode 100644 index 0000000..bff85f3 --- /dev/null +++ b/src/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}"/src/java/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/src/contrib/zkfuse/Makefile.am b/src/contrib/zkfuse/Makefile.am new file mode 100644 index 0000000..36da1a5 --- /dev/null +++ b/src/contrib/zkfuse/Makefile.am @@ -0,0 +1,4 @@ +## Process this file with automake to produce Makefile.in + +SUBDIRS = src + diff --git a/src/contrib/zkfuse/README.txt b/src/contrib/zkfuse/README.txt new file mode 100644 index 0000000..901d363 --- /dev/null +++ b/src/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/src/contrib/zkfuse/build.xml b/src/contrib/zkfuse/build.xml new file mode 100644 index 0000000..f589453 --- /dev/null +++ b/src/contrib/zkfuse/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zkfuse/configure.ac b/src/contrib/zkfuse/configure.ac new file mode 100644 index 0000000..c86e7cd --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/Makefile.am b/src/contrib/zkfuse/src/Makefile.am new file mode 100644 index 0000000..c0d87e3 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/blockingqueue.h b/src/contrib/zkfuse/src/blockingqueue.h new file mode 100644 index 0000000..4677290 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/doxygen.cfg b/src/contrib/zkfuse/src/doxygen.cfg new file mode 100644 index 0000000..308b094 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/event.cc b/src/contrib/zkfuse/src/event.cc new file mode 100644 index 0000000..541657e --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/event.h b/src/contrib/zkfuse/src/event.h new file mode 100644 index 0000000..936ecc6 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/log.cc b/src/contrib/zkfuse/src/log.cc new file mode 100644 index 0000000..e2bfb0d --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/log.h b/src/contrib/zkfuse/src/log.h new file mode 100644 index 0000000..aefce10 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/log4cxx.properties b/src/contrib/zkfuse/src/log4cxx.properties new file mode 100644 index 0000000..1e373e4 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/mutex.h b/src/contrib/zkfuse/src/mutex.h new file mode 100644 index 0000000..86c4604 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/thread.cc b/src/contrib/zkfuse/src/thread.cc new file mode 100644 index 0000000..f1ed816 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/thread.h b/src/contrib/zkfuse/src/thread.h new file mode 100644 index 0000000..0ed12d7 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/zkadapter.cc b/src/contrib/zkfuse/src/zkadapter.cc new file mode 100644 index 0000000..7f02fa3 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/zkadapter.h b/src/contrib/zkfuse/src/zkadapter.h new file mode 100644 index 0000000..8d4d1d5 --- /dev/null +++ b/src/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/src/contrib/zkfuse/src/zkfuse.cc b/src/contrib/zkfuse/src/zkfuse.cc new file mode 100644 index 0000000..6a82168 --- /dev/null +++ b/src/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/src/contrib/zkperl/Changes b/src/contrib/zkperl/Changes new file mode 100644 index 0000000..c31420e --- /dev/null +++ b/src/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/src/contrib/zkperl/LICENSE b/src/contrib/zkperl/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/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/src/contrib/zkperl/MANIFEST b/src/contrib/zkperl/MANIFEST new file mode 100644 index 0000000..1b57436 --- /dev/null +++ b/src/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/src/contrib/zkperl/Makefile.PL b/src/contrib/zkperl/Makefile.PL new file mode 100644 index 0000000..9a0996d --- /dev/null +++ b/src/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/src/contrib/zkperl/NOTICE b/src/contrib/zkperl/NOTICE new file mode 100644 index 0000000..b68fdac --- /dev/null +++ b/src/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/src/contrib/zkperl/README b/src/contrib/zkperl/README new file mode 100644 index 0000000..bbe2a0d --- /dev/null +++ b/src/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/src/contrib/zkperl/ZooKeeper.pm b/src/contrib/zkperl/ZooKeeper.pm new file mode 100644 index 0000000..507f029 --- /dev/null +++ b/src/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/src/contrib/zkperl/ZooKeeper.xs b/src/contrib/zkperl/ZooKeeper.xs new file mode 100644 index 0000000..4b6067b --- /dev/null +++ b/src/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/src/contrib/zkperl/build.xml b/src/contrib/zkperl/build.xml new file mode 100644 index 0000000..0c7207d --- /dev/null +++ b/src/contrib/zkperl/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zkperl/t/10_invalid.t b/src/contrib/zkperl/t/10_invalid.t new file mode 100644 index 0000000..5e080b6 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/15_thread.t b/src/contrib/zkperl/t/15_thread.t new file mode 100644 index 0000000..1ef56d0 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/20_tie.t b/src/contrib/zkperl/t/20_tie.t new file mode 100644 index 0000000..37e9a4f --- /dev/null +++ b/src/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/src/contrib/zkperl/t/22_stat_tie.t b/src/contrib/zkperl/t/22_stat_tie.t new file mode 100644 index 0000000..02e7913 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/24_watch_tie.t b/src/contrib/zkperl/t/24_watch_tie.t new file mode 100644 index 0000000..e77879e --- /dev/null +++ b/src/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/src/contrib/zkperl/t/30_connect.t b/src/contrib/zkperl/t/30_connect.t new file mode 100644 index 0000000..c2b68bb --- /dev/null +++ b/src/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/src/contrib/zkperl/t/35_log.t b/src/contrib/zkperl/t/35_log.t new file mode 100644 index 0000000..92821af --- /dev/null +++ b/src/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/src/contrib/zkperl/t/40_basic.t b/src/contrib/zkperl/t/40_basic.t new file mode 100644 index 0000000..38a8a21 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/45_class.t b/src/contrib/zkperl/t/45_class.t new file mode 100644 index 0000000..4aa1a57 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/50_access.t b/src/contrib/zkperl/t/50_access.t new file mode 100644 index 0000000..ef61ed6 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/60_watch.t b/src/contrib/zkperl/t/60_watch.t new file mode 100644 index 0000000..7d30602 --- /dev/null +++ b/src/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/src/contrib/zkperl/t/util.pl b/src/contrib/zkperl/t/util.pl new file mode 100644 index 0000000..1ca738d --- /dev/null +++ b/src/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/src/contrib/zkperl/typemap b/src/contrib/zkperl/typemap new file mode 100644 index 0000000..84636fd --- /dev/null +++ b/src/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/src/contrib/zkpython/README b/src/contrib/zkpython/README new file mode 100644 index 0000000..89d9998 --- /dev/null +++ b/src/contrib/zkpython/README @@ -0,0 +1,109 @@ +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 (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. + +Python >= 2.6 is required. We have tested against 2.6. We have not tested against 3.x. + +BUILD AND INSTALL: +------------------- + +To install, make sure that the C client has been built and that the libraries are installed in /usr/local/lib (or change this directory in setup.py). Then run: + +ant install + +from zookeeper/src/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/src/contrib/zkpython/build.xml b/src/contrib/zkpython/build.xml new file mode 100644 index 0000000..029d4f2 --- /dev/null +++ b/src/contrib/zkpython/build.xml @@ -0,0 +1,198 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zkpython/ivy.xml b/src/contrib/zkpython/ivy.xml new file mode 100644 index 0000000..6931f46 --- /dev/null +++ b/src/contrib/zkpython/ivy.xml @@ -0,0 +1,43 @@ + + + + + + + + ZKPython + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zkpython/src/c/pyzk_docstrings.h b/src/contrib/zkpython/src/c/pyzk_docstrings.h new file mode 100644 index 0000000..1f38d53 --- /dev/null +++ b/src/contrib/zkpython/src/c/pyzk_docstrings.h @@ -0,0 +1,594 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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_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/src/contrib/zkpython/src/c/zookeeper.c b/src/contrib/zkpython/src/c/zookeeper.c new file mode 100644 index 0000000..4474661 --- /dev/null +++ b/src/contrib/zkpython/src/c/zookeeper.c @@ -0,0 +1,1664 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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)); + acls->data[i].id.id = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "id" ) ) ); + acls->data[i].id.scheme = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "scheme" ) ) ); +#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(PyObject *self, PyObject *args) +{ + const char *host; + PyObject *watcherfn = Py_None; + int recv_timeout = 10000; + // int clientid = -1; + 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 (!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; + zhandle_t *zh = zookeeper_init( host, watcherfn != Py_None ? watcher_dispatch : NULL, + recv_timeout, cid.client_id == -1 ? 0 : &cid, + pyw, + 0 ); + + if (zh == NULL) + { + PyErr_SetString( ZooKeeperException, "Could not internally obtain zookeeper handle" ); + return NULL; + } + + zhandles[handle] = zh; + return Py_BuildValue( "i", handle); +} + + +/* -------------------------------------------------------------------------- */ +/* 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 }, + {"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) { + return; // TODO: Is there any way to raise an exception here? + } + + ZooKeeperException = PyErr_NewException("zookeeper.ZooKeeperException", + PyExc_Exception, + NULL); + + PyModule_AddObject(module, "ZooKeeperException", ZooKeeperException); + Py_INCREF(ZooKeeperException); + + int size = 10; + char version_str[size]; + snprintf(version_str, size, "%i.%i.%i", ZOO_MAJOR_VERSION, ZOO_MINOR_VERSION, ZOO_PATCH_VERSION); + + PyModule_AddStringConstant(module, "__version__", version_str); + + 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/src/contrib/zkpython/src/examples/README b/src/contrib/zkpython/src/examples/README new file mode 100644 index 0000000..3c53454 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/examples/watch_znode_for_changes.py b/src/contrib/zkpython/src/examples/watch_znode_for_changes.py new file mode 100644 index 0000000..07100f0 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/packages/deb/zkpython.control/control b/src/contrib/zkpython/src/packages/deb/zkpython.control/control new file mode 100644 index 0000000..6ec76d0 --- /dev/null +++ b/src/contrib/zkpython/src/packages/deb/zkpython.control/control @@ -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. +Package: zkpython +Version: @version@ +Section: misc +Priority: optional +Architecture: all +Depends: python +Maintainer: Apache Software Foundation +Description: ZooKeeper python binding library +Distribution: development diff --git a/src/contrib/zkpython/src/packages/rpm/spec/zkpython.spec b/src/contrib/zkpython/src/packages/rpm/spec/zkpython.spec new file mode 100644 index 0000000..d1f9814 --- /dev/null +++ b/src/contrib/zkpython/src/packages/rpm/spec/zkpython.spec @@ -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. + +# +# RPM Spec file for ZooKeeper version @version@ +# + +%define name zkpython +%define version @version@ +%define release @package.release@ + +# Installation Locations +%define _prefix @package.prefix@ + +# Build time settings +%define _build_dir @package.build.dir@ +%define _final_name @final.name@ +%define _python_lib @python.lib@ +%define debug_package %{nil} + +# Disable brp-java-repack-jars for aspect J +%define __os_install_post \ + /usr/lib/rpm/redhat/brp-compress \ + %{!?__debug_package:/usr/lib/rpm/redhat/brp-strip %{__strip}} \ + /usr/lib/rpm/redhat/brp-strip-static-archive %{__strip} \ + /usr/lib/rpm/redhat/brp-strip-comment-note %{__strip} %{__objdump} \ + /usr/lib/rpm/brp-python-bytecompile %{nil} + +# RPM searches perl files for dependancies and this breaks for non packaged perl lib +# like thrift so disable this +%define _use_internal_dependency_generator 0 + +Summary: ZooKeeper python binding library +Group: Development/Libraries +License: Apache License, Version 2.0 +URL: http://zookeeper.apache.org/ +Vendor: Apache Software Foundation +Name: %{name} +Version: %{version} +Release: %{release} +Source0: %{_python_lib} +Prefix: %{_prefix} +Requires: zookeeper-lib == %{version} +AutoReqProv: no +Provides: zkpython + +%description +ZooKeeper python binding library + +%prep +tar fxz %{_python_lib} -C %{_build_dir} + +%build + +######################### +#### INSTALL SECTION #### +######################### +%install + +%pre + +%post + +%preun + +%files +%defattr(-,root,root) +%{_prefix} + diff --git a/src/contrib/zkpython/src/python/setup.py b/src/contrib/zkpython/src/python/setup.py new file mode 100644 index 0000000..c6a1cee --- /dev/null +++ b/src/contrib/zkpython/src/python/setup.py @@ -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. + +from distutils.core import setup, Extension + +zookeeper_basedir = "../../../" + +zookeepermodule = Extension("zookeeper", + sources=["src/c/zookeeper.c"], + include_dirs=[zookeeper_basedir + "/src/c/include", + zookeeper_basedir + "/build/c", + zookeeper_basedir + "/src/c/generated"], + libraries=["zookeeper_mt"], + library_dirs=[zookeeper_basedir + "/src/c/.libs/", + zookeeper_basedir + "/build/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/src/contrib/zkpython/src/python/zk.py b/src/contrib/zkpython/src/python/zk.py new file mode 100644 index 0000000..24986e3 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/acl_test.py b/src/contrib/zkpython/src/test/acl_test.py new file mode 100644 index 0000000..1289c8a --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/async_test.py b/src/contrib/zkpython/src/test/async_test.py new file mode 100644 index 0000000..e813435 --- /dev/null +++ b/src/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 = zookeeper.async(self.handle, "/") + self.assertEqual(ret, zookeeper.OK, "async failed") + +if __name__ == '__main__': + unittest.main() diff --git a/src/contrib/zkpython/src/test/callback_test.py b/src/contrib/zkpython/src/test/callback_test.py new file mode 100644 index 0000000..55d7fe1 --- /dev/null +++ b/src/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: 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/src/contrib/zkpython/src/test/clientid_test.py b/src/contrib/zkpython/src/test/clientid_test.py new file mode 100644 index 0000000..90c8f0a --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/close_deadlock_test.py b/src/contrib/zkpython/src/test/close_deadlock_test.py new file mode 100644 index 0000000..921d2cc --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/connection_test.py b/src/contrib/zkpython/src/test/connection_test.py new file mode 100644 index 0000000..3913fe3 --- /dev/null +++ b/src/contrib/zkpython/src/test/connection_test.py @@ -0,0 +1,131 @@ +#!/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, + "/") + + 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/src/contrib/zkpython/src/test/create_test.py b/src/contrib/zkpython/src/test/create_test.py new file mode 100644 index 0000000..8ab80f9 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/delete_test.py b/src/contrib/zkpython/src/test/delete_test.py new file mode 100644 index 0000000..913b6a9 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/exists_test.py b/src/contrib/zkpython/src/test/exists_test.py new file mode 100644 index 0000000..ddc6ef3 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/get_set_test.py b/src/contrib/zkpython/src/test/get_set_test.py new file mode 100644 index 0000000..b77b3b2 --- /dev/null +++ b/src/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/src/contrib/zkpython/src/test/run_tests.sh b/src/contrib/zkpython/src/test/run_tests.sh new file mode 100644 index 0000000..18d9240 --- /dev/null +++ b/src/contrib/zkpython/src/test/run_tests.sh @@ -0,0 +1,40 @@ +#!/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 + +# 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 ../../../build/ -name "zookeeper.so" | head -1` +PYTHONPATH=`dirname $SO_PATH` +LIB_PATH=../../c/.libs/:../../../build/test/test-cppunit/.libs +for test in `ls $1/*_test.py`; +do + echo "Running $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/src/contrib/zkpython/src/test/zkServer.sh b/src/contrib/zkpython/src/test/zkServer.sh new file mode 100644 index 0000000..51d508f --- /dev/null +++ b/src/contrib/zkpython/src/test/zkServer.sh @@ -0,0 +1,77 @@ +#!/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 + +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}"/src/java/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) + if [ "x${base_dir}" == "x" ] + then + mkdir -p /tmp/zkdata + java -cp $CLASSPATH org.apache.zookeeper.server.ZooKeeperServerMain 22182 /tmp/zkdata &> /tmp/zk.log & + 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 & + fi + sleep 5 + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/src/contrib/zkpython/src/test/zktestbase.py b/src/contrib/zkpython/src/test/zktestbase.py new file mode 100644 index 0000000..8229418 --- /dev/null +++ b/src/contrib/zkpython/src/test/zktestbase.py @@ -0,0 +1,101 @@ +#!/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 + + def __init__(self,methodName='runTest'): + unittest.TestCase.__init__(self,methodName) + self.host = "localhost:%d" % self.SERVER_PORT + 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/src/contrib/zktreeutil/Makefile.am b/src/contrib/zktreeutil/Makefile.am new file mode 100644 index 0000000..36da1a5 --- /dev/null +++ b/src/contrib/zktreeutil/Makefile.am @@ -0,0 +1,4 @@ +## Process this file with automake to produce Makefile.in + +SUBDIRS = src + diff --git a/src/contrib/zktreeutil/README.txt b/src/contrib/zktreeutil/README.txt new file mode 100644 index 0000000..43b06fa --- /dev/null +++ b/src/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/src/contrib/zktreeutil/build.xml b/src/contrib/zktreeutil/build.xml new file mode 100644 index 0000000..809d134 --- /dev/null +++ b/src/contrib/zktreeutil/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zktreeutil/configure.ac b/src/contrib/zktreeutil/configure.ac new file mode 100644 index 0000000..b4a82a7 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/Makefile.am b/src/contrib/zktreeutil/src/Makefile.am new file mode 100644 index 0000000..641077a --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/SimpleTree.h b/src/contrib/zktreeutil/src/SimpleTree.h new file mode 100644 index 0000000..8226f05 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/ZkAdaptor.cc b/src/contrib/zktreeutil/src/ZkAdaptor.cc new file mode 100644 index 0000000..1df175a --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/ZkAdaptor.h b/src/contrib/zktreeutil/src/ZkAdaptor.h new file mode 100644 index 0000000..4b68e28 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/ZkTreeUtil.cc b/src/contrib/zktreeutil/src/ZkTreeUtil.cc new file mode 100644 index 0000000..270bf31 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/ZkTreeUtil.h b/src/contrib/zktreeutil/src/ZkTreeUtil.h new file mode 100644 index 0000000..0a9be03 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/src/ZkTreeUtilMain.cc b/src/contrib/zktreeutil/src/ZkTreeUtilMain.cc new file mode 100644 index 0000000..8afebf6 --- /dev/null +++ b/src/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/src/contrib/zktreeutil/tests/zk_sample.xml b/src/contrib/zktreeutil/tests/zk_sample.xml new file mode 100644 index 0000000..6e97daa --- /dev/null +++ b/src/contrib/zktreeutil/tests/zk_sample.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zooinspector/NOTICE.txt b/src/contrib/zooinspector/NOTICE.txt new file mode 100644 index 0000000..e0bf2f0 --- /dev/null +++ b/src/contrib/zooinspector/NOTICE.txt @@ -0,0 +1,3 @@ +This contrib module includes icons available under the Eclipse Public Licence Version 1.0 +. from the Eclipse Java Devlopment Platform. +The lib sub-directory includes a binary only jar library developed at http://sourceforge.net/projects/jtoaster/ \ No newline at end of file diff --git a/src/contrib/zooinspector/README.txt b/src/contrib/zooinspector/README.txt new file mode 100644 index 0000000..3c2a58f --- /dev/null +++ b/src/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/src/contrib/zooinspector/build.xml b/src/contrib/zooinspector/build.xml new file mode 100644 index 0000000..a242790 --- /dev/null +++ b/src/contrib/zooinspector/build.xml @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zooinspector/config/defaultConnectionSettings.cfg b/src/contrib/zooinspector/config/defaultConnectionSettings.cfg new file mode 100644 index 0000000..36a34ff --- /dev/null +++ b/src/contrib/zooinspector/config/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= diff --git a/src/contrib/zooinspector/config/defaultNodeVeiwers.cfg b/src/contrib/zooinspector/config/defaultNodeVeiwers.cfg new file mode 100644 index 0000000..38fe9a8 --- /dev/null +++ b/src/contrib/zooinspector/config/defaultNodeVeiwers.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/src/contrib/zooinspector/icons/edtsrclkup_co.gif b/src/contrib/zooinspector/icons/edtsrclkup_co.gif new file mode 100644 index 0000000..94eedf6 Binary files /dev/null and b/src/contrib/zooinspector/icons/edtsrclkup_co.gif differ diff --git a/src/contrib/zooinspector/icons/file_obj.gif b/src/contrib/zooinspector/icons/file_obj.gif new file mode 100644 index 0000000..b226e41 Binary files /dev/null and b/src/contrib/zooinspector/icons/file_obj.gif differ diff --git a/src/contrib/zooinspector/icons/fldr_obj.gif b/src/contrib/zooinspector/icons/fldr_obj.gif new file mode 100644 index 0000000..51e703b Binary files /dev/null and b/src/contrib/zooinspector/icons/fldr_obj.gif differ diff --git a/src/contrib/zooinspector/icons/info_obj.gif b/src/contrib/zooinspector/icons/info_obj.gif new file mode 100644 index 0000000..2da001e Binary files /dev/null and b/src/contrib/zooinspector/icons/info_obj.gif differ diff --git a/src/contrib/zooinspector/icons/jspdecl.gif b/src/contrib/zooinspector/icons/jspdecl.gif new file mode 100644 index 0000000..35f48ef Binary files /dev/null and b/src/contrib/zooinspector/icons/jspdecl.gif differ diff --git a/src/contrib/zooinspector/icons/launch_run.gif b/src/contrib/zooinspector/icons/launch_run.gif new file mode 100644 index 0000000..57f4102 Binary files /dev/null and b/src/contrib/zooinspector/icons/launch_run.gif differ diff --git a/src/contrib/zooinspector/icons/launch_stop.gif b/src/contrib/zooinspector/icons/launch_stop.gif new file mode 100644 index 0000000..fbf1686 Binary files /dev/null and b/src/contrib/zooinspector/icons/launch_stop.gif differ diff --git a/src/contrib/zooinspector/icons/new_con.gif b/src/contrib/zooinspector/icons/new_con.gif new file mode 100644 index 0000000..9d05088 Binary files /dev/null and b/src/contrib/zooinspector/icons/new_con.gif differ diff --git a/src/contrib/zooinspector/icons/refresh.gif b/src/contrib/zooinspector/icons/refresh.gif new file mode 100644 index 0000000..3ca04d0 Binary files /dev/null and b/src/contrib/zooinspector/icons/refresh.gif differ diff --git a/src/contrib/zooinspector/icons/save_edit.gif b/src/contrib/zooinspector/icons/save_edit.gif new file mode 100644 index 0000000..499dd0c Binary files /dev/null and b/src/contrib/zooinspector/icons/save_edit.gif differ diff --git a/src/contrib/zooinspector/icons/search_next.gif b/src/contrib/zooinspector/icons/search_next.gif new file mode 100644 index 0000000..072b184 Binary files /dev/null and b/src/contrib/zooinspector/icons/search_next.gif differ diff --git a/src/contrib/zooinspector/icons/search_prev.gif b/src/contrib/zooinspector/icons/search_prev.gif new file mode 100644 index 0000000..0716475 Binary files /dev/null and b/src/contrib/zooinspector/icons/search_prev.gif differ diff --git a/src/contrib/zooinspector/icons/trash.gif b/src/contrib/zooinspector/icons/trash.gif new file mode 100644 index 0000000..5f47780 Binary files /dev/null and b/src/contrib/zooinspector/icons/trash.gif differ diff --git a/src/contrib/zooinspector/ivy.xml b/src/contrib/zooinspector/ivy.xml new file mode 100644 index 0000000..bc02946 --- /dev/null +++ b/src/contrib/zooinspector/ivy.xml @@ -0,0 +1,48 @@ + + + + + + + + ZooInspector + + + + + + + + + + + + + + + + + + + + diff --git a/src/contrib/zooinspector/lib/jtoaster-1.0.4.jar b/src/contrib/zooinspector/lib/jtoaster-1.0.4.jar new file mode 100644 index 0000000..aaf3f6e Binary files /dev/null and b/src/contrib/zooinspector/lib/jtoaster-1.0.4.jar differ diff --git a/src/contrib/zooinspector/lib/log4j.properties b/src/contrib/zooinspector/lib/log4j.properties new file mode 100644 index 0000000..6f96d84 --- /dev/null +++ b/src/contrib/zooinspector/lib/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/src/contrib/zooinspector/licences/Apache Software Licence v2.0.txt b/src/contrib/zooinspector/licences/Apache Software Licence v2.0.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/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/src/contrib/zooinspector/licences/epl-v10.html b/src/contrib/zooinspector/licences/epl-v10.html new file mode 100644 index 0000000..fd39122 --- /dev/null +++ b/src/contrib/zooinspector/licences/epl-v10.html @@ -0,0 +1,261 @@ + + + + + + +Eclipse Public License - Version 1.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.

+ + + + diff --git a/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/ZooInspector.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/ZooInspector.java new file mode 100644 index 0000000..b637bfd --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/ZooInspector.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.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.ZooInspectorPanel; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorManagerImpl; + +/** + * + */ +public class ZooInspector { + /** + * @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); + final ZooInspectorPanel zooInspectorPanel = new ZooInspectorPanel( + new ZooInspectorManagerImpl()); + 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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java new file mode 100644 index 0000000..a9e5ac4 --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.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.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 { + 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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java new file mode 100644 index 0000000..15a9ee4 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java new file mode 100644 index 0000000..b0b1e98 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java new file mode 100644 index 0000000..fbae8f1 --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.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.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) { + super(frame); + this.setLayout(new BorderLayout()); + this.setIconImage(ZooInspectorIconResources.getInformationIcon() + .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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java new file mode 100644 index 0000000..c7db524 --- /dev/null +++ b/src/contrib/zooinspector/src/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 HashMap 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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorIconResources.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorIconResources.java new file mode 100644 index 0000000..fa45ab3 --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorIconResources.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.inspector.gui; + +import javax.swing.ImageIcon; + +/** + * A class containing static methods for retrieving {@link ImageIcon}s used in + * the application + */ +public class ZooInspectorIconResources { + + /** + * @return file icon + */ + public static ImageIcon getTreeLeafIcon() { + return new ImageIcon("icons/file_obj.gif"); //$NON-NLS-1$ + } + + /** + * @return folder open icon + */ + public static ImageIcon getTreeOpenIcon() { + return new ImageIcon("icons/fldr_obj.gif"); //$NON-NLS-1$ + } + + /** + * @return folder closed icon + */ + public static ImageIcon getTreeClosedIcon() { + return new ImageIcon("icons/fldr_obj.gif"); //$NON-NLS-1$ + } + + /** + * @return connect icon + */ + public static ImageIcon getConnectIcon() { + return new ImageIcon("icons/launch_run.gif"); //$NON-NLS-1$ + } + + /** + * @return disconnect icon + */ + public static ImageIcon getDisconnectIcon() { + return new ImageIcon("icons/launch_stop.gif"); //$NON-NLS-1$ + } + + /** + * @return save icon + */ + public static ImageIcon getSaveIcon() { + return new ImageIcon("icons/save_edit.gif"); //$NON-NLS-1$ + } + + /** + * @return add icon + */ + public static ImageIcon getAddNodeIcon() { + return new ImageIcon("icons/new_con.gif"); //$NON-NLS-1$ + } + + /** + * @return delete icon + */ + public static ImageIcon getDeleteNodeIcon() { + return new ImageIcon("icons/trash.gif"); //$NON-NLS-1$ + } + + /** + * @return refresh icon + */ + public static ImageIcon getRefreshIcon() { + return new ImageIcon("icons/refresh.gif"); //$NON-NLS-1$ + } + + /** + * @return information icon + */ + public static ImageIcon getInformationIcon() { + return new ImageIcon("icons/info_obj.gif"); //$NON-NLS-1$ + } + + /** + * @return node viewers icon + */ + public static ImageIcon getChangeNodeViewersIcon() { + return new ImageIcon("icons/edtsrclkup_co.gif"); //$NON-NLS-1$ + } + + /** + * @return up icon + */ + public static ImageIcon getUpIcon() { + return new ImageIcon("icons/search_prev.gif"); //$NON-NLS-1$ + } + + /** + * @return down icon + */ + public static ImageIcon getDownIcon() { + return new ImageIcon("icons/search_next.gif"); //$NON-NLS-1$ + } +} diff --git a/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java new file mode 100644 index 0000000..fd0b00a --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java @@ -0,0 +1,605 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.List; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.DropMode; +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.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 JButton upButton; + private final JButton downButton; + private final JButton removeButton; + private final JButton addButton; + private final JList viewersList; + private final JButton saveFileButton; + private final JButton loadFileButton; + private final JButton setDefaultsButton; + private final JFileChooser fileChooser = new JFileChooser(new File(".")); + + /** + * @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) { + super(frame); + final List newViewers = new ArrayList( + currentViewers); + this.setLayout(new BorderLayout()); + this.setIconImage(ZooInspectorIconResources.getChangeNodeViewersIcon() + .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); + upButton = new JButton(ZooInspectorIconResources.getUpIcon()); + downButton = new JButton(ZooInspectorIconResources.getDownIcon()); + removeButton = new JButton(ZooInspectorIconResources + .getDeleteNodeIcon()); + addButton = new JButton(ZooInspectorIconResources.getAddNodeIcon()); + upButton.setEnabled(false); + downButton.setEnabled(false); + removeButton.setEnabled(false); + addButton.setEnabled(true); + upButton.setToolTipText("Move currently selected node viewer up"); + downButton.setToolTipText("Move currently selected node viewer down"); + removeButton.setToolTipText("Remove currently selected node viewer"); + addButton.setToolTipText("Add node viewer"); + final JTextField newViewerTextField = new JTextField(); + 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(upButton, 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(downButton, 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(removeButton, 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(addButton, c6); + upButton.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); + } + } + }); + downButton.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); + } + } + }); + removeButton.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); + } + } + }); + addButton.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); + } + } + } + }); + saveFileButton = new JButton("Save"); + loadFileButton = new JButton("Load"); + setDefaultsButton = new JButton("Set As Defaults"); + saveFileButton + .setToolTipText("Save current node viewer configuration to file"); + loadFileButton + .setToolTipText("Load node viewer configuration frm file"); + setDefaultsButton + .setToolTipText("Set current configuration asd defaults"); + 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(saveFileButton, 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(loadFileButton, 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(setDefaultsButton, c9); + saveFileButton.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 veiwer configuration from file.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error saving node veiwer configuration from file: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + } + }); + loadFileButton.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 veiwer configuration from file.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error loading node veiwer configuration from file: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + }); + setDefaultsButton.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 veiwer configuration.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error setting default node veiwer 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) { + 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); + } + } + } +} diff --git a/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java new file mode 100644 index 0000000..05c256b --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java new file mode 100644 index 0000000..7a4efaf --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java @@ -0,0 +1,361 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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 JButton refreshButton; + private final JButton disconnectButton; + private final JButton connectButton; + private final ZooInspectorNodeViewersPanel nodeViewersPanel; + private final ZooInspectorTreeViewer treeViewer; + private final ZooInspectorManager zooInspectorManager; + private final JButton addNodeButton; + private final JButton deleteNodeButton; + private final JButton nodeViewersButton; + private final JButton aboutButton; + private final List listeners = new ArrayList(); + { + listeners.add(this); + } + + /** + * @param zooInspectorManager + * - the {@link ZooInspectorManager} for the application + */ + public ZooInspectorPanel(final ZooInspectorManager zooInspectorManager) { + this.zooInspectorManager = zooInspectorManager; + final ArrayList 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); + this.setLayout(new BorderLayout()); + JToolBar toolbar = new JToolBar(); + toolbar.setFloatable(false); + connectButton = new JButton(ZooInspectorIconResources.getConnectIcon()); + disconnectButton = new JButton(ZooInspectorIconResources + .getDisconnectIcon()); + refreshButton = new JButton(ZooInspectorIconResources.getRefreshIcon()); + addNodeButton = new JButton(ZooInspectorIconResources.getAddNodeIcon()); + deleteNodeButton = new JButton(ZooInspectorIconResources + .getDeleteNodeIcon()); + nodeViewersButton = new JButton(ZooInspectorIconResources + .getChangeNodeViewersIcon()); + aboutButton = new JButton(ZooInspectorIconResources + .getInformationIcon()); + toolbar.add(connectButton); + toolbar.add(disconnectButton); + toolbar.add(refreshButton); + toolbar.add(addNodeButton); + toolbar.add(deleteNodeButton); + toolbar.add(nodeViewersButton); + toolbar.add(aboutButton); + aboutButton.setEnabled(true); + connectButton.setEnabled(true); + disconnectButton.setEnabled(false); + refreshButton.setEnabled(false); + addNodeButton.setEnabled(false); + deleteNodeButton.setEnabled(false); + nodeViewersButton.setEnabled(true); + nodeViewersButton.setToolTipText("Change Node Viewers"); + aboutButton.setToolTipText("About ZooInspector"); + connectButton.setToolTipText("Connect"); + disconnectButton.setToolTipText("Disconnect"); + refreshButton.setToolTipText("Refresh"); + addNodeButton.setToolTipText("Add Node"); + deleteNodeButton.setToolTipText("Delete Node"); + connectButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorConnectionPropertiesDialog zicpd = new ZooInspectorConnectionPropertiesDialog( + zooInspectorManager.getLastConnectionProps(), + zooInspectorManager.getConnectionPropertiesTemplate(), + ZooInspectorPanel.this); + zicpd.setVisible(true); + } + }); + disconnectButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + disconnect(); + } + }); + refreshButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + treeViewer.refreshView(); + } + }); + addNodeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + final List selectedNodes = treeViewer + .getSelectedNodes(); + if (selectedNodes.size() == 1) { + final String nodeName = JOptionPane.showInputDialog( + ZooInspectorPanel.this, + "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 ZooInspectorPanel.this.zooInspectorManager + .createNode(selectedNodes.get(0), + nodeName); + } + + @Override + protected void done() { + treeViewer.refreshView(); + } + }; + worker.execute(); + } + } else { + JOptionPane.showMessageDialog(ZooInspectorPanel.this, + "Please select 1 parent node for the new node."); + } + } + }); + deleteNodeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + final List selectedNodes = treeViewer + .getSelectedNodes(); + if (selectedNodes.size() == 0) { + JOptionPane.showMessageDialog(ZooInspectorPanel.this, + "Please select at least 1 node to be deleted"); + } else { + int answer = JOptionPane.showConfirmDialog( + ZooInspectorPanel.this, + "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) { + ZooInspectorPanel.this.zooInspectorManager + .deleteNode(nodePath); + } + return true; + } + + @Override + protected void done() { + treeViewer.refreshView(); + } + }; + worker.execute(); + } + } + } + }); + nodeViewersButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + ZooInspectorNodeViewersDialog nvd = new ZooInspectorNodeViewersDialog( + JOptionPane.getRootFrame(), nodeViewers, listeners, + zooInspectorManager); + nvd.setVisible(true); + } + }); + aboutButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorAboutDialog zicpd = new ZooInspectorAboutDialog( + JOptionPane.getRootFrame()); + 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, 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(); + connectButton.setEnabled(false); + disconnectButton.setEnabled(true); + refreshButton.setEnabled(true); + addNodeButton.setEnabled(true); + deleteNodeButton.setEnabled(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(); + connectButton.setEnabled(true); + disconnectButton.setEnabled(false); + refreshButton.setEnabled(false); + addNodeButton.setEnabled(false); + deleteNodeButton.setEnabled(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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java new file mode 100644 index 0000000..128b358 --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.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.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +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.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +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.manager.NodeListener; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +import com.nitido.utils.toaster.Toaster; + +/** + * 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; + + /** + * @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) { + this.zooInspectorManager = zooInspectorManager; + this.setLayout(new BorderLayout()); + final JPopupMenu popupMenu = new JPopupMenu(); + 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); + 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()); + 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(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())); + } + + /** + * @author Colin + * + */ + private static class ZooInspectorTreeCellRenderer extends + DefaultTreeCellRenderer { + public ZooInspectorTreeCellRenderer() { + setLeafIcon(ZooInspectorIconResources.getTreeLeafIcon()); + setOpenIcon(ZooInspectorIconResources.getTreeOpenIcon()); + setClosedIcon(ZooInspectorIconResources.getTreeClosedIcon()); + } + } + + /** + * @author Colin + * + */ + 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(ZooInspectorIconResources + .getInformationIcon(), sb.toString()); + } +} diff --git a/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/about.html b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/about.html new file mode 100644 index 0000000..17fb3dc --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java new file mode 100644 index 0000000..5ac203c --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java new file mode 100644 index 0000000..001cb7b --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java @@ -0,0 +1,146 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.gui.ZooInspectorIconResources; +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(ZooInspectorIconResources + .getSaveIcon()); + 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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java new file mode 100644 index 0000000..5c2df8d --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java new file mode 100644 index 0000000..32119a8 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java new file mode 100644 index 0000000..e4fae41 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/NodeListener.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/NodeListener.java new file mode 100644 index 0000000..fe55a45 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/Pair.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/Pair.java new file mode 100644 index 0000000..b72950c --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java new file mode 100644 index 0000000..74c3cb2 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java new file mode 100644 index 0000000..d8194d5 --- /dev/null +++ b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java @@ -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. + */ +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( + "./config/defaultNodeVeiwers.cfg"); + private static final File defaultConnectionFile = new File( + "./config/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; + + /** + * @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(); + } + 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) { + try { + + return zooKeeper.getChildren(nodePath, false); + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving children of node: " + + nodePath, e); + } + } + 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) { + try { + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + return this.zooKeeper.getChildren(nodePath, false).get( + childIndex); + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving child " + childIndex + + " of node: " + nodePath, e); + } + } + 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.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 { + return loadNodeViewersFile(defaultNodeViewersFile); + } + + /* + * (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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java new file mode 100644 index 0000000..0c62547 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java new file mode 100644 index 0000000..9718c1d --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java new file mode 100644 index 0000000..d9fdf5c --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java b/src/contrib/zooinspector/src/java/org/apache/zookeeper/retry/ZooKeeperRetry.java new file mode 100644 index 0000000..ce959a1 --- /dev/null +++ b/src/contrib/zooinspector/src/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/src/contrib/zooinspector/zooInspector-dev.sh b/src/contrib/zooinspector/zooInspector-dev.sh new file mode 100644 index 0000000..5047204 --- /dev/null +++ b/src/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/zookeeper-dev-ZooInspector.jar:../../../build/lib/log4j-1.2.15.jar:lib/zookeeper-3.3.0.jar:lib/jtoaster-1.0.4.jar:lib org.apache.zookeeper.inspector.ZooInspector \ No newline at end of file diff --git a/src/contrib/zooinspector/zooInspector.cmd b/src/contrib/zooinspector/zooInspector.cmd new file mode 100644 index 0000000..4fa3ab2 --- /dev/null +++ b/src/contrib/zooinspector/zooInspector.cmd @@ -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/log4j-1.2.15.jar;lib/zookeeper-3.3.0.jar;lib/jToaster-1.0.4.jar;lib org.apache.zookeeper.inspector.ZooInspector \ No newline at end of file diff --git a/src/contrib/zooinspector/zooInspector.sh b/src/contrib/zooinspector/zooInspector.sh new file mode 100644 index 0000000..b35b4ca --- /dev/null +++ b/src/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 lib/New.jar:zookeeper-dev-ZooInspector.jar:lib/log4j-1.2.15.jar:;lib/zookeeper-3.3.0.jar:lib/jToaster-1.0.4.jar:lib org.apache.zookeeper.inspector.ZooInspector diff --git a/src/docs/forrest.properties b/src/docs/forrest.properties new file mode 100644 index 0000000..70cf81d --- /dev/null +++ b/src/docs/forrest.properties @@ -0,0 +1,109 @@ +# Copyright 2002-2004 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. + +############## +# Properties used by forrest.build.xml for building the website +# These are the defaults, un-comment them if you need to change them. +############## + +# Prints out a summary of Forrest settings for this project +#forrest.echo=true + +# Project name (used to name .war file) +#project.name=my-project + +# Specifies name of Forrest skin to use +#project.skin=tigris +#project.skin=pelt + +# comma separated list, file:// is supported +#forrest.skins.descriptors=http://forrest.apache.org/skins/skins.xml,file:///c:/myskins/skins.xml + +############## +# behavioural properties +#project.menu-scheme=tab_attributes +#project.menu-scheme=directories + +############## +# layout properties + +# Properties that can be set to override the default locations +# +# Parent properties must be set. This usually means uncommenting +# project.content-dir if any other property using it is uncommented + +#project.status=status.xml +#project.content-dir=src/documentation +project.configfile=${project.home}/src/documentation/conf/cli.xconf +#project.raw-content-dir=${project.content-dir}/content +#project.conf-dir=${project.content-dir}/conf +#project.sitemap-dir=${project.content-dir} +#project.xdocs-dir=${project.content-dir}/content/xdocs +#project.resources-dir=${project.content-dir}/resources +#project.stylesheets-dir=${project.resources-dir}/stylesheets +#project.images-dir=${project.resources-dir}/images +#project.schema-dir=${project.resources-dir}/schema +#project.skins-dir=${project.content-dir}/skins +#project.skinconf=${project.content-dir}/skinconf.xml +#project.lib-dir=${project.content-dir}/lib +#project.classes-dir=${project.content-dir}/classes +#project.translations-dir=${project.content-dir}/translations + +############## +# validation properties + +# This set of properties determine if validation is performed +# Values are inherited unless overridden. +# e.g. if forrest.validate=false then all others are false unless set to true. +forrest.validate=true +forrest.validate.xdocs=${forrest.validate} +forrest.validate.skinconf=${forrest.validate} +forrest.validate.stylesheets=${forrest.validate} +forrest.validate.skins=${forrest.validate} +forrest.validate.skins.stylesheets=${forrest.validate.skins} + +# Make Forrest work with JDK6 +forrest.validate.sitemap=false + +# *.failonerror=(true|false) - stop when an XML file is invalid +forrest.validate.failonerror=true + +# *.excludes=(pattern) - comma-separated list of path patterns to not validate +# e.g. +#forrest.validate.xdocs.excludes=samples/subdir/**, samples/faq.xml +#forrest.validate.xdocs.excludes= + + +############## +# General Forrest properties + +# The URL to start crawling from +#project.start-uri=linkmap.html +# Set logging level for messages printed to the console +# (DEBUG, INFO, WARN, ERROR, FATAL_ERROR) +#project.debuglevel=ERROR +# Max memory to allocate to Java +#forrest.maxmemory=64m +# Any other arguments to pass to the JVM. For example, to run on an X-less +# server, set to -Djava.awt.headless=true +#forrest.jvmargs= +# The bugtracking URL - the issue number will be appended +#project.bugtracking-url=http://issues.apache.org/bugzilla/show_bug.cgi?id= +#project.bugtracking-url=http://issues.apache.org/jira/browse/ +# The issues list as rss +#project.issues-rss-url= +#I18n Property only works for the "forrest run" target. +#project.i18n=true + +project.required.plugins=org.apache.forrest.plugin.output.pdf,org.apache.forrest.plugin.input.simplifiedDocbook diff --git a/src/docs/src/documentation/README.txt b/src/docs/src/documentation/README.txt new file mode 100644 index 0000000..9bc261b --- /dev/null +++ b/src/docs/src/documentation/README.txt @@ -0,0 +1,7 @@ +This is the base documentation directory. + +skinconf.xml # This file customizes Forrest for your project. In it, you + # tell forrest the project name, logo, copyright info, etc + +sitemap.xmap # Optional. This sitemap is consulted before all core sitemaps. + # See http://forrest.apache.org/docs/project-sitemap.html diff --git a/src/docs/src/documentation/TODO.txt b/src/docs/src/documentation/TODO.txt new file mode 100644 index 0000000..84e7dfa --- /dev/null +++ b/src/docs/src/documentation/TODO.txt @@ -0,0 +1,227 @@ +This is a running list of todo documentation items. Feel free +to add to the list or take on an item as you wish (in the form +of a JIRA patch of course). +------------------------------------------------------------- + +recipes.xml:110: +[maybe an illustration would be nice for each recipe?] + +recipes.xml:167: +"wait for each watch event". [how do you wait?] + +recipes.xml:457: +[tbd: myabe helpful to indicate which step this refers to?] + +zookeeperAdmin.xml:77: +because requires a majority [tbd: why?], it is best to use... + +zookeeperAdmin.xml:112: + $yinst -i jdk-1.6.0.00_3 -br test [y! prop - replace with open equiv] + +zookeeperAdmin.xml:99: +- use a maximum heap size of 3GB for a 4GB machine. [tbd: where would they do this? Environment variable, etc?] + +zookeeperAdmin.xml:120 +$ yinst install -nostart zookeeper_server [Y! prop - replace with open eq] + +zookeeperAdmin.xml:171: +In Java, you can run the following command to execute simple operations: [tbd: also, maybe give some of those simple operations?] + +zookeeperAdmin.xml:194: +Running either program gives you a shell in which to execute simple file-system-like operations. [tbd: again, sample + operations?] + +zookeeperAdmin.xml:252: +If servers use different configuration files, +care must be taken to ensure that the list of servers in all of the +standard form, with legal values, etc] + +zookeeperAdmin.xml:408: +(Note: The system property has no zookeeper +prefix, and the configuration variable name is different from +the system property. Yes - it's not consistent, and it's +annoying. [tbd: is there any explanation for +this?]) + +zookeeperAdmin.xml:445: When the election algorithm is + "0" a UDP port with the same port number as the port listed in + the server.num option will be + used. [tbd: should that be server.id? Also, why isn't server.id + documented anywhere?] + +zookeeperAdmin.xml:481: The default to this option is yes, which + means that a leader will accept client connections. + [tbd: how do you specifiy which server is the + leader?] + +zookeeperAdmin.xml:495 When the server + starts up, it determines which server it is by looking for the + file myid in the data directory. + [tdb: should we mention somewhere about creating this file, + myid, in the setup procedure?] + +zookeeperAdmin.xml:508: [tbd: is the next sentence explanation an of what the + election port or is it a description of a special case?] + If you want to test multiple servers on a single + machine, the individual choices of electionPort for each + server can be defined in each server's config files using the + line electionPort=xxxx to avoid clashes. + +zookeeperAdmin.xml:524: If followers fall too far behind a + leader, they will be dropped. [tbd: is this a correct + rewording: if followers fall beyond this limit, they are + dropped?] + +zookeeperAdmin.xml:551: ZooKeeper will not require updates + to be synced to the media. [tbd: useful because..., + dangerous because...] + +zookeeperAdmin.xml:580: Skips ACL checks. [tbd: when? where?] + +zookeeperAdmin.xml:649: [tbd: Patrick, Ben, et al: I believe the Message Broker + team does perform routine monitoring of Zookeeper. But I might be + wrong. To your knowledge, is there any monitoring of a Zookeeper + deployment that will a Zookeeper sys admin will want to do, outside of + Yahoo?] + +zookeeperAdmin.xml:755: Also, + the server lists in each Zookeeper server configuration file + should be consistent with one another. [tbd: I'm assuming + this last part is true. Is it?] + +zookeeperAdmin.xml:812: For best results, take note of the following list of good + Zookeeper practices. [tbd: I just threw this section in. Do we + have list that is is different from the "things to avoid"? If not, I can + easily remove this section.] + + +zookeeperOver.xml:162: Ephemeral nodes are useful when you + want to implement [tbd]. + +zookeeperOver.xml:174: And if the + connection between the client and one of the Zoo Keeper servers is + broken, the client will receive a local notification. These can be used + to [tbd] + +zookeeperOver.xml:215: For more information on these (guarantees), and how they can be used, see + [tbd] + +zookeeperOver.xml:294: shows the high-level components + of the ZooKeeper service. With the exception of the request processor, + [tbd: where does the request processor live?] + +zookeeperOver.xml:298: 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 components. [tbd: I changed the wording in this + sentence from the white paper. Can someone please make sure it is still + correct?] + +zookeeperOver.xml:342: 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. Some + distributed applications have used it to: [tbd: add uses from + white paper and video presentation.] + + +zookeeperProgrammers.xml:94: + + [tbd] + + +zookeeperProgrammers.xml:115: Also, + the Simple Programmming + Example [tbd] is helpful for understand the basic + structure of a ZooKeeper client application. + +zookeeperProgrammers.xml:142: The following characters are not + allowed because [tbd: + do we need reasons?] + +zookeeperProgrammers.xml:172: If + the version it supplies doesn't match the actual version of the data, + the update will fail. (This behavior can be overridden. For more + information see... )[tbd... reference here to the section + describing the special version number -1] + +zookeeperProgrammers.xml:197: More information about watches can be + found in the section + + Zookeeper Watches. + [tbd: fix this link] [tbd: Ben there is note from to emphasize + that "it is queued". What is "it" and is what we have here + sufficient?] + +zookeeperProgrammers.xml:335: 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. [tbd: note from Ben: + "perhaps capability is a better word." need clarification on that.] + + +zookeeperProgrammers.xml:601: Locks + [tbd:...] in Zookeeper Recipes. + [tbd:..]). + +zookeeperProgrammers.xml:766: See INSTALL for general information about running + configure. [tbd: what + is INSTALL? a directory? a file?] + + + +zookeeperProgrammers.xml:813: To verify that the node's been created: + + You should see a list of node who are children of the root node + "/".[tbd: document all the cli commands (I think this is ben's comment) + +zookeeperProgrammers.xml:838: Refer to for examples of usage in Java and C. + [tbd] + +zookeeperProgrammers.xml 847: [tbd: This is a new section. The below + is just placeholder. Eventually, a subsection on each of those operations, with a little + bit of illustrative code for each op.] + +zookeeperProgrammers.xml:915: Program Structure, with Simple Example + +zookeeperProgrammers.xml:999: ZooKeeper Whitepaper [tbd: find url] + +zookeeperProgrammers.xml:1008: API Reference [tbd: find url] + +zookeeperProgrammers.xml:1062: [tbd] + Any other good sources anyone can think of... + + +zookeeperStarted.xml:73: [tbd: should we start w/ a word here about were to get the source, + exactly what to download, how to unpack it, and where to put it? Also, + does the user need to be in sudo, or can they be under their regular + login?] + +zookeeperStarted.xml:84: This should generate a JAR file called zookeeper.jar. To start + Zookeeper, compile and run zookeeper.jar. [tbd, some more + instruction here. Perhaps a command line? Are these two steps or + one?] + +zookeeperStarted.xml:139: ZooKeeper logs messages using log4j -- more detail available in + the Logging + section of the Programmer's Guide.[tbd: + real reference needed] + +zookeeperStarted.xml:201: The C bindings exist in two variants: single + threaded and multi-threaded. These differ only in how the messaging loop + is done. [tbd: what is the messaging loop? Do we talk about it + anywyhere? is this too much info for a getting started guide?] + +zookeeperStarted.xml:217: The entry syncLimit limits how far out of date a server can + be from a leader. [TBD: someone please verify that the previous is + true.] + +zookeeperStarted.xml:232: These are the "electionPort" numbers of the servers (as opposed to + clientPorts), that is ports for [tbd: feedback need: what are + these ports, exactly?] + +zookeeperStarted.xml:258: [tbd: what is the other config param? + (I believe two are mentioned above.)] diff --git a/src/docs/src/documentation/classes/CatalogManager.properties b/src/docs/src/documentation/classes/CatalogManager.properties new file mode 100644 index 0000000..ac060b9 --- /dev/null +++ b/src/docs/src/documentation/classes/CatalogManager.properties @@ -0,0 +1,37 @@ +# Copyright 2002-2004 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. + +#======================================================================= +# CatalogManager.properties +# +# This is the default properties file for Apache Forrest. +# This facilitates local configuration of application-specific catalogs. +# +# See the Apache Forrest documentation: +# http://forrest.apache.org/docs/your-project.html +# http://forrest.apache.org/docs/validation.html + +# verbosity ... level of messages for status/debug +# See forrest/src/core/context/WEB-INF/cocoon.xconf + +# catalogs ... list of additional catalogs to load +# (Note that Apache Forrest will automatically load its own default catalog +# from src/core/context/resources/schema/catalog.xcat) +# use full pathnames +# pathname separator is always semi-colon (;) regardless of operating system +# directory separator is always slash (/) regardless of operating system +# +#catalogs=/home/me/forrest/my-site/src/documentation/resources/schema/catalog.xcat +catalogs= + diff --git a/src/docs/src/documentation/conf/cli.xconf b/src/docs/src/documentation/conf/cli.xconf new file mode 100644 index 0000000..c671340 --- /dev/null +++ b/src/docs/src/documentation/conf/cli.xconf @@ -0,0 +1,328 @@ + + + + + + + + . + WEB-INF/cocoon.xconf + ../tmp/cocoon-work + ../site + + + + + + + + + + + + + + + index.html + + + + + + + */* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/docs/src/documentation/content/xdocs/bookkeeperConfig.xml b/src/docs/src/documentation/content/xdocs/bookkeeperConfig.xml new file mode 100644 index 0000000..7a80949 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/bookkeeperConfig.xml @@ -0,0 +1,156 @@ + + + +
      + BookKeeper Administrator's Guide + + Setup Guide + + + + 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 document contains information about deploying, administering + and mantaining BookKeeper. It also discusses best practices and common + problems. + + As BookKeeper is still a prototype, this article is likely to change + significantly over time. + + + + +
      + Deployment + + This section contains information about deploying BookKeeper and + covers these topics: + + + + + + + + + + + + + + + + The first section tells you how many machines you need. The second explains how to bootstrap bookies + (BookKeeper storage servers). The third section explains how we use ZooKeeper and our requirements with + respect to ZooKeeper. + + +
      + System requirements + A typical BookKeeper installation comprises a set of bookies and a set of ZooKeeper replicas. The exact number of bookies + depends on the quorum mode, desired throughput, and number of clients using this installation simultaneously. The minimum number of + bookies is three for self-verifying (stores a message authentication code along with each entry) and four for generic (does not + store a message authentication codewith each entry), and there is no upper limit on the number of bookies. Increasing the number of + bookies, in fact, enables higher throughput. + + + For performance, we require each server to have at least two disks. It is possible to run a bookie with a single disk, but + performance will be significantly lower in this case. Of course, it works with one disk, but performance is significantly lower. + + + For ZooKeeper, there is no constraint with respect to the number of replicas. Having a single machine running ZooKeeper + in standalone mode is sufficient for BookKeeper. For resilience purposes, it might be a good idea to run ZooKeeper in quorum + mode with multiple servers. Please refer to the ZooKeeper documentation for detail on how to configure ZooKeeper with multiple + replicas + +
      + +
      + Running bookies + + To run a bookie, we execute the following command: + + + + java -cp .:./zookeeper-<version>-bookkeeper.jar:./zookeeper-<version>.jar\ + :../log4j/apache-log4j-1.2.15/log4j-1.2.15.jar -Dlog4j.configuration=log4j.properties\ + org.apache.bookkeeper.proto.BookieServer 3181 127.0.0.1:2181 /path_to_log_device/\ + /path_to_ledger_device/ + + + + The parameters are: + + + + + + Port number that the bookie listens on; + + + + + + Comma separated list of ZooKeeper servers with a hostname:port format; + + + + + + Path for Log Device (stores bookie write-ahead log); + + + + + + Path for Ledger Device (stores ledger entries); + + + + + + Ideally, /path_to_log_device/ and /path_to_ledger_device/ are each + in a different device. + +
      + +
      + ZooKeeper Metadata + + For BookKeeper, we require a ZooKeeper installation to store metadata, and to pass the list + of ZooKeeper servers as parameter to the constructor of the BookKeeper class ( + org.apache.bookkeeper.client,BookKeeper). + To setup ZooKeeper, please check the + ZooKeeper documentation. + +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/bookkeeperOverview.xml b/src/docs/src/documentation/content/xdocs/bookkeeperOverview.xml new file mode 100644 index 0000000..cdc1878 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/bookkeeperOverview.xml @@ -0,0 +1,419 @@ + + + + +
      + BookKeeper overview + + + + 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 guide contains detailed information about using BookKeeper + for logging. It discusses the basic operations BookKeeper supports, + and how to create logs and perform basic read and write operations on these + logs. + + +
      + BookKeeper overview + +
      + BookKeeper introduction + + BookKeeper is a replicated service to reliably log streams of records. In BookKeeper, + servers are "bookies", log streams are "ledgers", and each unit of a log (aka record) is a + "ledger entry". BookKeeper is designed to be reliable; bookies, the servers that store + ledgers, can crash, corrupt data, discard data, but as long as there are enough bookies + behaving correctly the service as a whole behaves correctly. + + + + The initial motivation for BookKeeper comes from the namenode of HDFS. Namenodes have to + log operations in a reliable fashion so that recovery is possible in the case of crashes. + We have found the applications for BookKeeper extend far beyond HDFS, however. Essentially, + any application that requires an append storage can replace their implementations with + BookKeeper. BookKeeper has the advantage of scaling throughput with the number of servers. + + + + At a high level, a bookkeeper client receives entries from a client application and stores it to + sets of bookies, and there are a few advantages in having such a service: + + + + + + We can use hardware that is optimized for such a service. We currently believe that such a + system has to be optimized only for disk I/O; + + + + + + We can have a pool of servers implementing such a log system, and shared among a number of servers; + + + + + + We can have a higher degree of replication with such a pool, which makes sense if the hardware necessary for it is cheaper compared to the one the application uses. + + + + +
      + +
      + In slightly more detail... + + BookKeeper implements highly available logs, and it has been designed with write-ahead logging in mind. Besides high availability + due to the replicated nature of the service, it provides high throughput due to striping. As we write entries in a subset of bookies of an + ensemble and rotate writes across available quorums, we are able to increase throughput with the number of servers for both reads and writes. + Scalability is a property that is possible to achieve in this case due to the use of quorums. Other replication techniques, such as + state-machine replication, do not enable such a property. + + + An application first creates a ledger before writing to bookies through a local BookKeeper client instance. + Upon creating a ledger, a BookKeeper client writes metadata about the ledger to ZooKeeper. Each ledger currently + has a single writer. This writer has to execute a close ledger operation before any other client can read from it. + If the writer of a ledger does not close a ledger properly because, for example, it has crashed before having the + opportunity of closing the ledger, then the next client that tries to open a ledger executes a procedure to recover + it. As closing a ledger consists essentially of writing the last entry written to a ledger to ZooKeeper, the recovery + procedure simply finds the last entry written correctly and writes it to ZooKeeper. + + + + Note that currently this recovery procedure is executed automatically upon trying to open a ledger and no explicit action is necessary. + Although two clients may try to recover a ledger concurrently, only one will succeed, the first one that is able to create the close znode + for the ledger. + +
      + +
      + Bookkeeper elements and concepts + + BookKeeper uses four basic elements: + + + + + + Ledger: A ledger is a sequence of entries, and each entry is a sequence of bytes. Entries are + written sequentially to a ledger and at most once. Consequently, ledgers have an append-only semantics; + + + + + + BookKeeper client: A client runs along with a BookKeeper application, and it enables applications + to execute operations on ledgers, such as creating a ledger and writing to it; + + + + + + Bookie: A bookie is a BookKeeper storage server. Bookies store the content of ledgers. For any given + ledger L, we call an ensemble the group of bookies storing the content of L. For performance, we store on + each bookie of an ensemble only a fragment of a ledger. That is, we stripe when writing entries to a ledger such that + each entry is written to sub-group of bookies of the ensemble. + + + + + + Metadata storage service: BookKeeper requires a metadata storage service to store information related + to ledgers and available bookies. We currently use ZooKeeper for such a task. + + + +
      + +
      + Bookkeeper initial design + + A set of bookies implements BookKeeper, and we use a quorum-based protocol to replicate data across the bookies. + There are basically two operations to an existing ledger: read and append. Here is the complete API list + (mode detail + here): + + + + + + Create ledger: creates a new empty ledger; + + + + + + Open ledger: opens an existing ledger for reading; + + + + + + Add entry: adds a record to a ledger either synchronously or asynchronously; + + + + + + Read entries: reads a sequence of entries from a ledger either synchronously or asynchronously + + + + + + There is only a single client that can write to a ledger. Once that ledger is closed or the client fails, + no more entries can be added. (We take advantage of this behavior to provide our strong guarantees.) + There will not be gaps in the ledger. Fingers get broken, people get roughed up or end up in prison when + books are manipulated, so there is no deleting or changing of entries. + + +
      + BookKeeper Overview + + + + + + +
      + + + A simple use of BooKeeper is to implement a write-ahead transaction log. A server maintains an in-memory data structure + (with periodic snapshots for example) and logs changes to that structure before it applies the change. The application + server creates a ledger at startup and store the ledger id and password in a well known place (ZooKeeper maybe). When + it needs to make a change, the server adds an entry with the change information to a ledger and apply the change when + BookKeeper adds the entry successfully. The server can even use asyncAddEntry to queue up many changes for high change + throughput. BooKeeper meticulously logs the changes in order and call the completion functions in order. + + + + When the application server dies, a backup server will come online, get the last snapshot and then it will open the + ledger of the old server and read all the entries from the time the snapshot was taken. (Since it doesn't know the + last entry number it will use MAX_INTEGER). Once all the entries have been processed, it will close the ledger and + start a new one for its use. + + + + A client library takes care of communicating with bookies and managing entry numbers. An entry has the following fields: + + + Entry fields + + + + + + + + Field + Type + Description + + + + + Ledger number + long + The id of the ledger of this entry + + + Entry number + long + The id of this entry + + + + + last confirmed (LC) + long + id of the last recorded entry + + + data + byte[] + the entry data (supplied by application) + + + authentication code + byte[] + Message authentication code that includes all other fields of the entry + + + + +
      + + + The client library generates a ledger entry. None of the fields are modified by the bookies and only the first three + fields are interpreted by the bookies. + + + + To add to a ledger, the client generates the entry above using the ledger number. The entry number will be one more + than the last entry generated. The LC field contains the last entry that has been successfully recorded by BookKeeper. + If the client writes entries one at a time, LC is the last entry id. But, if the client is using asyncAddEntry, there + may be many entries in flight. An entry is considered recorded when both of the following conditions are met: + + + + + + the entry has been accepted by a quorum of bookies + + + + + + all entries with a lower entry id have been accepted by a quorum of bookies + + + + + + LC seems mysterious right now, but it is too early to explain how we use it; just smile and move on. + + + + Once all the other fields have been field in, the client generates an authentication code with all of the previous fields. + The entry is then sent to a quorum of bookies to be recorded. Any failures will result in the entry being sent to a new + quorum of bookies. + + + + To read, the client library initially contacts a bookie and starts requesting entries. If an entry is missing or + invalid (a bad MAC for example), the client will make a request to a different bookie. By using quorum writes, + as long as enough bookies are up we are guaranteed to eventually be able to read an entry. + + +
      + +
      + Bookkeeper metadata management + + + There are some meta data that needs to be made available to BookKeeper clients: + + + + + + The available bookies; + + + + + + The list of ledgers; + + + + + + The list of bookies that have been used for a given ledger; + + + + + + The last entry of a ledger; + + + + + + We maintain this information in ZooKeeper. Bookies use ephemeral nodes to indicate their availability. Clients + use znodes to track ledger creation and deletion and also to know the end of the ledger and the bookies that + were used to store the ledger. Bookies also watch the ledger list so that they can cleanup ledgers that get deleted. + + +
      + +
      + Closing out ledgers + + + The process of closing out the ledger and finding the last ledger is difficult due to the durability guarantees of BookKeeper: + + + + + + If an entry has been successfully recorded, it must be readable. + + + + + + If an entry is read once, it must always be available to be read. + + + + + + If the ledger was closed gracefully, ZooKeeper will have the last entry and everything will work well. But, if the + BookKeeper client that was writing the ledger dies, there is some recovery that needs to take place. + + + + The problematic entries are the ones at the end of the ledger. There can be entries in flight when a BookKeeper client + dies. If the entry only gets to one bookie, the entry should not be readable since the entry will disappear if that bookie + fails. If the entry is only on one bookie, that doesn't mean that the entry has not been recorded successfully; the other + bookies that recorded the entry might have failed. + + + + The trick to making everything work is to have a correct idea of a last entry. We do it in roughly three steps: + + + + + Find the entry with the highest last recorded entry, LC; + + + + + + Find the highest consecutively recorded entry, LR; + + + + + + Make sure that all entries between LC and LR are on a quorum of bookies; + + + + +
      +
      +
      \ No newline at end of file diff --git a/src/docs/src/documentation/content/xdocs/bookkeeperProgrammer.xml b/src/docs/src/documentation/content/xdocs/bookkeeperProgrammer.xml new file mode 100644 index 0000000..5f330e1 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/bookkeeperProgrammer.xml @@ -0,0 +1,678 @@ + + + + +
      + BookKeeper Getting Started Guide + + + + 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 guide contains detailed information about using BookKeeper + for logging. It discusses the basic operations BookKeeper supports, + and how to create logs and perform basic read and write operations on these + logs. + + +
      + Programming with BookKeeper + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      + Instantiating BookKeeper. + + The first step to use BookKeeper is to instantiate a BookKeeper object: + + + + org.apache.bookkeeper.BookKeeper + + + + + There are three BookKeeper constructors: + + + + + public BookKeeper(String servers) + throws KeeperException, IOException + + + + + where: + + + + + servers is a comma-separated list of ZooKeeper servers. + + + + + + + public BookKeeper(ZooKeeper zk) + throws InterruptedException, KeeperException + + + + + where: + + + + + zk is a ZooKeeper object. This constructor is useful when + the application also using ZooKeeper and wants to have a single instance of ZooKeeper. + + + + + + + + public BookKeeper(ZooKeeper zk, ClientSocketChannelFactory channelFactory) + throws InterruptedException, KeeperException + + + + + where: + + + + + zk is a ZooKeeper object. This constructor is useful when + the application also using ZooKeeper and wants to have a single instance of ZooKeeper. + + + + + + channelFactory is a netty channel object + (org.jboss.netty.channel.socket). + + + + + + +
      + +
      + Creating a ledger. + + Before writing entries to BookKeeper, it is necessary to create a ledger. + With the current BookKeeper API, it is possible to create a ledger both synchronously + or asynchronously. The following methods belong + to org.apache.bookkeeper.client.BookKeeper. + + + + Synchronous call: + + + + + public LedgerHandle createLedger(int ensSize, int qSize, DigestType type, byte passwd[]) + throws KeeperException, InterruptedException, + IOException, BKException + + + + + where: + + + + + ensSize is the number of bookies (ensemble size); + + + + + + qSize is the write quorum size; + + + + + + type is the type of digest used with entries: either MAC or CRC32. + + + + + + passwd is a password that authorizes the client to write to the + ledger being created. + + + + + + All further operations on a ledger are invoked through the LedgerHandle + object returned. + + + + As a convenience, we provide a createLedger with default parameters (3,2,VERIFIABLE), + and the only two input parameters it requires are a digest type and a password. + + + + Asynchronous call: + + + + + public void asyncCreateLedger(int ensSize, + int qSize, + DigestType type, + byte passwd[], + CreateCallback cb, + Object ctx + ) + + + + + The parameters are the same of the synchronous version, with the + exception of cb and ctx. CreateCallback + is an interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called createComplete + that has the following signature: + + + + + void createComplete(int rc, LedgerHandle lh, Object ctx); + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKException for a list); + + + + + + lh is a LedgerHandle object to manipulate a ledger; + + + + + + ctx is a control object for accountability purposes. It can be essentially any object the application is happy with. + + + + + + The ctx object passed as a parameter to the call to create a ledger + is the one same returned in the callback. + +
      + +
      + Adding entries to a ledger. + + Once we have a ledger handle lh obtained through a call to create a ledger, we + can start writing entries. As with creating ledgers, we can write both synchronously and + asynchronously. The following methods belong + to org.apache.bookkeeper.client.LedgerHandle. + + + + Synchronous call: + + + + + public long addEntry(byte[] data) + throws InterruptedException + + + + + where: + + + + + + data is a byte array; + + + + + + A call to addEntry returns the status of the operation (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + Asynchronous call: + + + + + public void asyncAddEntry(byte[] data, AddCallback cb, Object ctx) + + + + + It also takes a byte array as the sequence of bytes to be stored as an entry. Additionaly, it takes + a callback object cb and a control object ctx. The callback object must implement + the AddCallback interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called addComplete + that has the following signature: + + + + + void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx); + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + + + lh is a LedgerHandle object to manipulate a ledger; + + + + + + entryId is the identifier of entry associated with this request; + + + + + + ctx is control object used for accountability purposes. It can be any object the application is happy with. + + + +
      + +
      + Closing a ledger. + + Once a client is done writing, it closes the ledger. The following methods belong + to org.apache.bookkeeper.client.LedgerHandle. + + + Synchronous close: + + + + + public void close() + throws InterruptedException + + + + + It takes no input parameters. + + + + Asynchronous close: + + + + public void asyncClose(CloseCallback cb, Object ctx) + throws InterruptedException + + + + + It takes a callback object cb and a control object ctx. The callback object must implement + the CloseCallback interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called closeComplete + that has the following signature: + + + + + void closeComplete(int rc, LedgerHandle lh, Object ctx) + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + + + lh is a LedgerHandle object to manipulate a ledger; + + + + + + ctx is control object used for accountability purposes. + + + + +
      + +
      + Opening a ledger. + + To read from a ledger, a client must open it first. The following methods belong + to org.apache.bookkeeper.client.BookKeeper. + + + + Synchronous open: + + + + + public LedgerHandle openLedger(long lId, DigestType type, byte passwd[]) + throws InterruptedException, BKException + + + + + + + ledgerId is the ledger identifier; + + + + + + type is the type of digest used with entries: either MAC or CRC32. + + + + + + passwd is a password to access the ledger (used only in the case of VERIFIABLE ledgers); + + + + + + Asynchronous open: + + + + public void asyncOpenLedger(long lId, DigestType type, byte passwd[], OpenCallback cb, Object ctx) + + + + + It also takes a a ledger identifier and a password. Additionaly, it takes a callback object + cb and a control object ctx. The callback object must implement + the OpenCallback interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called openComplete + that has the following signature: + + + + + public void openComplete(int rc, LedgerHandle lh, Object ctx) + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + + + lh is a LedgerHandle object to manipulate a ledger; + + + + + + ctx is control object used for accountability purposes. + + + +
      + +
      + Reading from ledger + + Read calls may request one or more consecutive entries. The following methods belong + to org.apache.bookkeeper.client.LedgerHandle. + + + + Synchronous read: + + + + + public Enumeration<LedgerEntry> readEntries(long firstEntry, long lastEntry) + throws InterruptedException, BKException + + + + + + + firstEntry is the identifier of the first entry in the sequence of entries to read; + + + + + + lastEntry is the identifier of the last entry in the sequence of entries to read. + + + + + + Asynchronous read: + + + + public void asyncReadEntries(long firstEntry, + long lastEntry, ReadCallback cb, Object ctx) + throws BKException, InterruptedException + + + + + It also takes a first and a last entry identifiers. Additionaly, it takes a callback object + cb and a control object ctx. The callback object must implement + the ReadCallback interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called readComplete + that has the following signature: + + + + + void readComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> seq, Object ctx) + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + + + lh is a LedgerHandle object to manipulate a ledger; + + + + + + seq is a Enumeration<LedgerEntry> object to containing the list of entries requested; + + + + + + ctx is control object used for accountability purposes. + + + +
      + +
      + Deleting a ledger + + Once a client is done with a ledger and is sure that nobody will ever need to read from it again, they can delete the ledger. + The following methods belong to org.apache.bookkeeper.client.BookKeeper. + + + + Synchronous delete: + + + + + public void deleteLedger(long lId) throws InterruptedException, BKException + + + + + + + lId is the ledger identifier; + + + + + + Asynchronous delete: + + + + public void asyncDeleteLedger(long lId, DeleteCallback cb, Object ctx) + + + + + It takes a ledger identifier. Additionally, it takes a callback object + cb and a control object ctx. The callback object must implement + the DeleteCallback interface in org.apache.bookkeeper.client.AsyncCallback, and + a class implementing it has to implement a method called deleteComplete + that has the following signature: + + + + + void deleteComplete(int rc, Object ctx) + + + + + where: + + + + + rc is a return code (please refer to org.apache.bookeeper.client.BKDefs for a list); + + + + + + ctx is control object used for accountability purposes. + + + +
      +
      +
      \ No newline at end of file diff --git a/src/docs/src/documentation/content/xdocs/bookkeeperStarted.xml b/src/docs/src/documentation/content/xdocs/bookkeeperStarted.xml new file mode 100644 index 0000000..74f6f7e --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/bookkeeperStarted.xml @@ -0,0 +1,208 @@ + + + + +
      + BookKeeper Getting Started Guide + + + + 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 guide contains detailed information about using BookKeeper + for logging. It discusses the basic operations BookKeeper supports, + and how to create logs and perform basic read and write operations on these + logs. + + +
      + Getting Started: Setting up BookKeeper to write logs. + + This document contains information to get you started quickly with + BookKeeper. It is aimed primarily at developers willing to try it out, and + contains simple installation instructions for a simple BookKeeper installation + and a simple programming example. For further programming detail, please refer to + BookKeeper Programmer's Guide. + + +
      + Pre-requisites + See + System Requirements in the Admin guide. +
      + +
      + Download + BookKeeper is distributed along with ZooKeeper. To get a ZooKeeper distribution, + download a recent + + stable release from one of the Apache Download + Mirrors. +
      + +
      + LocalBookKeeper + Under org.apache.bookkeeper.util, you'll find a java program + called LocalBookKeeper.java that sets you up to run BookKeeper on a + single machine. This is far from ideal from a performance perspective, + but the program is useful for both test and educational purposes. + +
      + +
      + Setting up bookies + If you're bold and you want more than just running things locally, then + you'll need to run bookies in different servers. You'll need at least three bookies + to start with. + + + + For each bookie, we need to execute a command like the following: + + + + java -cp .:./zookeeper-<version>-bookkeeper.jar:./zookeeper-<version>.jar\ + :lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar -Dlog4j.configuration=log4j.properties\ + org.apache.bookkeeper.proto.BookieServer 3181 127.0.0.1:2181 /path_to_log_device/\ + /path_to_ledger_device/ + + + "/path_to_log_device/" and "/path_to_ledger_device/" are different paths. Also, port 3181 + is the port that a bookie listens on for connection requests from clients. 127.0.0.1:2181 is the hostname:port + for the ZooKeeper server. In this example, the standalone ZooKeeper server is running locally on port 2181. + If we had multiple ZooKeeper servers, this parameter would be a comma separated list of all the hostname:port + values corresponding to them. + +
      + +
      + Setting up ZooKeeper + ZooKeeper stores metadata on behalf of BookKeeper clients and bookies. To get a minimal + ZooKeeper installation to work with BookKeeper, we can set up one server running in + standalone mode. Once we have the server running, we need to create a few znodes: + + + + + + /ledgers + + + + + + /ledgers/available + + + + + For each bookie, we add one znode such that the name of the znode is the + concatenation of the machine name and the port number that the bookie is + listening on. For example, if a bookie is running on bookie.foo.com an is listening + on port 3181, we add a znode + /ledgers/available/bookie.foo.com:3181. + + + +
      + +
      + Example + + In the following excerpt of code, we: + + + + + + Create a ledger; + + + + + + Write to the ledger; + + + + + + Close the ledger; + + + + + + Open the same ledger for reading; + + + + + + Read from the ledger; + + + + + + Close the ledger again; + + + + + +LedgerHandle lh = bkc.createLedger(ledgerPassword); +ledgerId = lh.getId(); +ByteBuffer entry = ByteBuffer.allocate(4); + +for(int i = 0; i < 10; i++){ + entry.putInt(i); + entry.position(0); + entries.add(entry.array()); + lh.addEntry(entry.array()); +} +lh.close(); +lh = bkc.openLedger(ledgerId, ledgerPassword); + +Enumeration<LedgerEntry> ls = lh.readEntries(0, 9); +int i = 0; +while(ls.hasMoreElements()){ + ByteBuffer origbb = ByteBuffer.wrap( + entries.get(i++)); + Integer origEntry = origbb.getInt(); + ByteBuffer result = ByteBuffer.wrap( + ls.nextElement().getEntry()); + + Integer retrEntry = result.getInt(); +} +lh.close(); + +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/bookkeeperStream.xml b/src/docs/src/documentation/content/xdocs/bookkeeperStream.xml new file mode 100644 index 0000000..9db605a --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/bookkeeperStream.xml @@ -0,0 +1,331 @@ + + + + +
      + Streaming with BookKeeper + + + + 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 guide contains detailed information about using how to stream bytes + on top of BookKeeper. It essentially motivates and discusses the basic stream + operations currently supported. + + +
      + Summary + + + When using the BookKeeper API, an application has to split the data to write into entries, each + entry being a byte array. This is natural for many applications. For example, when using BookKeeper + for write-ahead logging, an application typically wants to write the modifications corresponding + to a command or a transaction. Some other applications, however, might not have a natural boundary + for entries, and may prefer to write and read streams of bytes. This is exactly the purpose of the + stream API we have implemented on top of BookKeeper. + + + + The stream API is implemented in the package Streaming, and it contains two main classes: LedgerOutputStream and + LedgerInputStream. The class names are indicative of what they do. + +
      + +
      + Writing a stream of bytes + + Class LedgerOutputStream implements two constructors and five public methods: + + + + + public LedgerOutputStream(LedgerHandle lh) + + + + + where: + + + + + lh is a ledger handle for a previously created and open ledger. + + + + + + + public LedgerOutputStream(LedgerHandle lh, int size) + + + + + where: + + + + + lh is a ledger handle for a previously created and open ledger. + + + + + + size is the size of the byte buffer to store written bytes before flushing. + + + + + + + Closing a stream. This call closes the stream by flushing the write buffer. + + + + public void close() + + + + + which has no parameters. + + + + Flushing a stream. This call essentially flushes the write buffer. + + + + public synchronized void flush() + + + + + which has no parameters. + + + + Writing bytes. There are three calls for writing bytes to a stream. + + + + + public synchronized void write(byte[] b) + + + + + where: + + + + + b is an array of bytes to write. + + + + + + + public synchronized void write(byte[] b, int off, int len) + + + + + where: + + + + + b is an array of bytes to write. + + + + + + off is a buffer offset. + + + + + + len is the length to write. + + + + + + + public synchronized void write(int b) + + + + + where: + + + + + b contains a byte to write. The method writes the least significant byte of the integer four bytes. + + + +
      + +
      + Reading a stream of bytes + + + Class LedgerOutputStream implements two constructors and four public methods: + + + + + public LedgerInputStream(LedgerHandle lh) + throws BKException, InterruptedException + + + + + where: + + + + + lh is a ledger handle for a previously created and open ledger. + + + + + + + public LedgerInputStream(LedgerHandle lh, int size) + throws BKException, InterruptedException + + + + + where: + + + + + lh is a ledger handle for a previously created and open ledger. + + + + + + size is the size of the byte buffer to store bytes that the application + will eventually read. + + + + + + Closing. There is one call to close an input stream, but the call + is currently empty and the application is responsible for closing the ledger handle. + + + + public void close() + + + + + which has no parameters. + + + + Reading. There are three calls to read from the stream. + + + + public synchronized int read() + throws IOException + + + + + which has no parameters. + + + + + public synchronized int read(byte[] b) + throws IOException + + + + + where: + + + + + b is a byte array to write to. + + + + + + + + public synchronized int read(byte[] b, int off, int len) + throws IOException + + + + + where: + + + + + b is a byte array to write to. + + + + + + off is an offset for byte array b. + + + + + + len is the length in bytes to write to b. + + + + + +
      +
      \ No newline at end of file diff --git a/src/docs/src/documentation/content/xdocs/index.xml b/src/docs/src/documentation/content/xdocs/index.xml new file mode 100644 index 0000000..8ed4702 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/index.xml @@ -0,0 +1,98 @@ + + + + + + + +
      + 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 or browse the + archives. +

      +
        + +
      • ZooKeeper Overview

        Technical Overview Documents for Client Developers, Adminstrators, and Contributors

        +
        • Overview - a bird's eye view of ZooKeeper, including design concepts and architecture
        • +
        • Getting Started - a tutorial-style guide for developers to install, run, and program to ZooKeeper
        • +
        • Release Notes - new developer and user facing features, improvements, and incompatibilities
        • +
        +
      • + +
      • Developers

        Documents for Developers using the ZooKeeper Client API

        + +
      • + +
      • Administrators & Operators

        Documents for Administrators and Operations Engineers of ZooKeeper Deployments

        +
          +
        • Administrator's Guide - a guide for system administrators and anyone else who might deploy ZooKeeper
        • +
        • Quota Guide - a guide for system administrators on Quotas in ZooKeeper.
        • +
        • JMX - how to enable JMX in ZooKeeper
        • +
        • Hierarchical quorums
        • +
        • Observers - non-voting ensemble members that easily improve ZooKeeper's scalability
        • +
        +
      • + +
      • Contributors

        Documents for Developers Contributing to the ZooKeeper Open Source Project

        + +
      • + +
      • Miscellaneous ZooKeeper Documentation + +
      • + +
      • BookKeeper Documentation +

        BookKeeper is a highly-available system that implements high-performance write-ahead logging. It uses ZooKeeper for metadata, + which is the main reason for being a ZooKeeper contrib. +

        + +
      • +
      + + +
      diff --git a/src/docs/src/documentation/content/xdocs/javaExample.xml b/src/docs/src/documentation/content/xdocs/javaExample.xml new file mode 100644 index 0000000..c992282 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/javaExample.xml @@ -0,0 +1,663 @@ + + + + +
      + ZooKeeper Java Example + + + + 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 article contains sample Java code for a simple watch client. + + + + +
      + 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 ZooKeeper node 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 + then name of a znode - the one to be watched + an executable with arguments. + It fetches the data associated with the znode and starts the executable. + If the znode changes, the client refetches 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 exectuable 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 + . + + + // 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 communciates generic events that the main thread would be intersted 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 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 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 + zk.exists(znode, true, this, null); +} + + +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. + + +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 +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/src/docs/src/documentation/content/xdocs/recipes.xml b/src/docs/src/documentation/content/xdocs/recipes.xml new file mode 100644 index 0000000..ead041b --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/recipes.xml @@ -0,0 +1,637 @@ + + + + +
      + ZooKeeper Recipes and Solutions + + + + 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 guide contains pseudocode and guidelines for using Zookeeper to + solve common problems in Distributed Application Coordination. It + discusses such problems as event handlers, queues, and locks.. + + $Revision: 1.6 $ $Date: 2008/09/19 03:46:18 $ + + + +
      + 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. Hopfully 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. + + +
      + 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: + + + + Client calls the ZooKeeper API's exists() function on the barrier node, with + watch set to true. + + + + If exists() returns false, the + barrier is gone and the client proceeds + + + + Else, if exists() returns true, + the clients wait for a watch event from ZooKeeper for the barrier + node. + + + + 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 + + + + + + Create a name n = + b+“/â€+p + + + + Set watch: exists(b + ‘‘/ready’’, + true) + + + + Create child: create( + n, EPHEMERAL) + + + + L = getChildren(b, + false) + + + + if fewer children in L than + x, wait for watch event + + + + else create(b + ‘‘/ready’’, + REGULAR) + + + + + + L = getChildren(b, + false) + + + + if no children, exit + + + + if p is only process node in + L, delete(n) and exit + + + + if p is the lowest process + node in L, wait on highest process node in L + + + + else delete(n) if + still exists and wait on lowest process node in L + + + + 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. + + + There now exists a Queue implementation in ZooKeeper + recipes directory. This is distributed with the release -- + src/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 . 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. + + + There now exists a Lock implementation in ZooKeeper + recipes directory. This is distributed with the release -- + src/recipes/lock directory of the release artifact. + + + + Clients wishing to obtain a lock do the following: + + + + Call create( ) with a pathname + of "_locknode_/lock-" and the sequence and + ephemeral flags set. + + + + Call getChildren( ) on the lock + node without setting the watch flag (this is + important to avoid the herd effect). + + + + If the pathname created in step 1 has the lowest sequence number suffix, the + client has the lock and the client exits the protocol. + + + + The client calls exists( ) with + the watch flag set on the path in the lock directory with the next + lowest sequence number. + + + + if exists( ) returns false, 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. + + + +
      + Shared Locks + + You can implement shared locks by with a few changes to the lock + protocol: + + + + + + Obtaining a read + lock: + + Obtaining a write + lock: + + + + + + Call create( ) to + create a node with pathname + "_locknode_/read-". This is the + lock node use later in the protocol. Make sure to set both + the sequence and + ephemeral flags. + + + + Call getChildren( ) + on the lock node without setting the + watch flag - this is important, as it + avoids the herd effect. + + + + 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. + + + + Otherwise, call exists( + ), with watch flag, set on + the node in lock directory with pathname staring with + "write-" having the next lowest + sequence number. + + + + If exists( ) + returns false, goto step 2. + + + + Otherwise, wait for a notification for the pathname + from the previous step before going to step 2 + + + + + + Call create( ) to + create a node with pathname + "_locknode_/write-". This is the + lock node spoken of later in the protocol. Make sure to + set both sequence and + ephemeral flags. + + + + Call getChildren( ) + on the lock node without + setting the watch flag - this is + important, as it avoids the herd effect. + + + + 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. + + + + Call exists( ), + with watch flag set, on the node with + the pathname that has the next lowest sequence + number. + + + + If exists( ) + returns false, goto step 2. Otherwise, wait for a + notification for the pathname from the previous step + before going to step 2. + + + + + + + + + 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. + + +
      + +
      + Recoverable 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/n_" + with both flags SEQUENCE|EPHEMERAL. With the sequence flag, ZooKeeper + automatically appends a sequence number that is greater that 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 of 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: + + + + Create znode z with path "ELECTION/n_" with both SEQUENCE and + EPHEMERAL flags; + + + + Let C be the children of "ELECTION", and i be the sequence + number of z; + + + + Watch for changes on "ELECTION/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: + + + + Let C be the new set of children of ELECTION; + + + + If z is the smallest node in C, then execute leader + procedure; + + + + Otherwise, watch for changes on "ELECTION/n_j", where j is the + largest sequence number such that j < i and n_j is a znode in C; + + + + + Note that the znode having no preceding znode on the list of + children does 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. +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/site.xml b/src/docs/src/documentation/content/xdocs/site.xml new file mode 100644 index 0000000..e49d92c --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/site.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/docs/src/documentation/content/xdocs/tabs.xml b/src/docs/src/documentation/content/xdocs/tabs.xml new file mode 100644 index 0000000..aef7e59 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/tabs.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/src/docs/src/documentation/content/xdocs/zookeeperAdmin.xml b/src/docs/src/documentation/content/xdocs/zookeeperAdmin.xml new file mode 100644 index 0000000..d88ddbd --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperAdmin.xml @@ -0,0 +1,1861 @@ + + + +
      + ZooKeeper Administrator's Guide + + A Guide to Deployment and Administration + + + + 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 document contains information about deploying, administering + and mantaining ZooKeeper. It also discusses best practices and common + problems. + + + +
      + Deployment + + This section contains information about deploying Zookeeper and + covers these topics: + + + + + + + + + + + + + + + + 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.6 or greater (JDK 6 or + greater). 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. + + + As mentioned in the + ZooKeeper Getting Started Guide + , 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 setting a server that will be part of an + ensemble. These steps should be performed on every host in the + ensemble: + + + + 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 + + + + 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. + + + + Install the ZooKeeper Server Package. It can be downloaded + from: + + + + http://zookeeper.apache.org/releases.html + + + + + + 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 . 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. 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. + + 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. + + + + If your configuration file is set up, you can start a + ZooKeeper server: + + $ java -cp zookeeper.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf \ + org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.cfg + + + QuorumPeerMain starts a ZooKeeper server, + JMX + management beans are also registered which allows + management through a JMX management console. + The ZooKeeper JMX + document 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. + + + + + 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 section of the ZooKeeper Getting Started + Guide. + + For information on installing the client side libraries, refer to + the Bindings + section of the ZooKeeper + Programmer's Guide. +
      +
      + +
      + Administration + + This section contains information about running and maintaining + ZooKeeper and covers these topics: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      + Designing a ZooKeeper Deployment + + The reliablity of ZooKeeper rests on two basic assumptions. + + 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. + + 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 resourses 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 + + 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 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 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 <count> 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.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf org.apache.zookeeper.server.PurgeTxnLog <dataDir> <snapDir> -n <count> + + 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 + below. +
      + +
      + Debug Log Cleanup (log4j) + + See the section on 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 or + SMF + (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. +
      + +
      + Monitoring + + The ZooKeeper service can be monitored in one of two + primary ways; 1) the command port through the use of 4 letter words and 2) JMX. See the appropriate section for + your environment/requirements. +
      + +
      + Logging + + ZooKeeper uses log4j version 1.2 as + its logging infrastructure. 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, see + Log4j Default Initialization Procedure + 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. + +
      + 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. + + + + + dataDir + + + the location where ZooKeeper will store the in-memory + database snapshots and, unless specified otherwise, the + transaction log of updates to the database. + + + 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 + effect 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 snaphots. + + + Having a dedicated log device has a large impact on + throughput and stable latencies. It is highly recommened 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). + + + + + 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. + + + + + 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. + + + + + 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" + + + +
      + +
      + 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 "0" corresponds + to the original UDP-based version, "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. Currently, algorithm 3 is the default + + + The implementations of leader election 0, 1, and 2 are now + deprecated . We have the intention + of removing them in the next release, at which point only the + FastLeaderElection will be available. + + + + + + + initLimit + + + (No Java system property) + + Amount of time, in ticks (see 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. + + + + + leaderServes + + + (Java system property: zookeeper.leaderServes) + + Leader accepts client connections. Default value is "yes". + The leader machine coordinates updates. For higher update + throughput at thes 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. + + + 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. The leader election port is only necessary if electionAlg + is 1, 2, or 3 (default). If electionAlg is 0, then the second port is not + necessary. If you want to test multiple servers on a single machine, then + different ports can be used for each server. + + + + + syncLimit + + + (No Java system property) + + Amount of time, in ticks (see 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 + + + + + + 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 + + + + + + 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. + + + + Default value is 5 seconds. + + + + + + 4lw.commands.whitelist + + + (Java system property: zookeeper.4lw.commands.whitelist) + + New in 3.4.10: + This property contains a list of comma separated + Four Letter Words commands. It is introduced + to provide fine grained control over the set of commands ZooKeeper can execute, + so users can turn off certain commands if necessary. + By default it contains all supported four letter word commands except "wchp" and "wchc", + if the property is not specified. If the property is specified, then only commands listed + in the whitelist are enabled. + + + 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 + + + Users can also use asterisk option so they 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=* + + + + + + + ipReachableTimeout + + + (Java system property: zookeeper.ipReachableTimeout) + + New in 3.4.11: + Set this timeout value for IP addresses reachable checking when hostname is resolved, as mesured in + milliseconds. + By default, ZooKeeper will use the first IP address of the hostname(without any reachable checking). + When zookeeper.ipReachableTimeout is set(larger than 0), ZooKeeper will will try to pick up the first + IP address which is reachable. This is done by calling Java API InetAddress.isReachable(long timeout) + function, in which this timeout value is used. If none of such reachable IP address can be found, the + first IP address of the hostname will be used anyway. + + + + + + + tcpKeepAlive + + + (Java system property: zookeeper.tcpKeepAlive) + + New in 3.4.11: + 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. + + + + + + +
      + +
      + Authentication & Authorization Options + + The options in this section allow control over + authentication/authorization performed by the service. + + + + zookeeper.DigestAuthenticationProvider.superDigest + + + (Java system property only: 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:<password>". Provide the + generated "super:<data>" 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:<password>". 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. + + + + + 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. + + + + + 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 + +
      +
      +
      +
      + +
      + 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 default is + 0xfffff, or just under 1M. If this option is changed, the system + property must be set on all servers and clients otherwise + problems will arise. This is really a sanity check. ZooKeeper is + designed to store data on the order of kilobytes in size. + + + + + 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. + + + + +
      + +
      + Communication using the Netty framework + + New in + 3.4: Netty + 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. + + Prior to version 3.4 ZooKeeper has always used NIO + directly, however in versions 3.4 and later Netty is + supported as an option to NIO (replaces). NIO continues to + be the default, however Netty based communication can be + used in place of NIO by setting the environment variable + "zookeeper.serverCnxnFactory" to + "org.apache.zookeeper.server.NettyServerCnxnFactory". You + have the option of setting this on either the client(s) or + server(s), typically you would want to set this on both, + however that is at your discretion. + + + TBD - tuning options for netty - currently there are none that are netty specific but we should add some. Esp around max bound on the number of reader worker threads netty creates. + + + TBD - how to manage encryption + + + TBD - how to manage certificates + + +
      + +
      + +
      + 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. + + + + 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. This + only works on the leader. + + + + + 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. + + + + + 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 +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 +zk_fsync_threshold_exceed_count 0 + + + 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 + + + + + Here's an example of the ruok + command: + + $ echo ruok | nc 127.0.0.1 5111 +imok + + + +
      + +
      + 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 files in it: + + + + myid - contains a single integer in + human readable ASCII text that represents the server id. + + + + snapshot.<zxid> - 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 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 trouble shooting. + + 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 section in + this document for more details on setting a retention policy + and maintenance of ZooKeeper storage. + + + 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 + + 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. +
      +
      + +
      + 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 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 should mitigate it. + + + + + 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-tennant installations see the section + detailing ZooKeeper "chroot" support, this can be very useful + when deploying many applications/services interfacing to a + single ZooKeeper cluster. + +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/zookeeperHierarchicalQuorums.xml b/src/docs/src/documentation/content/xdocs/zookeeperHierarchicalQuorums.xml new file mode 100644 index 0000000..f71c4a8 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperHierarchicalQuorums.xml @@ -0,0 +1,75 @@ + + + + +
      + Introduction to hierarchical quorums + + + + 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 document contains information about 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. + +
      \ No newline at end of file diff --git a/src/docs/src/documentation/content/xdocs/zookeeperInternals.xml b/src/docs/src/documentation/content/xdocs/zookeeperInternals.xml new file mode 100644 index 0000000..4954123 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperInternals.xml @@ -0,0 +1,487 @@ + + + + +
      + ZooKeeper Internals + + + + 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 article contains topics which discuss the inner workings of + ZooKeeper. So far, that's logging and atomic broadcast. + + + + +
      + Introduction + + This document contains information on the inner workings of ZooKeeper. + So far, it discusses these topics: + + + + + + + +
      + +
      +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, it will be eventually delivered by all servers. + +Total order + If a message is +delivered before message b by one server, a will be delivered before b by all +servers. If a and b are delivered messages, either a will be delivered before b +or b will be delivered before a. + +Causal order + + +If a message b is sent after a message a has been delivered by the sender of b, +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 we achieve consensus in the presence of failures +we use timeouts. However, we rely on times for liveness not for correctness. So, +if timeouts stop working (clocks malfunction for example) 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 correspond 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 it has two parts represent the zxid 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 +included all the proposals that really have been committed? The answer is yes. +Below, we make clear why.) + + +
      + +
      + +Leader Activation + +Leader activation includes leader election. We currently have two leader election +algorithms in ZooKeeper: LeaderElection and FastLeaderElection (AuthFastLeaderElection +is a variant of FastLeaderElection that uses UDP and allows servers to perform a simple +form of authentication to avoid IP spoofing). ZooKeeper messaging doesn't care about the +exact method of electing a leader has 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 amoung 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 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 you 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), fter 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 have 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 COMMITED. + + + +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. + + + + + + + + +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 proposals +message when that proposal is committed. + + + +
      + +
      +Summary +So there you go. Why does it work? Specifically, why does is 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 uncommited 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. + + +
      + +
      + +
      +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 as an abstraction layer for logging. +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 section +of the ZooKeeper Administrator's Guide. + + + +
      Developer Guidelines + +Please follow the +slf4j manual when creating log statements within code. +Also read the +FAQ on 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: + + ERROR level designates error events that might still allow the application to continue running. + WARN level designates potentially harmful situations. + INFO level designates informational messages that highlight the progress of the application at coarse-grained level. + DEBUG Level designates fine-grained informational events that are most useful to debug an application. + 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/src/docs/src/documentation/content/xdocs/zookeeperJMX.xml b/src/docs/src/documentation/content/xdocs/zookeeperJMX.xml new file mode 100644 index 0000000..f0ea636 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperJMX.xml @@ -0,0 +1,236 @@ + + + + +
      + ZooKeeper JMX + + + + 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. + + + + ZooKeeper support for JMX + + + +
      + 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 page to get started with JMX. + + + See the + JMX Management Guide 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 initalization 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 + 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 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. + + +
      + +
      + 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 + <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. + +
      + + 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/src/docs/src/documentation/content/xdocs/zookeeperObservers.xml b/src/docs/src/documentation/content/xdocs/zookeeperObservers.xml new file mode 100644 index 0000000..3955f3d --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperObservers.xml @@ -0,0 +1,145 @@ + + + + +
      + ZooKeeper Observers + + + + 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 guide contains information about using non-voting servers, or + observers in your ZooKeeper ensembles. + + + +
      + 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. + +
      + +
      + Example use cases + + Two example use cases for Observers are listed below. In fact, wherever + you wish to scale the numbe 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/src/docs/src/documentation/content/xdocs/zookeeperOtherInfo.xml b/src/docs/src/documentation/content/xdocs/zookeeperOtherInfo.xml new file mode 100644 index 0000000..a2445b1 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperOtherInfo.xml @@ -0,0 +1,46 @@ + + + + +
      + ZooKeeper + + + + 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. + + + + currently empty + + + +
      + Other Info + currently empty +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/zookeeperOver.xml b/src/docs/src/documentation/content/xdocs/zookeeperOver.xml new file mode 100644 index 0000000..7a0444c --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperOver.xml @@ -0,0 +1,464 @@ + + + + +
      + ZooKeeper + + + + 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 document contains overview information about ZooKeeper. It + discusses design goals, key concepts, implementation, and + performance. + + + +
      + 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 hierarchal namespace which is organized similarly to a standard + file system. The name space 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 acheive 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 sets of hosts called an ensemble. + +
      + ZooKeeper Service + + + + + + +
      + + 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 name space 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 name space is identified by a + path. + +
      + ZooKeeper's Hierarchical Namespace + + + + + + +
      +
      + +
      + Nodes and ephemeral nodes + + Unlike is 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. Ephemeral nodes are useful when you + want to implement [tbd]. +
      + +
      + Conditional updates and watches + + ZooKeeper supports the concept of watches. + Clients can set a watch on a znodes. 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. And if the + connection between the client and one of the Zoo Keeper servers is + broken, the client will receive a local notification. These can be used + to [tbd]. +
      + +
      + 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. + + + + + + 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. + + + + For more information on these, and how they can be used, see + [tbd] +
      + +
      + Simple API + + One of the design goals of ZooKeeper is provide 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 + + + + + For a more in-depth discussion on these, and how they can be used + to implement higher level operations, please refer to + [tbd] +
      + +
      + Implementation + + 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 components. + +
      + ZooKeeper Components + + + + + + +
      + + 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 irequests. 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. Some + distributed applications have used it to: [tbd: add uses from + white paper and video presentation.] For more information, see + [tbd] +
      + +
      + Performance + + ZooKeeper is designed to be highly performant. But is it? The + results of the ZooKeeper's development team at Yahoo! Research indicate + that it is. (See .) 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 + + + + + + +
      + The figure 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. + + In version 3.2 r/w performance improved by ~2x + compared to the previous + 3.1 release. + + Benchmarks also indicate that it is reliable, too. shows how a deployment responds to + various failures. The events marked in the figure are the + following: + + + + Failure and recovery of a follower + + + + Failure and recovery of a different follower + + + + Failure of the leader + + + + Failure and recovery of two followers + + + + 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 + + + + + +
      + + The 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 + + 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 + + for more information. + +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml b/src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml new file mode 100644 index 0000000..8fbd679 --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml @@ -0,0 +1,1640 @@ + + + +
      + ZooKeeper Programmer's Guide + + Developing Distributed Applications that use ZooKeeper + + + + 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 guide contains detailed information about creating + distributed applications that use ZooKeeper. It discusses the basic + operations ZooKeeper supports, and how these can be used to build + higher-level abstractions. It contains solutions to common tasks, a + troubleshooting guide, and links to other information. + + $Revision: 1.14 $ $Date: 2008/09/19 05:31:45 $ + + + +
      + 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 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 next four sections provide practical programming + information. These are: + + + + + + + + + + + + + [tbd] + + + + + + + + The book concludes with an appendix containing links to other + useful, ZooKeeper-related information. + + Most of 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 chaptes on + the ZooKeeper Data Model and ZooKeeper Basic Operations. Also, + the Simple Programmming + Example [tbd] is helpful for understanding the basic + structure of a ZooKeeper client application. +
      + +
      + The ZooKeeper Data Model + + ZooKeeper has a hierarchal name space, 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 - \u0019 and \u007F + - \u009F. + + + + The following characters are not allowed: \ud800 -uF8FFF, + \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. For more + information see... )[tbd...] + + + 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 refer 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. + + + A znode is the main abstraction a programmer needs to be aware of. Znodes 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. +
      + +
      + 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 usually 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. +
      + +
      + 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. "<path>0000000001". See + Queue + Recipe 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 "<path>-2147483648"). +
      +
      + +
      + 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 of 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 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: + + + + + + + + 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: + + + 'connected' : session is established and client + is communicating with cluster (client/server communication is + operating properly) + .... client is partitioned from the + cluster + 'disconnected' : client has lost connectivity + with the cluster + .... time elapses, after 'timeout' period the + cluster expires the session, nothing is seen by client as it is + disconnected from cluster + .... time elapses, the client regains network + level connectivity with the cluster + '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: + + + + The application calls an operation on a session that is no + longer alive/valid + 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). + +
      + +
      + 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. + +
      + 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. + + +
      + +
      + 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 + + + + 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 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:id, + where scheme is a the authentication scheme + that the id corresponds to. For + example, ip:172.16.16.1 is an id for a + host with the address 172.16.16.1. + + 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 clients 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.) + +
      + Builtin ACL Schemes + + ZooKeeeper has the following built in schemes: + + + world has a + single id, anyone, that represents + anyone. + + auth doesn't + use any id, represents any authenticated + user. + + 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. + + +
      + +
      + 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. + + + 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. + + This is a very simple example which is intended to show + how to interact with ZooKeeper ACLs + specifically. See .../trunk/src/c/src/cli.c + for an example of a C client implementation + + + +#include <string.h> +#include <errno.h> + +#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 assocatied 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. + +
      + +
      + 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. + + + + + 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: + + + + 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.) + + + + 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 + for more details. + + + 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 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. + [tbd:..]). + + + + +
      + +
      + 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 accross 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 the completion + callback is 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. +
      + +
      + 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. + + + + Run ant compile_jute from the ZooKeeper + top level directory (.../trunk). + This will create a directory named "generated" under + .../trunk/src/c. + + + + Change directory to the.../trunk/src/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. + + + + If you are building from a project source package, + unzip/untar the source tarball and cd to the + zookeeper-x.x.x/src/c directory. + + + + Run ./configure <your-options> 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.) + + + + + See INSTALL for general information about running + configure. + + + + + Run make or make + install to build the libraries and install them. + + + + 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 API in your application + you have to remember to + + + + Include ZooKeeper header: #include + <zookeeper/zookeeper.h> + + + + 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 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. + + + + + See .../trunk/src/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: + + + + + + + +
      + 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 + + +
      + +
      + Read Operations + + +
      + +
      + Write Operations + + +
      + +
      + Handling Watches + + +
      + +
      + Miscelleaneous ZooKeeper Operations + +
      + + +
      + +
      + Program Structure, with Simple Example + + [tbd] +
      + +
      + 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: + + + + 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. + + + + 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. + + + + 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. + + + + 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. + + + + 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. + + + + ZooKeeper Whitepaper [tbd: find url] + + + The definitive discussion of ZooKeeper design and performance, + by Yahoo! Research + + + + + API Reference [tbd: find url] + + + The complete reference to the ZooKeeper API + + + + + ZooKeeper + Talk at the Hadoup Summit 2008 + + + A video introduction to ZooKeeper, by Benjamin Reed of Yahoo! + Research + + + + + Barrier and + Queue Tutorial + + + The excellent Java tutorial by Flavio Junqueira, implementing + simple barriers and producer-consumer queues using ZooKeeper. + + + + + ZooKeeper + - A Reliable, Scalable Distributed Coordination System + + + An article by Todd Hoff (07/15/2008) + + + + + ZooKeeper Recipes + + + Pseudo-level discussion of the implementation of various + synchronization solutions with ZooKeeper: Event Handles, Queues, + Locks, and Two-phase Commits. + + + + + [tbd] + + + Any other good sources anyone can think of... + + + + +
      diff --git a/src/docs/src/documentation/content/xdocs/zookeeperQuotas.xml b/src/docs/src/documentation/content/xdocs/zookeeperQuotas.xml new file mode 100644 index 0000000..7668e6a --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperQuotas.xml @@ -0,0 +1,71 @@ + + + +
      + ZooKeeper Quota's Guide + A Guide to Deployment and Administration + + + + 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 document contains information about deploying, + administering and mantaining ZooKeeper. It also discusses best + practices and common problems. + + +
      + 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. + + $ 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) + and -b (for bytes). + The ZooKeeper quota are stored in ZooKeeper itself in /zookeeper/quota. To disable other people from + changing the quota's set the ACL for /zookeeper/quota such that only admins are able to read and write to it. + +
      +
      + 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/src/docs/src/documentation/content/xdocs/zookeeperStarted.xml b/src/docs/src/documentation/content/xdocs/zookeeperStarted.xml new file mode 100644 index 0000000..70c227f --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperStarted.xml @@ -0,0 +1,418 @@ + + + + +
      + ZooKeeper Getting Started Guide + + + + 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 guide contains detailed information about creating + distributed applications that use ZooKeeper. It discusses the basic + operations ZooKeeper supports, and how these can be used to build + higher-level abstractions. It contains solutions to common tasks, a + troubleshooting guide, and links to other information. + + + +
      + 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. + +
      + Pre-requisites + + See + System Requirements in the Admin guide. +
      + +
      + Download + + To get a ZooKeeper distribution, download a recent + + stable 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 + 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. +
      + +
      + Managing ZooKeeper Storage + For long running production systems ZooKeeper storage must + be managed externally (dataDir and logs). See the section on + 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: + + + + + From the shell, type help to get a listing of commands that can be executed from the client, as in: + + + + 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: + + + + 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: + + + Issue another ls / command to see what the directory looks like: + + + + 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: + + + + We can change the data associated with zk_test by issuing the set command, as in: + + + + + (Notice we did a get after setting the data and it did, indeed, change. + Finally, let's delete the node by issuing: + + + + That's it for now. To explore more, continue with the rest of this document and see the Programmer's Guide. +
      + +
      + 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 for + sample code using of 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. + + + 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 is 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 milleseconds 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. + + + + 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 dataDirs and + distinct clientPorts 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. + +
      + +
      + 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. + + + + [tbd: what is the other config param?] + + +
      +
      +
      diff --git a/src/docs/src/documentation/content/xdocs/zookeeperTutorial.xml b/src/docs/src/documentation/content/xdocs/zookeeperTutorial.xml new file mode 100644 index 0000000..77cca8f --- /dev/null +++ b/src/docs/src/documentation/content/xdocs/zookeeperTutorial.xml @@ -0,0 +1,712 @@ + + + + +
      + Programming with ZooKeeper - A basic tutorial + + + + 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 article contains sample Java code for simple implementations of barrier + and consumers queues.. + + + + +
      + 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<String> 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 reponsability 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 the root node). Upon reception of a notification, +it checks once more whether the root node has any child. + + + /** + * 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<String> 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 estructure thata group 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 necessary 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<String> 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<String> 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<String> 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<String> 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/src/docs/src/documentation/resources/images/2pc.jpg b/src/docs/src/documentation/resources/images/2pc.jpg new file mode 100644 index 0000000..fe4488f Binary files /dev/null and b/src/docs/src/documentation/resources/images/2pc.jpg differ diff --git a/src/docs/src/documentation/resources/images/bk-overview.jpg b/src/docs/src/documentation/resources/images/bk-overview.jpg new file mode 100644 index 0000000..6e12fb4 Binary files /dev/null and b/src/docs/src/documentation/resources/images/bk-overview.jpg differ diff --git a/src/docs/src/documentation/resources/images/favicon.ico b/src/docs/src/documentation/resources/images/favicon.ico new file mode 100644 index 0000000..161bcf7 Binary files /dev/null and b/src/docs/src/documentation/resources/images/favicon.ico differ diff --git a/src/docs/src/documentation/resources/images/hadoop-logo.jpg b/src/docs/src/documentation/resources/images/hadoop-logo.jpg new file mode 100644 index 0000000..809525d Binary files /dev/null and b/src/docs/src/documentation/resources/images/hadoop-logo.jpg differ diff --git a/src/docs/src/documentation/resources/images/state_dia.dia b/src/docs/src/documentation/resources/images/state_dia.dia new file mode 100644 index 0000000..4a58a00 Binary files /dev/null and b/src/docs/src/documentation/resources/images/state_dia.dia differ diff --git a/src/docs/src/documentation/resources/images/state_dia.jpg b/src/docs/src/documentation/resources/images/state_dia.jpg new file mode 100644 index 0000000..b6f4a8b Binary files /dev/null and b/src/docs/src/documentation/resources/images/state_dia.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkarch.jpg b/src/docs/src/documentation/resources/images/zkarch.jpg new file mode 100644 index 0000000..a0e5fcc Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkarch.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkcomponents.jpg b/src/docs/src/documentation/resources/images/zkcomponents.jpg new file mode 100644 index 0000000..7690578 Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkcomponents.jpg differ diff --git a/src/docs/src/documentation/resources/images/zknamespace.jpg b/src/docs/src/documentation/resources/images/zknamespace.jpg new file mode 100644 index 0000000..05534bc Binary files /dev/null and b/src/docs/src/documentation/resources/images/zknamespace.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkperfRW-3.2.jpg b/src/docs/src/documentation/resources/images/zkperfRW-3.2.jpg new file mode 100644 index 0000000..594b50b Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkperfRW-3.2.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkperfRW.jpg b/src/docs/src/documentation/resources/images/zkperfRW.jpg new file mode 100644 index 0000000..ad3019f Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkperfRW.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkperfreliability.jpg b/src/docs/src/documentation/resources/images/zkperfreliability.jpg new file mode 100644 index 0000000..232bba8 Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkperfreliability.jpg differ diff --git a/src/docs/src/documentation/resources/images/zkservice.jpg b/src/docs/src/documentation/resources/images/zkservice.jpg new file mode 100644 index 0000000..1ec9154 Binary files /dev/null and b/src/docs/src/documentation/resources/images/zkservice.jpg differ diff --git a/src/docs/src/documentation/resources/images/zookeeper_small.gif b/src/docs/src/documentation/resources/images/zookeeper_small.gif new file mode 100644 index 0000000..4e8014f Binary files /dev/null and b/src/docs/src/documentation/resources/images/zookeeper_small.gif differ diff --git a/src/docs/src/documentation/skinconf.xml b/src/docs/src/documentation/skinconf.xml new file mode 100644 index 0000000..43f3a49 --- /dev/null +++ b/src/docs/src/documentation/skinconf.xml @@ -0,0 +1,360 @@ + + + + + + + + + + + + true + + false + + true + + true + + + true + + + true + + + true + + + false + + + true + + + ZooKeeper + ZooKeeper: distributed coordination + http://zookeeper.apache.org/ + images/zookeeper_small.gif + + + Hadoop + Apache Hadoop + http://hadoop.apache.org/ + images/hadoop-logo.jpg + + + + + + + images/favicon.ico + + + + The Apache Software Foundation. + http://www.apache.org/licenses/ + + + + + + + + + + + + + + + + + + + p.quote { + margin-left: 2em; + padding: .5em; + background-color: #f0f0f0; + font-family: monospace; + } + + pre.code { + margin-left: 0em; + padding: 0.5em; + background-color: #f0f0f0; + font-family: monospace; + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1in + 1in + 1.25in + 1in + + + + false + + + false + + + + + + Built with Apache Forrest + http://forrest.apache.org/ + images/built-with-forrest-button.png + 88 + 31 + + + + + + diff --git a/src/docs/status.xml b/src/docs/status.xml new file mode 100644 index 0000000..3ac3fda --- /dev/null +++ b/src/docs/status.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + Initial Import + + + + + + + + + Customize this template project with your project's details. This + TODO list is generated from 'status.xml'. + + + Add lots of content. XML content goes in + src/documentation/content/xdocs, or wherever the + ${project.xdocs-dir} property (set in + forrest.properties) points. + + + Mail forrest-dev@xml.apache.org + with feedback. + + + + + + diff --git a/src/java/OldChangeLog b/src/java/OldChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/src/java/lhh/com/lhh/corator/CoratorSessionExample.java b/src/java/lhh/com/lhh/corator/CoratorSessionExample.java new file mode 100644 index 0000000..58ff661 --- /dev/null +++ b/src/java/lhh/com/lhh/corator/CoratorSessionExample.java @@ -0,0 +1,37 @@ +package com.lhh.corator; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.apache.curator.retry.RetryNTimes; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; + +public class CoratorSessionExample { + public static void main(String[] args) throws Exception { + CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", + new RetryNTimes(3, 1000)); + client.start(); + client.getConnectionStateListenable().addListener(new ConnectionStateListener() { + @Override + public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) { + //节点最新的状æ€å‘生了丢失 + if (connectionState == ConnectionState.LOST) { + try { + if (client.getZookeeperClient().blockUntilConnectedOrTimedOut()) { + doTask();//连接上了以åŽå†åŽ»æ“作 + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }); + doTask(); + System.in.read(); + } + + private static void doTask() { + } +} diff --git a/src/java/lhh/com/lhh/corator/LeaderLatchExample.java b/src/java/lhh/com/lhh/corator/LeaderLatchExample.java new file mode 100644 index 0000000..bc9a366 --- /dev/null +++ b/src/java/lhh/com/lhh/corator/LeaderLatchExample.java @@ -0,0 +1,42 @@ +package com.lhh.corator; + +import com.google.common.collect.Lists; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.leader.LeaderLatch; +import org.apache.curator.retry.RetryNTimes; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * 自定义领导者选举 + * è¿™ç§æ–¹å¼ç”¨çš„临时节点 + */ +public class LeaderLatchExample { + public static void main(String[] args) throws Exception { + List clients = Lists.newArrayList(); + List leaderLatchs = Lists.newArrayList(); + for (int i = 0; i < 10; i++) { + CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", + new RetryNTimes(3, 1000)); + clients.add(client); + client.start(); + + LeaderLatch leaderLatch = new LeaderLatch(client, "/LeaderLatch", "client#" + i); + leaderLatchs.add(leaderLatch); + leaderLatch.start(); + } + TimeUnit.SECONDS.sleep(5); + for (LeaderLatch leaderLatch : leaderLatchs) { + if (leaderLatch.hasLeadership()) { + //æ¯æ¬¡éƒ½ä¸ç›¸åŒ + System.out.println(leaderLatch.getId() + "is leader"); + break; + } + } + for (CuratorFramework curatorFramework : clients) { + curatorFramework.close(); + } + } +} diff --git a/src/java/lhh/com/lhh/corator/LeaderSelectorExample.java b/src/java/lhh/com/lhh/corator/LeaderSelectorExample.java new file mode 100644 index 0000000..c21d8fb --- /dev/null +++ b/src/java/lhh/com/lhh/corator/LeaderSelectorExample.java @@ -0,0 +1,58 @@ +package com.lhh.corator; + +import com.google.common.collect.Lists; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.leader.LeaderSelector; +import org.apache.curator.framework.recipes.leader.LeaderSelectorListener; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.retry.RetryNTimes; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * 自定义领导者选举 + * è¿™ç§æ–¹å¼ç”¨çš„æ˜¯é” + */ +public class LeaderSelectorExample { + + + public static void main(String[] args) throws IOException { + List clients = Lists.newArrayList(); + List leaderSelectors = Lists.newArrayList(); + + for (int i = 0; i < 10; i++) { + CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new RetryNTimes(3, 3000)); + client.start(); + clients.add(client); + + LeaderSelector leaderSelector = new LeaderSelector(client, "/LeaderSelector", new LeaderSelectorListener() { + @Override + public void takeLeadership(CuratorFramework client) throws Exception { + // 当上Leader了就会进入这个方法 + System.out.println("当å‰Leader是" + client); + + TimeUnit.SECONDS.sleep(5); + } + + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) { + + } + }); + leaderSelector.start(); + leaderSelectors.add(leaderSelector); + } + + System.in.read(); + + for (CuratorFramework client : clients) { + client.close(); + } + for (LeaderSelector selector : leaderSelectors) { + selector.close(); + } + } +} diff --git a/src/java/lhh/com/lhh/corator/ZkCoratorTest.java b/src/java/lhh/com/lhh/corator/ZkCoratorTest.java new file mode 100644 index 0000000..9279377 --- /dev/null +++ b/src/java/lhh/com/lhh/corator/ZkCoratorTest.java @@ -0,0 +1,41 @@ +package com.lhh.corator; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.NodeCache; +import org.apache.curator.framework.recipes.cache.NodeCacheListener; +import org.apache.curator.retry.RetryNTimes; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; + +public class ZkCoratorTest { + public static void main(String[] args) throws Exception { + //解决了节点é‡å¤é—®é¢˜ ä¸æ˜¯ä¸€æ¬¡æ€§ + CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", + new RetryNTimes(3, 1000)); + client.start(); +// client.create().withMode(CreateMode.EPHEMERAL).forPath("/data","100".getBytes()); + String path = "/data"; + //NodeCache 就是对一个节点的缓存 +// NodeCache nodeCache = new NodeCache(client, path); +// nodeCache.start(); +// nodeCache.getListenable().addListener(new NodeCacheListener() { +// @Override +// public void nodeChanged() throws Exception { +// System.out.println("1212123"); +// } +// }); + + /** + * 这里使用的是原生客户端一次性的 + * åªæ˜¯ä¼šåˆ¤æ–­ä¸€æ¬¡ + */ + client.getData().usingWatcher(new Watcher() { + @Override + public void process(WatchedEvent event) { + System.out.println("用的是CuratorFramework Watcher"); + } + }).forPath(path); + System.in.read(); + } +} diff --git a/src/java/lhh/com/lhh/native1/client/ZookeeperClientTest.java b/src/java/lhh/com/lhh/native1/client/ZookeeperClientTest.java new file mode 100644 index 0000000..f3ee613 --- /dev/null +++ b/src/java/lhh/com/lhh/native1/client/ZookeeperClientTest.java @@ -0,0 +1,27 @@ +package com.lhh.native1.client; + +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import static org.apache.zookeeper.Watcher.Event.EventType.NodeDataChanged; + +/** + * åŽŸç”Ÿçš„ä»£ç  + */ +public class ZookeeperClientTest { + public static void main(String[] args) throws Exception { +// ZooKeeper client = new ZooKeeper("192.168.37.71:2181", +// 5000, +// event -> System.out.println("event = " + event)); + ZooKeeper client = new ZooKeeper("localhost:2181", + 5000, + event -> System.out.println("event = " + event)); + Stat stat = new Stat(); + new String(client.getData("/data", event -> { + //åªèƒ½åˆ¤æ–­ç¬¬ä¸€æ¬¡æ”¹åŠ¨ + if (NodeDataChanged == event.getType()) { + System.out.println("æ•°æ®å‘生了改å˜"); + } + },stat)); + System.in.read(); + } +} diff --git a/src/java/lhh/com/lhh/zkclient/WatchClient.java b/src/java/lhh/com/lhh/zkclient/WatchClient.java new file mode 100644 index 0000000..6160e9c --- /dev/null +++ b/src/java/lhh/com/lhh/zkclient/WatchClient.java @@ -0,0 +1,17 @@ +package com.lhh.zkclient; + +import org.I0Itec.zkclient.ZkClient; +import org.I0Itec.zkclient.serialize.SerializableSerializer; + +import java.util.concurrent.TimeUnit; + +public class WatchClient { + public static void main(String[] args) throws InterruptedException { + ZkClient zk = new ZkClient("localhost:2181", 1000, 1000, new SerializableSerializer()); + for (int i = 0; i <10 ; i++) { + TimeUnit.SECONDS.sleep(2); + zk.writeData("/data","90"+i); + } + + } +} diff --git a/src/java/lhh/com/lhh/zkclient/client.java b/src/java/lhh/com/lhh/zkclient/client.java new file mode 100644 index 0000000..e39c03c --- /dev/null +++ b/src/java/lhh/com/lhh/zkclient/client.java @@ -0,0 +1,30 @@ +package com.lhh.zkclient; + +import org.I0Itec.zkclient.IZkDataListener; +import org.I0Itec.zkclient.ZkClient; +import org.I0Itec.zkclient.serialize.SerializableSerializer; + +import java.io.IOException; + +/** + * 使用zk自带的client + */ +public class client { + public static void main(String[] args) throws IOException { + ZkClient zk = new ZkClient("localhost:2181", 1000, 1000, new SerializableSerializer()); +// zk.createPersistent("/data","1".getBytes()); + zk.subscribeDataChanges("/data", new IZkDataListener() { + @Override + public void handleDataChange(String dataPath, Object data) throws Exception { + System.out.println("æ•°æ®è¢«æ”¹å˜äº†"); + } + + @Override + public void handleDataDeleted(String dataPath) throws Exception { + + } + }); + + System.in.read(); + } +} diff --git a/src/java/lib/cobertura/README.txt b/src/java/lib/cobertura/README.txt new file mode 100644 index 0000000..f5ba88f --- /dev/null +++ b/src/java/lib/cobertura/README.txt @@ -0,0 +1,3 @@ +Download the cobertura binary from the following location and unpack it into this directory. Run "cobertura-report" target from build.xml to generate coverage report. + +http://cobertura.sourceforge.net/download.html diff --git a/src/java/lib/curator/curator-client-4.0.1.jar b/src/java/lib/curator/curator-client-4.0.1.jar new file mode 100644 index 0000000..15c7077 Binary files /dev/null and b/src/java/lib/curator/curator-client-4.0.1.jar differ diff --git a/src/java/lib/curator/curator-framework-4.0.1.jar b/src/java/lib/curator/curator-framework-4.0.1.jar new file mode 100644 index 0000000..40dab27 Binary files /dev/null and b/src/java/lib/curator/curator-framework-4.0.1.jar differ diff --git a/src/java/lib/curator/curator-recipes-4.0.1.jar b/src/java/lib/curator/curator-recipes-4.0.1.jar new file mode 100644 index 0000000..6b09383 Binary files /dev/null and b/src/java/lib/curator/curator-recipes-4.0.1.jar differ diff --git a/src/java/lib/curator/curator-test-4.0.1.jar b/src/java/lib/curator/curator-test-4.0.1.jar new file mode 100644 index 0000000..7422011 Binary files /dev/null and b/src/java/lib/curator/curator-test-4.0.1.jar differ diff --git a/src/java/lib/jdiff/zookeeper_3.1.1.xml b/src/java/lib/jdiff/zookeeper_3.1.1.xml new file mode 100644 index 0000000..c28b238 --- /dev/null +++ b/src/java/lib/jdiff/zookeeper_3.1.1.xml @@ -0,0 +1,2717 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    • --short - prints a short version string "1.2.3" +
    • --revision - prints a short version string with the SVN + repository revision "1.2.3-94" +
    • --full - prints the revision and the build date +
    ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The client object will pick an arbitrary server and try to connect to it. + If failed, it will try the next one in the list, until a connection is + established, or all the servers have been tried. + + @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 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]]> + + + + + + + The client object will pick an arbitrary server and try to connect to it. + If failed, it will try the next one in the list, until a connection is + established, or all the servers have been tried. +

    + 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 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 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]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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. 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 flags + 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 org.apache.zookeeper.KeeperException.InvalidACLException if the ACL is invalid + @throws InterruptedException if the transaction is interrupted + @throws IllegalArgumentException if an invalid path is specified]]> + + + + + + + + + + + + + + + + + + , CreateMode)]]> + + + + + + + + + + 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]]> + + + + + + + + + + + + + + + + + + + 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]]> + + + + + + + + + + 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.]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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]]> + + + + + + + + + + + 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.]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 KeeperExecption 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]]> + + + + + + + + + + + + + + + + + + + + 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. + @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]]> + + + + + + + + + + + + + + + + + + + + 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. + + @param path + @param acl + @param version + @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]]> + + + + + + + + + + + + + + + + + + + + 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 willbe + 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]]> + + + + + + + + + + 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 willbe + 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.]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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. +

    + 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 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 type + EventNone and state sKeeperStateDisconnected.]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java/lib/jline-0.9.94.LICENSE.txt b/src/java/lib/jline-0.9.94.LICENSE.txt new file mode 100644 index 0000000..1cdc44c --- /dev/null +++ b/src/java/lib/jline-0.9.94.LICENSE.txt @@ -0,0 +1,33 @@ +Copyright (c) 2002-2006, Marc Prud'hommeaux +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 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/src/java/lib/log4j-1.2.17.LICENSE.txt b/src/java/lib/log4j-1.2.17.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/src/java/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/src/java/lib/slf4j-1.7.25.LICENSE.txt b/src/java/lib/slf4j-1.7.25.LICENSE.txt new file mode 100644 index 0000000..315bd49 --- /dev/null +++ b/src/java/lib/slf4j-1.7.25.LICENSE.txt @@ -0,0 +1,24 @@ +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/src/java/lib/zkclient-0.3.jar b/src/java/lib/zkclient-0.3.jar new file mode 100644 index 0000000..6dbd23c Binary files /dev/null and b/src/java/lib/zkclient-0.3.jar differ diff --git a/src/java/main/log4j.properties b/src/java/main/log4j.properties new file mode 100644 index 0000000..9825970 --- /dev/null +++ b/src/java/main/log4j.properties @@ -0,0 +1,58 @@ +# 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=DEBUG +zookeeper.tracelog.dir=. +zookeeper.tracelog.file=zookeeper_trace.log + +# +# ZooKeeper Logging Configuration +# + +# Format is " (, )+ + +# DEFAULT: console appender only +log4j.rootLogger=${zookeeper.root.logger} + +# 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=${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 +# Log DEBUG level and above messages to a log file +log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender +log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold} +log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file} + +# 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 + +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 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=${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/src/java/main/org/apache/jute/BinaryInputArchive.java b/src/java/main/org/apache/jute/BinaryInputArchive.java new file mode 100644 index 0000000..7722bff --- /dev/null +++ b/src/java/main/org/apache/jute/BinaryInputArchive.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.jute; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * + */ +public class BinaryInputArchive implements InputArchive { + static public final String UNREASONBLE_LENGTH= "Unreasonable length = "; + private DataInput in; + + static public BinaryInputArchive getArchive(InputStream strm) { + return new BinaryInputArchive(new DataInputStream(strm)); + } + + static private 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 = in; + } + + 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, "UTF8"); + } + + static public final int maxBuffer = Integer.getInteger("jute.maxbuffer", 0xfffff); + + 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 > maxBuffer + 1024) { + throw new IOException(UNREASONBLE_LENGTH + len); + } + } +} diff --git a/src/java/main/org/apache/jute/BinaryOutputArchive.java b/src/java/main/org/apache/jute/BinaryOutputArchive.java new file mode 100644 index 0000000..213e203 --- /dev/null +++ b/src/java/main/org/apache/jute/BinaryOutputArchive.java @@ -0,0 +1,146 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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. + */ + final 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/src/java/main/org/apache/jute/CsvInputArchive.java b/src/java/main/org/apache/jute/CsvInputArchive.java new file mode 100644 index 0000000..3eb40ec --- /dev/null +++ b/src/java/main/org/apache/jute/CsvInputArchive.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.jute; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PushbackReader; +import java.io.UnsupportedEncodingException; + +/** + * + */ +class CsvInputArchive implements InputArchive { + + private PushbackReader stream; + + private class CsvIndex implements Index { + public boolean done() { + char c = '\0'; + try { + c = (char) stream.read(); + stream.unread(c); + } catch (IOException ex) { + } + return (c == '}') ? true : false; + } + public void incr() {} + } + + private void throwExceptionOnError(String tag) throws IOException { + throw new IOException("Error deserializing "+tag); + } + + private String readField(String tag) throws IOException { + try { + StringBuilder buf = new StringBuilder(); + while (true) { + char c = (char) stream.read(); + switch (c) { + case ',': + return buf.toString(); + case '}': + case '\n': + case '\r': + stream.unread(c); + return buf.toString(); + default: + buf.append(c); + } + } + } catch (IOException ex) { + throw new IOException("Error reading "+tag); + } + } + + static CsvInputArchive getArchive(InputStream strm) + throws UnsupportedEncodingException { + return new CsvInputArchive(strm); + } + + /** Creates a new instance of CsvInputArchive */ + public CsvInputArchive(InputStream in) + throws UnsupportedEncodingException { + stream = new PushbackReader(new InputStreamReader(in, "UTF-8")); + } + + public byte readByte(String tag) throws IOException { + return (byte) readLong(tag); + } + + public boolean readBool(String tag) throws IOException { + String sval = readField(tag); + return "T".equals(sval) ? true : false; + } + + public int readInt(String tag) throws IOException { + return (int) readLong(tag); + } + + public long readLong(String tag) throws IOException { + String sval = readField(tag); + try { + long lval = Long.parseLong(sval); + return lval; + } catch (NumberFormatException ex) { + throw new IOException("Error deserializing "+tag); + } + } + + public float readFloat(String tag) throws IOException { + return (float) readDouble(tag); + } + + public double readDouble(String tag) throws IOException { + String sval = readField(tag); + try { + double dval = Double.parseDouble(sval); + return dval; + } catch (NumberFormatException ex) { + throw new IOException("Error deserializing "+tag); + } + } + + public String readString(String tag) throws IOException { + String sval = readField(tag); + return Utils.fromCSVString(sval); + + } + + public byte[] readBuffer(String tag) throws IOException { + String sval = readField(tag); + return Utils.fromCSVBuffer(sval); + } + + public void readRecord(Record r, String tag) throws IOException { + r.deserialize(this, tag); + } + + public void startRecord(String tag) throws IOException { + if (tag != null && !"".equals(tag)) { + char c1 = (char) stream.read(); + char c2 = (char) stream.read(); + if (c1 != 's' || c2 != '{') { + throw new IOException("Error deserializing "+tag); + } + } + } + + public void endRecord(String tag) throws IOException { + char c = (char) stream.read(); + if (tag == null || "".equals(tag)) { + if (c != '\n' && c != '\r') { + throw new IOException("Error deserializing record."); + } else { + return; + } + } + + if (c != '}') { + throw new IOException("Error deserializing "+tag); + } + c = (char) stream.read(); + if (c != ',') { + stream.unread(c); + } + + return; + } + + public Index startVector(String tag) throws IOException { + char c1 = (char) stream.read(); + char c2 = (char) stream.read(); + if (c1 != 'v' || c2 != '{') { + throw new IOException("Error deserializing "+tag); + } + return new CsvIndex(); + } + + public void endVector(String tag) throws IOException { + char c = (char) stream.read(); + if (c != '}') { + throw new IOException("Error deserializing "+tag); + } + c = (char) stream.read(); + if (c != ',') { + stream.unread(c); + } + return; + } + + public Index startMap(String tag) throws IOException { + char c1 = (char) stream.read(); + char c2 = (char) stream.read(); + if (c1 != 'm' || c2 != '{') { + throw new IOException("Error deserializing "+tag); + } + return new CsvIndex(); + } + + public void endMap(String tag) throws IOException { + char c = (char) stream.read(); + if (c != '}') { + throw new IOException("Error deserializing "+tag); + } + c = (char) stream.read(); + if (c != ',') { + stream.unread(c); + } + return; + } +} diff --git a/src/java/main/org/apache/jute/CsvOutputArchive.java b/src/java/main/org/apache/jute/CsvOutputArchive.java new file mode 100644 index 0000000..f6d60d8 --- /dev/null +++ b/src/java/main/org/apache/jute/CsvOutputArchive.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.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 CsvOutputArchive implements OutputArchive { + + private PrintStream stream; + private boolean isFirst = true; + + static CsvOutputArchive getArchive(OutputStream strm) + throws UnsupportedEncodingException { + return new CsvOutputArchive(strm); + } + + 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 CsvOutputArchive */ + public CsvOutputArchive(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(Utils.toCSVString(s)); + throwExceptionOnError(tag); + } + + public void writeBuffer(byte buf[], String tag) + throws IOException { + printCommaUnlessFirst(); + stream.print(Utils.toCSVBuffer(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; + } +} diff --git a/src/java/main/org/apache/jute/Index.java b/src/java/main/org/apache/jute/Index.java new file mode 100644 index 0000000..258c6b5 --- /dev/null +++ b/src/java/main/org/apache/jute/Index.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.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(); + * } + * + * + */ +public interface Index { + public boolean done(); + public void incr(); +} diff --git a/src/java/main/org/apache/jute/InputArchive.java b/src/java/main/org/apache/jute/InputArchive.java new file mode 100644 index 0000000..b19ab3d --- /dev/null +++ b/src/java/main/org/apache/jute/InputArchive.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.jute; + +import java.io.IOException; + +/** + * Interface that all the Deserializers have to implement. + * + */ +public interface InputArchive { + public byte readByte(String tag) throws IOException; + public boolean readBool(String tag) throws IOException; + public int readInt(String tag) throws IOException; + public long readLong(String tag) throws IOException; + public float readFloat(String tag) throws IOException; + public double readDouble(String tag) throws IOException; + public String readString(String tag) throws IOException; + public byte[] readBuffer(String tag) throws IOException; + public void readRecord(Record r, String tag) throws IOException; + public void startRecord(String tag) throws IOException; + public void endRecord(String tag) throws IOException; + public Index startVector(String tag) throws IOException; + public void endVector(String tag) throws IOException; + public Index startMap(String tag) throws IOException; + public void endMap(String tag) throws IOException; +} diff --git a/src/java/main/org/apache/jute/OutputArchive.java b/src/java/main/org/apache/jute/OutputArchive.java new file mode 100644 index 0000000..4e084e8 --- /dev/null +++ b/src/java/main/org/apache/jute/OutputArchive.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.jute; + +import java.io.IOException; +import java.util.List; +import java.util.TreeMap; + +/** + * Interface that alll the serializers have to implement. + * + */ +public interface OutputArchive { + public void writeByte(byte b, String tag) throws IOException; + public void writeBool(boolean b, String tag) throws IOException; + public void writeInt(int i, String tag) throws IOException; + public void writeLong(long l, String tag) throws IOException; + public void writeFloat(float f, String tag) throws IOException; + public void writeDouble(double d, String tag) throws IOException; + public void writeString(String s, String tag) throws IOException; + public void writeBuffer(byte buf[], String tag) + throws IOException; + public void writeRecord(Record r, String tag) throws IOException; + 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; + public void endVector(List v, String tag) throws IOException; + public void startMap(TreeMap v, String tag) throws IOException; + public void endMap(TreeMap v, String tag) throws IOException; + +} diff --git a/src/java/main/org/apache/jute/Record.java b/src/java/main/org/apache/jute/Record.java new file mode 100644 index 0000000..d955280 --- /dev/null +++ b/src/java/main/org/apache/jute/Record.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.jute; + +import org.apache.yetus.audience.InterfaceAudience; + +import java.io.IOException; + +/** + * Interface that is implemented by generated classes. + * + */ +@InterfaceAudience.Public +public interface Record { + public void serialize(OutputArchive archive, String tag) + throws IOException; + public void deserialize(InputArchive archive, String tag) + throws IOException; +} diff --git a/src/java/main/org/apache/jute/RecordReader.java b/src/java/main/org/apache/jute/RecordReader.java new file mode 100644 index 0000000..2977d3f --- /dev/null +++ b/src/java/main/org/apache/jute/RecordReader.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.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 InputArchive archive; + + static private HashMap archiveFactory; + + static { + archiveFactory = new HashMap(); + Class[] params = { InputStream.class }; + try { + archiveFactory.put("binary", + BinaryInputArchive.class.getDeclaredMethod( + "getArchive", params)); + archiveFactory.put("csv", + CsvInputArchive.class.getDeclaredMethod( + "getArchive", params)); + archiveFactory.put("xml", + XmlInputArchive.class.getDeclaredMethod( + "getArchive", params)); + } catch (SecurityException ex) { + ex.printStackTrace(); + } catch (NoSuchMethodException ex) { + ex.printStackTrace(); + } + } + + static private InputArchive createArchive(InputStream in, String format) + throws IOException { + Method factory = (Method) archiveFactory.get(format); + if (factory != null) { + Object[] params = { in }; + try { + return (InputArchive) factory.invoke(null, params); + } catch (IllegalArgumentException ex) { + ex.printStackTrace(); + } catch (InvocationTargetException ex) { + ex.printStackTrace(); + } catch (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) + throws IOException { + 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/src/java/main/org/apache/jute/RecordWriter.java b/src/java/main/org/apache/jute/RecordWriter.java new file mode 100644 index 0000000..0adbd56 --- /dev/null +++ b/src/java/main/org/apache/jute/RecordWriter.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.jute; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +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 private OutputArchive getBinaryArchive(OutputStream out) { + return new BinaryOutputArchive(new DataOutputStream(out)); + } + + static private OutputArchive getCsvArchive(OutputStream out) + throws IOException { + try { + return new CsvOutputArchive(out); + } catch (UnsupportedEncodingException ex) { + throw new IOException("Unsupported encoding UTF-8"); + } + } + + static private OutputArchive getXmlArchive(OutputStream out) + throws IOException { + return new XmlOutputArchive(out); + } + + static HashMap constructFactory() { + HashMap factory = new HashMap(); + Class[] params = { OutputStream.class }; + try { + factory.put("binary", + BinaryOutputArchive.class.getDeclaredMethod( + "getArchive", params)); + factory.put("csv", + CsvOutputArchive.class.getDeclaredMethod( + "getArchive", params)); + factory.put("xml", + XmlOutputArchive.class.getDeclaredMethod( + "getArchive", params)); + } catch (SecurityException ex) { + ex.printStackTrace(); + } catch (NoSuchMethodException ex) { + ex.printStackTrace(); + } + return factory; + } + + static private HashMap archiveFactory = constructFactory(); + + static private OutputArchive createArchive(OutputStream out, + String format) + throws IOException { + Method factory = (Method) archiveFactory.get(format); + if (factory != null) { + Object[] params = { out }; + try { + return (OutputArchive) factory.invoke(null, params); + } catch (IllegalArgumentException ex) { + ex.printStackTrace(); + } catch (InvocationTargetException ex) { + ex.printStackTrace(); + } catch (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) + throws IOException { + 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/src/java/main/org/apache/jute/Utils.java b/src/java/main/org/apache/jute/Utils.java new file mode 100644 index 0000000..1205fa2 --- /dev/null +++ b/src/java/main/org/apache/jute/Utils.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.jute; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * 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; + } + + private static final char[] hexchars = { '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F' }; + /** + * + * @param s + * @return + */ + static String toXMLString(String s) { + if (s == null) + return ""; + + StringBuilder sb = new StringBuilder(); + for (int idx = 0; idx < s.length(); idx++) { + char ch = s.charAt(idx); + if (ch == '<') { + sb.append("<"); + } else if (ch == '&') { + sb.append("&"); + } else if (ch == '%') { + sb.append("%25"); + } else if (ch < 0x20) { + sb.append("%"); + sb.append(hexchars[ch/16]); + sb.append(hexchars[ch%16]); + } else { + sb.append(ch); + } + } + return sb.toString(); + } + + static private int h2c(char ch) { + if (ch >= '0' && ch <= '9') { + return ch - '0'; + } else if (ch >= 'A' && ch <= 'F') { + return ch - 'A'; + } else if (ch >= 'a' && ch <= 'f') { + return ch - 'a'; + } + return 0; + } + + /** + * + * @param s + * @return + */ + static String fromXMLString(String s) { + StringBuilder sb = new StringBuilder(); + for (int idx = 0; idx < s.length();) { + char ch = s.charAt(idx++); + if (ch == '%') { + char ch1 = s.charAt(idx++); + char ch2 = s.charAt(idx++); + char res = (char)(h2c(ch1)*16 + h2c(ch2)); + sb.append(res); + } else { + sb.append(ch); + } + } + + return sb.toString(); + } + + /** + * + * @param s + * @return + */ + static String toCSVString(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(); + } + + /** + * + * @param s + * @throws java.io.IOException + * @return + */ + static String fromCSVString(String s) throws IOException { + if (s.charAt(0) != '\'') { + throw new IOException("Error deserializing string."); + } + int len = s.length(); + StringBuilder sb = new StringBuilder(len-1); + for (int i = 1; i < len; i++) { + char c = s.charAt(i); + if (c == '%') { + char ch1 = s.charAt(i+1); + char ch2 = s.charAt(i+2); + i += 2; + if (ch1 == '0' && ch2 == '0') { sb.append('\0'); } + else if (ch1 == '0' && ch2 == 'A') { sb.append('\n'); } + else if (ch1 == '0' && ch2 == 'D') { sb.append('\r'); } + else if (ch1 == '2' && ch2 == 'C') { sb.append(','); } + else if (ch1 == '7' && ch2 == 'D') { sb.append('}'); } + else if (ch1 == '2' && ch2 == '5') { sb.append('%'); } + else {throw new IOException("Error deserializing string.");} + } else { + sb.append(c); + } + } + return sb.toString(); + } + + /** + * + * @param s + * @return + */ + static String toXMLBuffer(byte barr[]) { + if (barr == null || barr.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(2*barr.length); + for (int idx = 0; idx < barr.length; idx++) { + sb.append(Integer.toHexString(barr[idx])); + } + return sb.toString(); + } + + /** + * + * @param s + * @throws java.io.IOException + * @return + */ + static byte[] fromXMLBuffer(String s) + throws IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + if (s.length() == 0) { return stream.toByteArray(); } + int blen = s.length()/2; + byte[] barr = new byte[blen]; + for (int idx = 0; idx < blen; idx++) { + char c1 = s.charAt(2*idx); + char c2 = s.charAt(2*idx+1); + barr[idx] = Byte.parseByte(""+c1+c2, 16); + } + stream.write(barr); + return stream.toByteArray(); + } + + /** + * + * @param buf + * @return + */ + static String toCSVBuffer(byte barr[]) { + if (barr == null || barr.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(barr.length + 1); + sb.append('#'); + for(int idx = 0; idx < barr.length; idx++) { + sb.append(Integer.toHexString(barr[idx])); + } + return sb.toString(); + } + + /** + * Converts a CSV-serialized representation of buffer to a new + * ByteArrayOutputStream. + * @param s CSV-serialized representation of buffer + * @throws java.io.IOException + * @return Deserialized ByteArrayOutputStream + */ + static byte[] fromCSVBuffer(String s) + throws IOException { + if (s.charAt(0) != '#') { + throw new IOException("Error deserializing buffer."); + } + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + if (s.length() == 1) { return stream.toByteArray(); } + int blen = (s.length()-1)/2; + byte[] barr = new byte[blen]; + for (int idx = 0; idx < blen; idx++) { + char c1 = s.charAt(2*idx+1); + char c2 = s.charAt(2*idx+2); + barr[idx] = Byte.parseByte(""+c1+c2, 16); + } + stream.write(barr); + return stream.toByteArray(); + } + 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/src/java/main/org/apache/jute/XmlInputArchive.java b/src/java/main/org/apache/jute/XmlInputArchive.java new file mode 100644 index 0000000..99e11d1 --- /dev/null +++ b/src/java/main/org/apache/jute/XmlInputArchive.java @@ -0,0 +1,251 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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.util.ArrayList; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; +/** + * + */ +class XmlInputArchive implements InputArchive { + + static private class Value { + private String type; + private StringBuffer sb; + + public Value(String t) { + type = t; + sb = new StringBuffer(); + } + public void addChars(char[] buf, int offset, int len) { + sb.append(buf, offset, len); + } + public String getValue() { return sb.toString(); } + public String getType() { return type; } + } + + private static class XMLParser extends DefaultHandler { + private boolean charsValid = false; + + private ArrayList valList; + + private XMLParser(ArrayList vlist) { + valList = vlist; + } + + public void startDocument() throws SAXException {} + + public void endDocument() throws SAXException {} + + public void startElement(String ns, + String sname, + String qname, + Attributes attrs) throws SAXException { + charsValid = false; + if ("boolean".equals(qname) || + "i4".equals(qname) || + "int".equals(qname) || + "string".equals(qname) || + "double".equals(qname) || + "ex:i1".equals(qname) || + "ex:i8".equals(qname) || + "ex:float".equals(qname)) { + charsValid = true; + valList.add(new Value(qname)); + } else if ("struct".equals(qname) || + "array".equals(qname)) { + valList.add(new Value(qname)); + } + } + + public void endElement(String ns, + String sname, + String qname) throws SAXException { + charsValid = false; + if ("struct".equals(qname) || + "array".equals(qname)) { + valList.add(new Value("/"+qname)); + } + } + + public void characters(char buf[], int offset, int len) + throws SAXException { + if (charsValid) { + Value v = valList.get(valList.size()-1); + v.addChars(buf, offset,len); + } + } + + } + + private class XmlIndex implements Index { + public boolean done() { + Value v = valList.get(vIdx); + if ("/array".equals(v.getType())) { + valList.set(vIdx, null); + vIdx++; + return true; + } else { + return false; + } + } + public void incr() {} + } + + private ArrayList valList; + private int vLen; + private int vIdx; + + private Value next() throws IOException { + if (vIdx < vLen) { + Value v = valList.get(vIdx); + valList.set(vIdx, null); + vIdx++; + return v; + } else { + throw new IOException("Error in deserialization."); + } + } + + static XmlInputArchive getArchive(InputStream strm) + throws ParserConfigurationException, SAXException, IOException { + return new XmlInputArchive(strm); + } + + /** Creates a new instance of BinaryInputArchive */ + public XmlInputArchive(InputStream in) + throws ParserConfigurationException, SAXException, IOException { + valList = new ArrayList(); + DefaultHandler handler = new XMLParser(valList); + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + parser.parse(in, handler); + vLen = valList.size(); + vIdx = 0; + } + + public byte readByte(String tag) throws IOException { + Value v = next(); + if (!"ex:i1".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Byte.parseByte(v.getValue()); + } + + public boolean readBool(String tag) throws IOException { + Value v = next(); + if (!"boolean".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return "1".equals(v.getValue()); + } + + public int readInt(String tag) throws IOException { + Value v = next(); + if (!"i4".equals(v.getType()) && + !"int".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Integer.parseInt(v.getValue()); + } + + public long readLong(String tag) throws IOException { + Value v = next(); + if (!"ex:i8".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Long.parseLong(v.getValue()); + } + + public float readFloat(String tag) throws IOException { + Value v = next(); + if (!"ex:float".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Float.parseFloat(v.getValue()); + } + + public double readDouble(String tag) throws IOException { + Value v = next(); + if (!"double".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Double.parseDouble(v.getValue()); + } + + public String readString(String tag) throws IOException { + Value v = next(); + if (!"string".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Utils.fromXMLString(v.getValue()); + } + + public byte[] readBuffer(String tag) throws IOException { + Value v = next(); + if (!"string".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return Utils.fromXMLBuffer(v.getValue()); + } + + public void readRecord(Record r, String tag) throws IOException { + r.deserialize(this, tag); + } + + public void startRecord(String tag) throws IOException { + Value v = next(); + if (!"struct".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + } + + public void endRecord(String tag) throws IOException { + Value v = next(); + if (!"/struct".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + } + + public Index startVector(String tag) throws IOException { + Value v = next(); + if (!"array".equals(v.getType())) { + throw new IOException("Error deserializing "+tag+"."); + } + return new XmlIndex(); + } + + public void endVector(String tag) throws IOException {} + + public Index startMap(String tag) throws IOException { + return startVector(tag); + } + + public void endMap(String tag) throws IOException { endVector(tag); } + +} diff --git a/src/java/main/org/apache/jute/XmlOutputArchive.java b/src/java/main/org/apache/jute/XmlOutputArchive.java new file mode 100644 index 0000000..b65e9a0 --- /dev/null +++ b/src/java/main/org/apache/jute/XmlOutputArchive.java @@ -0,0 +1,251 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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.util.List; +import java.util.Stack; +import java.util.TreeMap; + +/** + * + */ +class XmlOutputArchive implements OutputArchive { + + private PrintStream stream; + + private int indent = 0; + + private Stack compoundStack; + + private void putIndent() { + StringBuilder sb = new StringBuilder(""); + for (int idx = 0; idx < indent; idx++) { + sb.append(" "); + } + stream.print(sb.toString()); + } + + private void addIndent() { + indent++; + } + + private void closeIndent() { + indent--; + } + + private void printBeginEnvelope(String tag) { + if (!compoundStack.empty()) { + String s = compoundStack.peek(); + if ("struct".equals(s)) { + putIndent(); + stream.print("\n"); + addIndent(); + putIndent(); + stream.print(""+tag+"\n"); + putIndent(); + stream.print(""); + } else if ("vector".equals(s)) { + stream.print(""); + } else if ("map".equals(s)) { + stream.print(""); + } + } else { + stream.print(""); + } + } + + private void printEndEnvelope(String tag) { + if (!compoundStack.empty()) { + String s = compoundStack.peek(); + if ("struct".equals(s)) { + stream.print("\n"); + closeIndent(); + putIndent(); + stream.print("\n"); + } else if ("vector".equals(s)) { + stream.print("\n"); + } else if ("map".equals(s)) { + stream.print("\n"); + } + } else { + stream.print("\n"); + } + } + + private void insideVector(String tag) { + printBeginEnvelope(tag); + compoundStack.push("vector"); + } + + private void outsideVector(String tag) throws IOException { + String s = compoundStack.pop(); + if (!"vector".equals(s)) { + throw new IOException("Error serializing vector."); + } + printEndEnvelope(tag); + } + + private void insideMap(String tag) { + printBeginEnvelope(tag); + compoundStack.push("map"); + } + + private void outsideMap(String tag) throws IOException { + String s = compoundStack.pop(); + if (!"map".equals(s)) { + throw new IOException("Error serializing map."); + } + printEndEnvelope(tag); + } + + private void insideRecord(String tag) { + printBeginEnvelope(tag); + compoundStack.push("struct"); + } + + private void outsideRecord(String tag) throws IOException { + String s = compoundStack.pop(); + if (!"struct".equals(s)) { + throw new IOException("Error serializing record."); + } + printEndEnvelope(tag); + } + + static XmlOutputArchive getArchive(OutputStream strm) { + return new XmlOutputArchive(strm); + } + + /** Creates a new instance of XmlOutputArchive */ + public XmlOutputArchive(OutputStream out) { + stream = new PrintStream(out); + compoundStack = new Stack(); + } + + public void writeByte(byte b, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Byte.toString(b)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeBool(boolean b, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(b ? "1" : "0"); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeInt(int i, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Integer.toString(i)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeLong(long l, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Long.toString(l)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeFloat(float f, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Float.toString(f)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeDouble(double d, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Double.toString(d)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeString(String s, String tag) throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Utils.toXMLString(s)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeBuffer(byte buf[], String tag) + throws IOException { + printBeginEnvelope(tag); + stream.print(""); + stream.print(Utils.toXMLBuffer(buf)); + stream.print(""); + printEndEnvelope(tag); + } + + public void writeRecord(Record r, String tag) throws IOException { + r.serialize(this, tag); + } + + public void startRecord(Record r, String tag) throws IOException { + insideRecord(tag); + stream.print("\n"); + addIndent(); + } + + public void endRecord(Record r, String tag) throws IOException { + closeIndent(); + putIndent(); + stream.print(""); + outsideRecord(tag); + } + + public void startVector(List v, String tag) throws IOException { + insideVector(tag); + stream.print("\n"); + addIndent(); + } + + public void endVector(List v, String tag) throws IOException { + closeIndent(); + putIndent(); + stream.print(""); + outsideVector(tag); + } + + public void startMap(TreeMap v, String tag) throws IOException { + insideMap(tag); + stream.print("\n"); + addIndent(); + } + + public void endMap(TreeMap v, String tag) throws IOException { + closeIndent(); + putIndent(); + stream.print(""); + outsideMap(tag); + } + +} diff --git a/src/java/main/org/apache/jute/compiler/CGenerator.java b/src/java/main/org/apache/jute/compiler/CGenerator.java new file mode 100644 index 0000000..689ee21 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/CGenerator.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.jute.compiler; + +import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; + +/** + * C++ Code generator front-end for Hadoop record I/O. + */ +class CGenerator { + private String mName; + private ArrayList mInclFiles; + private ArrayList 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, ArrayList ilist, ArrayList 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); + } + } + + FileWriter c = null; + FileWriter h = null; + try { + c = new FileWriter(new File(outputDirectory, mName+".c")); + 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"); + } finally { + try { + if (h != null) { + h.close(); + } + } finally { + if (c != null) { + c.close(); + } + } + } + } +} diff --git a/src/java/main/org/apache/jute/compiler/CSharpGenerator.java b/src/java/main/org/apache/jute/compiler/CSharpGenerator.java new file mode 100644 index 0000000..c28751d --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/CSharpGenerator.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.jute.compiler; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +public class CSharpGenerator { + private ArrayList 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, ArrayList ilist, ArrayList 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/src/java/main/org/apache/jute/compiler/CppGenerator.java b/src/java/main/org/apache/jute/compiler/CppGenerator.java new file mode 100644 index 0000000..98892a3 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/CppGenerator.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.jute.compiler; + +import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; + +/** + * C++ Code generator front-end for Hadoop record I/O. + */ +class CppGenerator { + private String mName; + private ArrayList mInclFiles; + private ArrayList 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, ArrayList ilist, ArrayList 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); + } + } + FileWriter cc = null; + FileWriter hh = null; + + try { + cc = new FileWriter(new File(outputDirectory, mName+".cc")); + 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"); + } finally { + try { + if (hh != null) { + hh.close(); + } + } finally { + if (cc != null) { + cc.close(); + } + } + } + } +} diff --git a/src/java/main/org/apache/jute/compiler/JBoolean.java b/src/java/main/org/apache/jute/compiler/JBoolean.java new file mode 100644 index 0000000..b45b161 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JBoolean.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.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/src/java/main/org/apache/jute/compiler/JBuffer.java b/src/java/main/org/apache/jute/compiler/JBuffer.java new file mode 100644 index 0000000..b2be5bd --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JBuffer.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.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/src/java/main/org/apache/jute/compiler/JByte.java b/src/java/main/org/apache/jute/compiler/JByte.java new file mode 100644 index 0000000..4b1cea4 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JByte.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.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/src/java/main/org/apache/jute/compiler/JCompType.java b/src/java/main/org/apache/jute/compiler/JCompType.java new file mode 100644 index 0000000..d98658f --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JCompType.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.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/src/java/main/org/apache/jute/compiler/JDouble.java b/src/java/main/org/apache/jute/compiler/JDouble.java new file mode 100644 index 0000000..21f9cc8 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JDouble.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 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/src/java/main/org/apache/jute/compiler/JField.java b/src/java/main/org/apache/jute/compiler/JField.java new file mode 100644 index 0000000..50f9fc9 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JField.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.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/src/java/main/org/apache/jute/compiler/JFile.java b/src/java/main/org/apache/jute/compiler/JFile.java new file mode 100644 index 0000000..e5b0ba3 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JFile.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.jute.compiler; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +/** + * 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 ArrayList mInclFiles; + private ArrayList 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/src/java/main/org/apache/jute/compiler/JFloat.java b/src/java/main/org/apache/jute/compiler/JFloat.java new file mode 100644 index 0000000..a4be6ec --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JFloat.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.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/src/java/main/org/apache/jute/compiler/JInt.java b/src/java/main/org/apache/jute/compiler/JInt.java new file mode 100644 index 0000000..23b902e --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JInt.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.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/src/java/main/org/apache/jute/compiler/JLong.java b/src/java/main/org/apache/jute/compiler/JLong.java new file mode 100644 index 0000000..342fd9a --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JLong.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.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/src/java/main/org/apache/jute/compiler/JMap.java b/src/java/main/org/apache/jute/compiler/JMap.java new file mode 100644 index 0000000..cc503be --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JMap.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.jute.compiler; + +/** + * + */ +public class JMap extends JCompType { + + static private int level = 0; + + static private String getLevel() { return Integer.toString(level); } + + static private void incrLevel() { level++; } + + static private void decrLevel() { level--; } + + static private 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/src/java/main/org/apache/jute/compiler/JRecord.java b/src/java/main/org/apache/jute/compiler/JRecord.java new file mode 100644 index 0000000..7322f21 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JRecord.java @@ -0,0 +1,776 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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; + +/** + * + */ +public class JRecord extends JCompType { + + private String mFQName; + private String mName; + private String mModule; + private ArrayList 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 ArrayList 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 HashMap 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 struct_name = JVector.extractVectorName(jvType); + if (vectorStructs.get(struct_name) == null) { + vectorStructs.put(struct_name, struct_name); + h.write("struct " + struct_name + " {\n int32_t count;\n" + jv.getElementType().genCDecl("*data") + "\n};\n"); + h.write("int serialize_" + struct_name + "(struct oarchive *out, const char *tag, struct " + struct_name + " *v);\n"); + h.write("int deserialize_" + struct_name + "(struct iarchive *in, const char *tag, struct " + struct_name + " *v);\n"); + h.write("int allocate_" + struct_name + "(struct " + struct_name + " *v, int32_t len);\n"); + h.write("int deallocate_" + struct_name + "(struct " + struct_name + " *v);\n"); + c.write("int allocate_" + struct_name + "(struct " + struct_name + " *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_" + struct_name + "(struct " + struct_name + " *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_" + struct_name + "(struct oarchive *out, const char *tag, struct " + struct_name + " *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_" + struct_name + "(struct iarchive *in, const char *tag, struct " + struct_name + " *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 rec_name = getName(); + h.write("struct " + rec_name + " {\n"); + for (JField f : mFields) { + h.write(f.genCDecl()); + } + h.write("};\n"); + h.write("int serialize_" + rec_name + "(struct oarchive *out, const char *tag, struct " + rec_name + " *v);\n"); + h.write("int deserialize_" + rec_name + "(struct iarchive *in, const char *tag, struct " + rec_name + "*v);\n"); + h.write("void deallocate_" + rec_name + "(struct " + rec_name + "*);\n"); + c.write("int serialize_" + rec_name + "(struct oarchive *out, const char *tag, struct " + rec_name + " *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_" + rec_name + "(struct iarchive *in, const char *tag, struct " + rec_name + "*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_" + rec_name + "(struct " + rec_name + "*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(); + } + + static private 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."); + } + File jfile = new File(pkgdir, getName()+".java"); + FileWriter jj = null; + try{ + jj = new FileWriter(jfile); + 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.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(" CsvOutputArchive a_ = \n"); + jj.write(" new CsvOutputArchive(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"); + } finally { + if (jj != null) { + jj.close(); + } + } + + } + + 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."); + } + File csharpFile = new File(outputDirectory, getName()+".cs"); + FileWriter cs = null; + + try { + cs = new FileWriter(csharpFile); + + 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"); + } finally { + if (cs != null) { + cs.close(); + } + } + } + + 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/src/java/main/org/apache/jute/compiler/JString.java b/src/java/main/org/apache/jute/compiler/JString.java new file mode 100644 index 0000000..7f246c3 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JString.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.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/src/java/main/org/apache/jute/compiler/JType.java b/src/java/main/org/apache/jute/compiler/JType.java new file mode 100644 index 0000000..ee1b9c0 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/JType.java @@ -0,0 +1,204 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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. + * + */ +abstract public 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 public String extractVectorName(JType jvType) { + return JRecord.extractMethodSuffix(jvType)+"_vector"; + } +} diff --git a/src/java/main/org/apache/jute/compiler/JavaGenerator.java b/src/java/main/org/apache/jute/compiler/JavaGenerator.java new file mode 100644 index 0000000..4078520 --- /dev/null +++ b/src/java/main/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.ArrayList; +import java.util.Iterator; + +/** + * Java Code generator front-end for Hadoop record I/O. + */ +class JavaGenerator { + private ArrayList 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, ArrayList incl, + ArrayList 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/src/java/main/org/apache/jute/compiler/generated/ParseException.java b/src/java/main/org/apache/jute/compiler/generated/ParseException.java new file mode 100644 index 0000000..e4b0a9b --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/ParseException.java @@ -0,0 +1,210 @@ +/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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; + +/** + * This exception is thrown when parse errors are encountered. + * You can explicitly create objects of this exception type by + * calling the method generateParseException in the generated + * parser. + * + * You can modify this class to customize your error reporting + * mechanisms so long as you retain the public fields. + */ +public class ParseException extends Exception { + + /** + * This constructor is used by the method "generateParseException" + * in the generated parser. Calling this constructor generates + * a new object of this type with the fields "currentToken", + * "expectedTokenSequences", and "tokenImage" set. The boolean + * flag "specialConstructor" is also set to true to indicate that + * this constructor was used to create this object. + * This constructor calls its super class with the empty string + * to force the "toString" method of parent class "Throwable" to + * print the error message in the form: + * ParseException: + */ + public ParseException(Token currentTokenVal, + int[][] expectedTokenSequencesVal, + String[] tokenImageVal + ) + { + super(""); + specialConstructor = true; + currentToken = currentTokenVal; + expectedTokenSequences = expectedTokenSequencesVal; + tokenImage = tokenImageVal; + } + + /** + * The following constructors are for use by you for whatever + * purpose you can think of. Constructing the exception in this + * manner makes the exception behave in the normal way - i.e., as + * documented in the class "Throwable". The fields "errorToken", + * "expectedTokenSequences", and "tokenImage" do not contain + * relevant information. The JavaCC generated code does not use + * these constructors. + */ + + public ParseException() { + super(); + specialConstructor = false; + } + + public ParseException(String message) { + super(message); + specialConstructor = false; + } + + /** + * This variable determines which constructor was used to create + * this object and thereby affects the semantics of the + * "getMessage" method (see below). + */ + protected boolean specialConstructor; + + /** + * This is the last token that has been consumed successfully. If + * this object has been created due to a parse error, the token + * followng this token will (therefore) be the first error token. + */ + public Token currentToken; + + /** + * Each entry in this array is an array of integers. Each array + * of integers represents a sequence of tokens (by their ordinal + * values) that is expected at this point of the parse. + */ + public int[][] expectedTokenSequences; + + /** + * This is a reference to the "tokenImage" array of the generated + * parser within which the parse error occurred. This array is + * defined in the generated ...Constants interface. + */ + public String[] tokenImage; + + /** + * This method has the standard behavior when this object has been + * created using the standard constructors. Otherwise, it uses + * "currentToken" and "expectedTokenSequences" to generate a parse + * error message and returns it. If this object has been created + * due to a parse error, and you do not catch it (it gets thrown + * from the parser), then this method is called during the printing + * of the final stack trace, and hence the correct error message + * gets displayed. + */ + public String getMessage() { + if (!specialConstructor) { + return super.getMessage(); + } + StringBuffer expected = new StringBuffer(); + int maxSize = 0; + for (int i = 0; i < expectedTokenSequences.length; i++) { + if (maxSize < expectedTokenSequences[i].length) { + maxSize = expectedTokenSequences[i].length; + } + for (int j = 0; j < expectedTokenSequences[i].length; j++) { + expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" "); + } + if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { + expected.append("..."); + } + expected.append(eol).append(" "); + } + String retval = "Encountered \""; + Token tok = currentToken.next; + for (int i = 0; i < maxSize; i++) { + if (i != 0) retval += " "; + if (tok.kind == 0) { + retval += tokenImage[0]; + break; + } + retval += add_escapes(tok.image); + tok = tok.next; + } + retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; + retval += "." + eol; + if (expectedTokenSequences.length == 1) { + retval += "Was expecting:" + eol + " "; + } else { + retval += "Was expecting one of:" + eol + " "; + } + retval += expected.toString(); + return retval; + } + + /** + * The end of line string for this machine. + */ + protected String eol = System.getProperty("line.separator", "\n"); + + /** + * Used to convert raw characters to their escaped version + * when these raw version cannot be used as part of an ASCII + * string literal. + */ + protected String add_escapes(String str) { + StringBuffer retval = new StringBuffer(); + char ch; + for (int i = 0; i < str.length(); i++) { + switch (str.charAt(i)) + { + case 0 : + continue; + case '\b': + retval.append("\\b"); + continue; + case '\t': + retval.append("\\t"); + continue; + case '\n': + retval.append("\\n"); + continue; + case '\f': + retval.append("\\f"); + continue; + case '\r': + retval.append("\\r"); + continue; + case '\"': + retval.append("\\\""); + continue; + case '\'': + retval.append("\\\'"); + continue; + case '\\': + retval.append("\\\\"); + continue; + default: + if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u" + s.substring(s.length() - 4, s.length())); + } else { + retval.append(ch); + } + continue; + } + } + return retval.toString(); + } + +} diff --git a/src/java/main/org/apache/jute/compiler/generated/Rcc.java b/src/java/main/org/apache/jute/compiler/generated/Rcc.java new file mode 100644 index 0000000..1bd4878 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/Rcc.java @@ -0,0 +1,525 @@ +/* Generated By:JavaCC: Do not edit this line. Rcc.java */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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.util.Iterator; +import java.io.File; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class Rcc implements RccConstants { + 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= 0) { + la1tokens[jj_kind] = true; + jj_kind = -1; + } + for (int i = 0; i < 6; i++) { + if (jj_la1[i] == jj_gen) { + for (int j = 0; j < 32; j++) { + if ((jj_la1_0[i] & (1<", + "\" \"", + "\"\\t\"", + "\"\\n\"", + "\"\\r\"", + "\"//\"", + "", + "", + "\"/*\"", + "\"*/\"", + "", + "\"module\"", + "\"class\"", + "\"include\"", + "\"byte\"", + "\"boolean\"", + "\"int\"", + "\"long\"", + "\"float\"", + "\"double\"", + "\"ustring\"", + "\"buffer\"", + "\"vector\"", + "\"map\"", + "\"{\"", + "\"}\"", + "\"<\"", + "\">\"", + "\";\"", + "\",\"", + "\".\"", + "", + "", + }; + +} diff --git a/src/java/main/org/apache/jute/compiler/generated/RccTokenManager.java b/src/java/main/org/apache/jute/compiler/generated/RccTokenManager.java new file mode 100644 index 0000000..5ae99e2 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/RccTokenManager.java @@ -0,0 +1,833 @@ +/* Generated By:JavaCC: Do not edit this line. RccTokenManager.java */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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.util.Iterator; +import java.io.File; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class RccTokenManager implements RccConstants +{ + public java.io.PrintStream debugStream = System.out; + public void setDebugStream(java.io.PrintStream ds) { debugStream = ds; } +private final int jjMoveStringLiteralDfa0_1() +{ + return jjMoveNfa_1(0, 0); +} +private final void jjCheckNAdd(int state) +{ + if (jjrounds[state] != jjround) + { + jjstateSet[jjnewStateCnt++] = state; + jjrounds[state] = jjround; + } +} +private final void jjAddStates(int start, int end) +{ + do { + jjstateSet[jjnewStateCnt++] = jjnextStates[start]; + } while (start++ != end); +} +private final void jjCheckNAddTwoStates(int state1, int state2) +{ + jjCheckNAdd(state1); + jjCheckNAdd(state2); +} +private final void jjCheckNAddStates(int start, int end) +{ + do { + jjCheckNAdd(jjnextStates[start]); + } while (start++ != end); +} +private final void jjCheckNAddStates(int start) +{ + jjCheckNAdd(jjnextStates[start]); + jjCheckNAdd(jjnextStates[start + 1]); +} +private final int jjMoveNfa_1(int startState, int curPos) +{ + int[] nextStates; + int startsAt = 0; + jjnewStateCnt = 3; + int i = 1; + jjstateSet[0] = startState; + int j, kind = 0x7fffffff; + for (;;) + { + if (++jjround == 0x7fffffff) + ReInitRounds(); + if (curChar < 64) + { + long l = 1L << curChar; + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 0: + if ((0x2400L & l) != 0L) + { + if (kind > 6) + kind = 6; + } + if (curChar == 13) + jjstateSet[jjnewStateCnt++] = 1; + break; + case 1: + if (curChar == 10 && kind > 6) + kind = 6; + break; + case 2: + if (curChar == 13) + jjstateSet[jjnewStateCnt++] = 1; + break; + default : break; + } + } while(i != startsAt); + } + else if (curChar < 128) + { + long l = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + default : break; + } + } while(i != startsAt); + } + else + { + int i2 = (curChar & 0xff) >> 6; + long l2 = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + default : break; + } + } while(i != startsAt); + } + if (kind != 0x7fffffff) + { + jjmatchedKind = kind; + jjmatchedPos = curPos; + kind = 0x7fffffff; + } + ++curPos; + if ((i = jjnewStateCnt) == (startsAt = 3 - (jjnewStateCnt = startsAt))) + return curPos; + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { return curPos; } + } +} +private final int jjStopStringLiteralDfa_0(int pos, long active0) +{ + switch (pos) + { + case 0: + if ((active0 & 0xfff800L) != 0L) + { + jjmatchedKind = 32; + return 4; + } + return -1; + case 1: + if ((active0 & 0xfff800L) != 0L) + { + jjmatchedKind = 32; + jjmatchedPos = 1; + return 4; + } + return -1; + case 2: + if ((active0 & 0x7ef800L) != 0L) + { + jjmatchedKind = 32; + jjmatchedPos = 2; + return 4; + } + if ((active0 & 0x810000L) != 0L) + return 4; + return -1; + case 3: + if ((active0 & 0x24000L) != 0L) + return 4; + if ((active0 & 0x7cb800L) != 0L) + { + jjmatchedKind = 32; + jjmatchedPos = 3; + return 4; + } + return -1; + case 4: + if ((active0 & 0x41000L) != 0L) + return 4; + if ((active0 & 0x78a800L) != 0L) + { + jjmatchedKind = 32; + jjmatchedPos = 4; + return 4; + } + return -1; + case 5: + if ((active0 & 0x680800L) != 0L) + return 4; + if ((active0 & 0x10a000L) != 0L) + { + jjmatchedKind = 32; + jjmatchedPos = 5; + return 4; + } + return -1; + default : + return -1; + } +} +private final int jjStartNfa_0(int pos, long active0) +{ + return jjMoveNfa_0(jjStopStringLiteralDfa_0(pos, active0), pos + 1); +} +private final int jjStopAtPos(int pos, int kind) +{ + jjmatchedKind = kind; + jjmatchedPos = pos; + return pos + 1; +} +private final int jjStartNfaWithStates_0(int pos, int kind, int state) +{ + jjmatchedKind = kind; + jjmatchedPos = pos; + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { return pos + 1; } + return jjMoveNfa_0(state, pos + 1); +} +private final int jjMoveStringLiteralDfa0_0() +{ + switch(curChar) + { + case 44: + return jjStopAtPos(0, 29); + case 46: + return jjStopAtPos(0, 30); + case 47: + return jjMoveStringLiteralDfa1_0(0x120L); + case 59: + return jjStopAtPos(0, 28); + case 60: + return jjStopAtPos(0, 26); + case 62: + return jjStopAtPos(0, 27); + case 98: + return jjMoveStringLiteralDfa1_0(0x20c000L); + case 99: + return jjMoveStringLiteralDfa1_0(0x1000L); + case 100: + return jjMoveStringLiteralDfa1_0(0x80000L); + case 102: + return jjMoveStringLiteralDfa1_0(0x40000L); + case 105: + return jjMoveStringLiteralDfa1_0(0x12000L); + case 108: + return jjMoveStringLiteralDfa1_0(0x20000L); + case 109: + return jjMoveStringLiteralDfa1_0(0x800800L); + case 117: + return jjMoveStringLiteralDfa1_0(0x100000L); + case 118: + return jjMoveStringLiteralDfa1_0(0x400000L); + case 123: + return jjStopAtPos(0, 24); + case 125: + return jjStopAtPos(0, 25); + default : + return jjMoveNfa_0(0, 0); + } +} +private final int jjMoveStringLiteralDfa1_0(long active0) +{ + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(0, active0); + return 1; + } + switch(curChar) + { + case 42: + if ((active0 & 0x100L) != 0L) + return jjStopAtPos(1, 8); + break; + case 47: + if ((active0 & 0x20L) != 0L) + return jjStopAtPos(1, 5); + break; + case 97: + return jjMoveStringLiteralDfa2_0(active0, 0x800000L); + case 101: + return jjMoveStringLiteralDfa2_0(active0, 0x400000L); + case 108: + return jjMoveStringLiteralDfa2_0(active0, 0x41000L); + case 110: + return jjMoveStringLiteralDfa2_0(active0, 0x12000L); + case 111: + return jjMoveStringLiteralDfa2_0(active0, 0xa8800L); + case 115: + return jjMoveStringLiteralDfa2_0(active0, 0x100000L); + case 117: + return jjMoveStringLiteralDfa2_0(active0, 0x200000L); + case 121: + return jjMoveStringLiteralDfa2_0(active0, 0x4000L); + default : + break; + } + return jjStartNfa_0(0, active0); +} +private final int jjMoveStringLiteralDfa2_0(long old0, long active0) +{ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(0, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(1, active0); + return 2; + } + switch(curChar) + { + case 97: + return jjMoveStringLiteralDfa3_0(active0, 0x1000L); + case 99: + return jjMoveStringLiteralDfa3_0(active0, 0x402000L); + case 100: + return jjMoveStringLiteralDfa3_0(active0, 0x800L); + case 102: + return jjMoveStringLiteralDfa3_0(active0, 0x200000L); + case 110: + return jjMoveStringLiteralDfa3_0(active0, 0x20000L); + case 111: + return jjMoveStringLiteralDfa3_0(active0, 0x48000L); + case 112: + if ((active0 & 0x800000L) != 0L) + return jjStartNfaWithStates_0(2, 23, 4); + break; + case 116: + if ((active0 & 0x10000L) != 0L) + return jjStartNfaWithStates_0(2, 16, 4); + return jjMoveStringLiteralDfa3_0(active0, 0x104000L); + case 117: + return jjMoveStringLiteralDfa3_0(active0, 0x80000L); + default : + break; + } + return jjStartNfa_0(1, active0); +} +private final int jjMoveStringLiteralDfa3_0(long old0, long active0) +{ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(1, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(2, active0); + return 3; + } + switch(curChar) + { + case 97: + return jjMoveStringLiteralDfa4_0(active0, 0x40000L); + case 98: + return jjMoveStringLiteralDfa4_0(active0, 0x80000L); + case 101: + if ((active0 & 0x4000L) != 0L) + return jjStartNfaWithStates_0(3, 14, 4); + break; + case 102: + return jjMoveStringLiteralDfa4_0(active0, 0x200000L); + case 103: + if ((active0 & 0x20000L) != 0L) + return jjStartNfaWithStates_0(3, 17, 4); + break; + case 108: + return jjMoveStringLiteralDfa4_0(active0, 0xa000L); + case 114: + return jjMoveStringLiteralDfa4_0(active0, 0x100000L); + case 115: + return jjMoveStringLiteralDfa4_0(active0, 0x1000L); + case 116: + return jjMoveStringLiteralDfa4_0(active0, 0x400000L); + case 117: + return jjMoveStringLiteralDfa4_0(active0, 0x800L); + default : + break; + } + return jjStartNfa_0(2, active0); +} +private final int jjMoveStringLiteralDfa4_0(long old0, long active0) +{ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(2, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(3, active0); + return 4; + } + switch(curChar) + { + case 101: + return jjMoveStringLiteralDfa5_0(active0, 0x208000L); + case 105: + return jjMoveStringLiteralDfa5_0(active0, 0x100000L); + case 108: + return jjMoveStringLiteralDfa5_0(active0, 0x80800L); + case 111: + return jjMoveStringLiteralDfa5_0(active0, 0x400000L); + case 115: + if ((active0 & 0x1000L) != 0L) + return jjStartNfaWithStates_0(4, 12, 4); + break; + case 116: + if ((active0 & 0x40000L) != 0L) + return jjStartNfaWithStates_0(4, 18, 4); + break; + case 117: + return jjMoveStringLiteralDfa5_0(active0, 0x2000L); + default : + break; + } + return jjStartNfa_0(3, active0); +} +private final int jjMoveStringLiteralDfa5_0(long old0, long active0) +{ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(3, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(4, active0); + return 5; + } + switch(curChar) + { + case 97: + return jjMoveStringLiteralDfa6_0(active0, 0x8000L); + case 100: + return jjMoveStringLiteralDfa6_0(active0, 0x2000L); + case 101: + if ((active0 & 0x800L) != 0L) + return jjStartNfaWithStates_0(5, 11, 4); + else if ((active0 & 0x80000L) != 0L) + return jjStartNfaWithStates_0(5, 19, 4); + break; + case 110: + return jjMoveStringLiteralDfa6_0(active0, 0x100000L); + case 114: + if ((active0 & 0x200000L) != 0L) + return jjStartNfaWithStates_0(5, 21, 4); + else if ((active0 & 0x400000L) != 0L) + return jjStartNfaWithStates_0(5, 22, 4); + break; + default : + break; + } + return jjStartNfa_0(4, active0); +} +private final int jjMoveStringLiteralDfa6_0(long old0, long active0) +{ + if (((active0 &= old0)) == 0L) + return jjStartNfa_0(4, old0); + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(5, active0); + return 6; + } + switch(curChar) + { + case 101: + if ((active0 & 0x2000L) != 0L) + return jjStartNfaWithStates_0(6, 13, 4); + break; + case 103: + if ((active0 & 0x100000L) != 0L) + return jjStartNfaWithStates_0(6, 20, 4); + break; + case 110: + if ((active0 & 0x8000L) != 0L) + return jjStartNfaWithStates_0(6, 15, 4); + break; + default : + break; + } + return jjStartNfa_0(5, active0); +} +static final long[] jjbitVec0 = { + 0x0L, 0x0L, 0xffffffffffffffffL, 0xffffffffffffffffL +}; +private final int jjMoveNfa_0(int startState, int curPos) +{ + int[] nextStates; + int startsAt = 0; + jjnewStateCnt = 5; + int i = 1; + jjstateSet[0] = startState; + int j, kind = 0x7fffffff; + for (;;) + { + if (++jjround == 0x7fffffff) + ReInitRounds(); + if (curChar < 64) + { + long l = 1L << curChar; + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 0: + if (curChar == 34) + jjCheckNAdd(1); + break; + case 1: + if ((0xfffffffbffffffffL & l) != 0L) + jjCheckNAddTwoStates(1, 2); + break; + case 2: + if (curChar == 34 && kind > 31) + kind = 31; + break; + case 4: + if ((0x3ff000000000000L & l) == 0L) + break; + if (kind > 32) + kind = 32; + jjstateSet[jjnewStateCnt++] = 4; + break; + default : break; + } + } while(i != startsAt); + } + else if (curChar < 128) + { + long l = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 0: + if ((0x7fffffe07fffffeL & l) == 0L) + break; + if (kind > 32) + kind = 32; + jjCheckNAdd(4); + break; + case 1: + jjAddStates(0, 1); + break; + case 4: + if ((0x7fffffe87fffffeL & l) == 0L) + break; + if (kind > 32) + kind = 32; + jjCheckNAdd(4); + break; + default : break; + } + } while(i != startsAt); + } + else + { + int i2 = (curChar & 0xff) >> 6; + long l2 = 1L << (curChar & 077); + MatchLoop: do + { + switch(jjstateSet[--i]) + { + case 1: + if ((jjbitVec0[i2] & l2) != 0L) + jjAddStates(0, 1); + break; + default : break; + } + } while(i != startsAt); + } + if (kind != 0x7fffffff) + { + jjmatchedKind = kind; + jjmatchedPos = curPos; + kind = 0x7fffffff; + } + ++curPos; + if ((i = jjnewStateCnt) == (startsAt = 5 - (jjnewStateCnt = startsAt))) + return curPos; + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { return curPos; } + } +} +private final int jjMoveStringLiteralDfa0_2() +{ + switch(curChar) + { + case 42: + return jjMoveStringLiteralDfa1_2(0x200L); + default : + return 1; + } +} +private final int jjMoveStringLiteralDfa1_2(long active0) +{ + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + return 1; + } + switch(curChar) + { + case 47: + if ((active0 & 0x200L) != 0L) + return jjStopAtPos(1, 9); + break; + default : + return 2; + } + return 2; +} +static final int[] jjnextStates = { + 1, 2, +}; +public static final String[] jjstrLiteralImages = { +"", null, null, null, null, null, null, null, null, null, null, +"\155\157\144\165\154\145", "\143\154\141\163\163", "\151\156\143\154\165\144\145", "\142\171\164\145", +"\142\157\157\154\145\141\156", "\151\156\164", "\154\157\156\147", "\146\154\157\141\164", +"\144\157\165\142\154\145", "\165\163\164\162\151\156\147", "\142\165\146\146\145\162", +"\166\145\143\164\157\162", "\155\141\160", "\173", "\175", "\74", "\76", "\73", "\54", "\56", null, null, }; +public static final String[] lexStateNames = { + "DEFAULT", + "WithinOneLineComment", + "WithinMultiLineComment", +}; +public static final int[] jjnewLexState = { + -1, -1, -1, -1, -1, 1, 0, -1, 2, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, +}; +static final long[] jjtoToken = { + 0x1fffff801L, +}; +static final long[] jjtoSkip = { + 0x37eL, +}; +static final long[] jjtoSpecial = { + 0x360L, +}; +static final long[] jjtoMore = { + 0x480L, +}; +protected SimpleCharStream input_stream; +private final int[] jjrounds = new int[5]; +private final int[] jjstateSet = new int[10]; +StringBuffer image; +int jjimageLen; +int lengthOfMatch; +protected char curChar; +public RccTokenManager(SimpleCharStream stream){ + if (SimpleCharStream.staticFlag) + throw new Error("ERROR: Cannot use a static CharStream class with a non-static lexical analyzer."); + input_stream = stream; +} +public RccTokenManager(SimpleCharStream stream, int lexState){ + this(stream); + SwitchTo(lexState); +} +public void ReInit(SimpleCharStream stream) +{ + jjmatchedPos = jjnewStateCnt = 0; + curLexState = defaultLexState; + input_stream = stream; + ReInitRounds(); +} +private final void ReInitRounds() +{ + int i; + jjround = 0x80000001; + for (i = 5; i-- > 0;) + jjrounds[i] = 0x80000000; +} +public void ReInit(SimpleCharStream stream, int lexState) +{ + ReInit(stream); + SwitchTo(lexState); +} +public void SwitchTo(int lexState) +{ + if (lexState >= 3 || lexState < 0) + throw new TokenMgrError("Error: Ignoring invalid lexical state : " + lexState + ". State unchanged.", TokenMgrError.INVALID_LEXICAL_STATE); + else + curLexState = lexState; +} + +protected Token jjFillToken() +{ + Token t = Token.newToken(jjmatchedKind); + t.kind = jjmatchedKind; + String im = jjstrLiteralImages[jjmatchedKind]; + t.image = (im == null) ? input_stream.GetImage() : im; + t.beginLine = input_stream.getBeginLine(); + t.beginColumn = input_stream.getBeginColumn(); + t.endLine = input_stream.getEndLine(); + t.endColumn = input_stream.getEndColumn(); + return t; +} + +int curLexState = 0; +int defaultLexState = 0; +int jjnewStateCnt; +int jjround; +int jjmatchedPos; +int jjmatchedKind; + +public Token getNextToken() +{ + int kind; + Token specialToken = null; + Token matchedToken; + int curPos = 0; + + EOFLoop : + for (;;) + { + try + { + curChar = input_stream.BeginToken(); + } + catch(java.io.IOException e) + { + jjmatchedKind = 0; + matchedToken = jjFillToken(); + matchedToken.specialToken = specialToken; + return matchedToken; + } + image = null; + jjimageLen = 0; + + for (;;) + { + switch(curLexState) + { + case 0: + try { input_stream.backup(0); + while (curChar <= 32 && (0x100002600L & (1L << curChar)) != 0L) + curChar = input_stream.BeginToken(); + } + catch (java.io.IOException e1) { continue EOFLoop; } + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_0(); + break; + case 1: + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_1(); + if (jjmatchedPos == 0 && jjmatchedKind > 7) + { + jjmatchedKind = 7; + } + break; + case 2: + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_2(); + if (jjmatchedPos == 0 && jjmatchedKind > 10) + { + jjmatchedKind = 10; + } + break; + } + if (jjmatchedKind != 0x7fffffff) + { + if (jjmatchedPos + 1 < curPos) + input_stream.backup(curPos - jjmatchedPos - 1); + if ((jjtoToken[jjmatchedKind >> 6] & (1L << (jjmatchedKind & 077))) != 0L) + { + matchedToken = jjFillToken(); + matchedToken.specialToken = specialToken; + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + return matchedToken; + } + else if ((jjtoSkip[jjmatchedKind >> 6] & (1L << (jjmatchedKind & 077))) != 0L) + { + if ((jjtoSpecial[jjmatchedKind >> 6] & (1L << (jjmatchedKind & 077))) != 0L) + { + matchedToken = jjFillToken(); + if (specialToken == null) + specialToken = matchedToken; + else + { + matchedToken.specialToken = specialToken; + specialToken = (specialToken.next = matchedToken); + } + SkipLexicalActions(matchedToken); + } + else + SkipLexicalActions(null); + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + continue EOFLoop; + } + jjimageLen += jjmatchedPos + 1; + if (jjnewLexState[jjmatchedKind] != -1) + curLexState = jjnewLexState[jjmatchedKind]; + curPos = 0; + jjmatchedKind = 0x7fffffff; + try { + curChar = input_stream.readChar(); + continue; + } + catch (java.io.IOException e1) { } + } + int error_line = input_stream.getEndLine(); + int error_column = input_stream.getEndColumn(); + String error_after = null; + boolean EOFSeen = false; + try { input_stream.readChar(); input_stream.backup(1); } + catch (java.io.IOException e1) { + EOFSeen = true; + error_after = curPos <= 1 ? "" : input_stream.GetImage(); + if (curChar == '\n' || curChar == '\r') { + error_line++; + error_column = 0; + } + else + error_column++; + } + if (!EOFSeen) { + input_stream.backup(1); + error_after = curPos <= 1 ? "" : input_stream.GetImage(); + } + throw new TokenMgrError(EOFSeen, curLexState, error_line, error_column, error_after, curChar, TokenMgrError.LEXICAL_ERROR); + } + } +} + +void SkipLexicalActions(Token matchedToken) +{ + switch(jjmatchedKind) + { + default : + break; + } +} +} diff --git a/src/java/main/org/apache/jute/compiler/generated/SimpleCharStream.java b/src/java/main/org/apache/jute/compiler/generated/SimpleCharStream.java new file mode 100644 index 0000000..e38b451 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/SimpleCharStream.java @@ -0,0 +1,457 @@ +/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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; + +/** + * An implementation of interface CharStream, where the stream is assumed to + * contain only ASCII characters (without unicode processing). + */ + +public class SimpleCharStream +{ + public static final boolean staticFlag = false; + int bufsize; + int available; + int tokenBegin; + public int bufpos = -1; + protected int bufline[]; + protected int bufcolumn[]; + + protected int column = 0; + protected int line = 1; + + protected boolean prevCharIsCR = false; + protected boolean prevCharIsLF = false; + + protected java.io.Reader inputStream; + + protected char[] buffer; + protected int maxNextCharInd = 0; + protected int inBuf = 0; + protected int tabSize = 8; + + protected void setTabSize(int i) { tabSize = i; } + protected int getTabSize(int i) { return tabSize; } + + + protected void ExpandBuff(boolean wrapAround) + { + char[] newbuffer = new char[bufsize + 2048]; + int newbufline[] = new int[bufsize + 2048]; + int newbufcolumn[] = new int[bufsize + 2048]; + + try + { + if (wrapAround) + { + System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); + System.arraycopy(buffer, 0, newbuffer, + bufsize - tokenBegin, bufpos); + buffer = newbuffer; + + System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); + System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); + bufline = newbufline; + + System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); + System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); + bufcolumn = newbufcolumn; + + maxNextCharInd = (bufpos += (bufsize - tokenBegin)); + } + else + { + System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); + buffer = newbuffer; + + System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); + bufline = newbufline; + + System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); + bufcolumn = newbufcolumn; + + maxNextCharInd = (bufpos -= tokenBegin); + } + } + catch (Throwable t) + { + throw new Error(t.getMessage()); + } + + + bufsize += 2048; + available = bufsize; + tokenBegin = 0; + } + + protected void FillBuff() throws java.io.IOException + { + if (maxNextCharInd == available) + { + if (available == bufsize) + { + if (tokenBegin > 2048) + { + bufpos = maxNextCharInd = 0; + available = tokenBegin; + } + else if (tokenBegin < 0) + bufpos = maxNextCharInd = 0; + else + ExpandBuff(false); + } + else if (available > tokenBegin) + available = bufsize; + else if ((tokenBegin - available) < 2048) + ExpandBuff(true); + else + available = tokenBegin; + } + + int i; + try { + if ((i = inputStream.read(buffer, maxNextCharInd, + available - maxNextCharInd)) == -1) + { + inputStream.close(); + throw new java.io.IOException(); + } + else + maxNextCharInd += i; + return; + } + catch(java.io.IOException e) { + --bufpos; + backup(0); + if (tokenBegin == -1) + tokenBegin = bufpos; + throw e; + } + } + + public char BeginToken() throws java.io.IOException + { + tokenBegin = -1; + char c = readChar(); + tokenBegin = bufpos; + + return c; + } + + protected void UpdateLineColumn(char c) + { + column++; + + if (prevCharIsLF) + { + prevCharIsLF = false; + line += (column = 1); + } + else if (prevCharIsCR) + { + prevCharIsCR = false; + if (c == '\n') + { + prevCharIsLF = true; + } + else + line += (column = 1); + } + + switch (c) + { + case '\r' : + prevCharIsCR = true; + break; + case '\n' : + prevCharIsLF = true; + break; + case '\t' : + column--; + column += (tabSize - (column % tabSize)); + break; + default : + break; + } + + bufline[bufpos] = line; + bufcolumn[bufpos] = column; + } + + public char readChar() throws java.io.IOException + { + if (inBuf > 0) + { + --inBuf; + + if (++bufpos == bufsize) + bufpos = 0; + + return buffer[bufpos]; + } + + if (++bufpos >= maxNextCharInd) + FillBuff(); + + char c = buffer[bufpos]; + + UpdateLineColumn(c); + return (c); + } + + /** + * @deprecated + * @see #getEndColumn + */ + + public int getColumn() { + return bufcolumn[bufpos]; + } + + /** + * @deprecated + * @see #getEndLine + */ + + public int getLine() { + return bufline[bufpos]; + } + + public int getEndColumn() { + return bufcolumn[bufpos]; + } + + public int getEndLine() { + return bufline[bufpos]; + } + + public int getBeginColumn() { + return bufcolumn[tokenBegin]; + } + + public int getBeginLine() { + return bufline[tokenBegin]; + } + + public void backup(int amount) { + + inBuf += amount; + if ((bufpos -= amount) < 0) + bufpos += bufsize; + } + + public SimpleCharStream(java.io.Reader dstream, int startline, + int startcolumn, int buffersize) + { + inputStream = dstream; + line = startline; + column = startcolumn - 1; + + available = bufsize = buffersize; + buffer = new char[buffersize]; + bufline = new int[buffersize]; + bufcolumn = new int[buffersize]; + } + + public SimpleCharStream(java.io.Reader dstream, int startline, + int startcolumn) + { + this(dstream, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.Reader dstream) + { + this(dstream, 1, 1, 4096); + } + public void ReInit(java.io.Reader dstream, int startline, + int startcolumn, int buffersize) + { + inputStream = dstream; + line = startline; + column = startcolumn - 1; + + if (buffer == null || buffersize != buffer.length) + { + available = bufsize = buffersize; + buffer = new char[buffersize]; + bufline = new int[buffersize]; + bufcolumn = new int[buffersize]; + } + prevCharIsLF = prevCharIsCR = false; + tokenBegin = inBuf = maxNextCharInd = 0; + bufpos = -1; + } + + public void ReInit(java.io.Reader dstream, int startline, + int startcolumn) + { + ReInit(dstream, startline, startcolumn, 4096); + } + + public void ReInit(java.io.Reader dstream) + { + ReInit(dstream, 1, 1, 4096); + } + public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, + int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException + { + this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); + } + + public SimpleCharStream(java.io.InputStream dstream, int startline, + int startcolumn, int buffersize) + { + this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); + } + + public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, + int startcolumn) throws java.io.UnsupportedEncodingException + { + this(dstream, encoding, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream, int startline, + int startcolumn) + { + this(dstream, startline, startcolumn, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException + { + this(dstream, encoding, 1, 1, 4096); + } + + public SimpleCharStream(java.io.InputStream dstream) + { + this(dstream, 1, 1, 4096); + } + + public void ReInit(java.io.InputStream dstream, String encoding, int startline, + int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException + { + ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); + } + + public void ReInit(java.io.InputStream dstream, int startline, + int startcolumn, int buffersize) + { + ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); + } + + public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException + { + ReInit(dstream, encoding, 1, 1, 4096); + } + + public void ReInit(java.io.InputStream dstream) + { + ReInit(dstream, 1, 1, 4096); + } + public void ReInit(java.io.InputStream dstream, String encoding, int startline, + int startcolumn) throws java.io.UnsupportedEncodingException + { + ReInit(dstream, encoding, startline, startcolumn, 4096); + } + public void ReInit(java.io.InputStream dstream, int startline, + int startcolumn) + { + ReInit(dstream, startline, startcolumn, 4096); + } + public String GetImage() + { + if (bufpos >= tokenBegin) + return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); + else + return new String(buffer, tokenBegin, bufsize - tokenBegin) + + new String(buffer, 0, bufpos + 1); + } + + public char[] GetSuffix(int len) + { + char[] ret = new char[len]; + + if ((bufpos + 1) >= len) + System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); + else + { + System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, + len - bufpos - 1); + System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); + } + + return ret; + } + + public void Done() + { + buffer = null; + bufline = null; + bufcolumn = null; + } + + /** + * Method to adjust line and column numbers for the start of a token. + */ + public void adjustBeginLineColumn(int newLine, int newCol) + { + int start = tokenBegin; + int len; + + if (bufpos >= tokenBegin) + { + len = bufpos - tokenBegin + inBuf + 1; + } + else + { + len = bufsize - tokenBegin + bufpos + 1 + inBuf; + } + + int i = 0, j = 0, k = 0; + int nextColDiff = 0, columnDiff = 0; + + while (i < len && + bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) + { + bufline[j] = newLine; + nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; + bufcolumn[j] = newCol + columnDiff; + columnDiff = nextColDiff; + i++; + } + + if (i < len) + { + bufline[j] = newLine++; + bufcolumn[j] = newCol + columnDiff; + + while (i++ < len) + { + if (bufline[j = start % bufsize] != bufline[++start % bufsize]) + bufline[j] = newLine++; + else + bufline[j] = newLine; + } + } + + line = bufline[j]; + column = bufcolumn[j]; + } + +} diff --git a/src/java/main/org/apache/jute/compiler/generated/Token.java b/src/java/main/org/apache/jute/compiler/generated/Token.java new file mode 100644 index 0000000..3e7018b --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/Token.java @@ -0,0 +1,99 @@ +/* Generated By:JavaCC: Do not edit this line. Token.java Version 3.0 */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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; + +/** + * Describes the input token stream. + */ + +public class Token { + + /** + * An integer that describes the kind of this token. This numbering + * system is determined by JavaCCParser, and a table of these numbers is + * stored in the file ...Constants.java. + */ + public int kind; + + /** + * beginLine and beginColumn describe the position of the first character + * of this token; endLine and endColumn describe the position of the + * last character of this token. + */ + public int beginLine, beginColumn, endLine, endColumn; + + /** + * The string image of the token. + */ + public String image; + + /** + * A reference to the next regular (non-special) token from the input + * stream. If this is the last token from the input stream, or if the + * token manager has not read tokens beyond this one, this field is + * set to null. This is true only if this token is also a regular + * token. Otherwise, see below for a description of the contents of + * this field. + */ + public Token next; + + /** + * This field is used to access special tokens that occur prior to this + * token, but after the immediately preceding regular (non-special) token. + * If there are no such special tokens, this field is set to null. + * When there are more than one such special token, this field refers + * to the last of these special tokens, which in turn refers to the next + * previous special token through its specialToken field, and so on + * until the first special token (whose specialToken field is null). + * The next fields of special tokens refer to other special tokens that + * immediately follow it (without an intervening regular token). If there + * is no such token, this field is null. + */ + public Token specialToken; + + /** + * Returns the image. + */ + public String toString() + { + return image; + } + + /** + * Returns a new Token object, by default. However, if you want, you + * can create and return subclass objects based on the value of ofKind. + * Simply add the cases to the switch for all those special cases. + * For example, if you have a subclass of Token called IDToken that + * you want to create if ofKind is ID, simlpy add something like : + * + * case MyParserConstants.ID : return new IDToken(); + * + * to the following switch statement. Then you can cast matchedToken + * variable to the appropriate type and use it in your lexical actions. + */ + public static final Token newToken(int ofKind) + { + switch(ofKind) + { + default : return new Token(); + } + } + +} diff --git a/src/java/main/org/apache/jute/compiler/generated/TokenMgrError.java b/src/java/main/org/apache/jute/compiler/generated/TokenMgrError.java new file mode 100644 index 0000000..e246962 --- /dev/null +++ b/src/java/main/org/apache/jute/compiler/generated/TokenMgrError.java @@ -0,0 +1,151 @@ +/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 3.0 */ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS 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; + +public class TokenMgrError extends Error +{ + /* + * Ordinals for various reasons why an Error of this type can be thrown. + */ + + /** + * Lexical error occured. + */ + static final int LEXICAL_ERROR = 0; + + /** + * An attempt wass made to create a second instance of a static token manager. + */ + static final int STATIC_LEXER_ERROR = 1; + + /** + * Tried to change to an invalid lexical state. + */ + static final int INVALID_LEXICAL_STATE = 2; + + /** + * Detected (and bailed out of) an infinite loop in the token manager. + */ + static final int LOOP_DETECTED = 3; + + /** + * Indicates the reason why the exception is thrown. It will have + * one of the above 4 values. + */ + int errorCode; + + /** + * Replaces unprintable characters by their espaced (or unicode escaped) + * equivalents in the given string + */ + protected static final String addEscapes(String str) { + StringBuffer retval = new StringBuffer(); + char ch; + for (int i = 0; i < str.length(); i++) { + switch (str.charAt(i)) + { + case 0 : + continue; + case '\b': + retval.append("\\b"); + continue; + case '\t': + retval.append("\\t"); + continue; + case '\n': + retval.append("\\n"); + continue; + case '\f': + retval.append("\\f"); + continue; + case '\r': + retval.append("\\r"); + continue; + case '\"': + retval.append("\\\""); + continue; + case '\'': + retval.append("\\\'"); + continue; + case '\\': + retval.append("\\\\"); + continue; + default: + if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u" + s.substring(s.length() - 4, s.length())); + } else { + retval.append(ch); + } + continue; + } + } + return retval.toString(); + } + + /** + * Returns a detailed message for the Error when it is thrown by the + * token manager to indicate a lexical error. + * Parameters : + * EOFSeen : indicates if EOF caused the lexicl error + * curLexState : lexical state in which this error occured + * errorLine : line number when the error occured + * errorColumn : column number when the error occured + * errorAfter : prefix that was seen before this error occured + * curchar : the offending character + * Note: You can customize the lexical error message by modifying this method. + */ + protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) { + return("Lexical error at line " + + errorLine + ", column " + + errorColumn + ". Encountered: " + + (EOFSeen ? " " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + + "after : \"" + addEscapes(errorAfter) + "\""); + } + + /** + * You can also modify the body of this method to customize your error messages. + * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not + * of end-users concern, so you can return something like : + * + * "Internal Error : Please file a bug report .... " + * + * from this method for such cases in the release version of your parser. + */ + public String getMessage() { + return super.getMessage(); + } + + /* + * Constructors of various flavors follow. + */ + + public TokenMgrError() { + } + + public TokenMgrError(String message, int reason) { + super(message); + errorCode = reason; + } + + public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) { + this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason); + } +} diff --git a/src/java/main/org/apache/jute/compiler/generated/package.html b/src/java/main/org/apache/jute/compiler/generated/package.html new file mode 100644 index 0000000..8ef8a8c --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/jute/compiler/generated/rcc.jj b/src/java/main/org/apache/jute/compiler/generated/rcc.jj new file mode 100644 index 0000000..94d4f42 --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/jute/compiler/package.html b/src/java/main/org/apache/jute/compiler/package.html new file mode 100644 index 0000000..03bdb1b --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/jute/package.html b/src/java/main/org/apache/jute/package.html new file mode 100644 index 0000000..531a6e3 --- /dev/null +++ b/src/java/main/org/apache/jute/package.html @@ -0,0 +1,801 @@ + + + + + + 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, kXML, kCSV };
    +
    +  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, three data encodings are supported, namely binary, CSV and XML. + +

    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. +
    + + +

    CSV Serialization Format

    + +The CSV serialization format has a lot more structure than the "standard" +Excel CSV format, but we believe the additional structure is useful because + +
      +
    • it makes parsing a lot easier without detracting too much from legibility +
    • the delimiters around composites make it obvious when one is reading a +sequence of Hadoop records +
    + +Serialization formats for the various types are detailed in the grammar that +follows. The notable feature of the formats is the use of delimiters for +indicating the certain field types. + +
      +
    • A string field begins with a single quote ('). +
    • A buffer field begins with a sharp (#). +
    • A class, vector or map begins with 's{', 'v{' or 'm{' respectively and +ends with '}'. +
    + +The CSV format can be described by the following grammar: + +
    
    +record = primitive / struct / vector / map
    +primitive = boolean / int / long / float / double / ustring / buffer
    +
    +boolean = "T" / "F"
    +int = ["-"] 1*DIGIT
    +long = ";" ["-"] 1*DIGIT
    +float = ["-"] 1*DIGIT "." 1*DIGIT ["E" / "e" ["-"] 1*DIGIT]
    +double = ";" ["-"] 1*DIGIT "." 1*DIGIT ["E" / "e" ["-"] 1*DIGIT]
    +
    +ustring = "'" *(UTF8 char except NULL, LF, % and , / "%00" / "%0a" / "%25" / "%2c" )
    +
    +buffer = "#" *(BYTE except NULL, LF, % and , / "%00" / "%0a" / "%25" / "%2c" )
    +
    +struct = "s{" record *("," record) "}"
    +vector = "v{" [record *("," record)] "}"
    +map = "m{" [*(record "," record)] "}"
    +
    + +

    XML Serialization Format

    + +The XML serialization format is the same used by Apache XML-RPC +(http://ws.apache.org/xmlrpc/types.html). This is an extension of the original +XML-RPC format and adds some additional data types. All record I/O types are +not directly expressible in this format, and access to a DDL is required in +order to convert these to valid types. All types primitive or composite are +represented by <value> elements. The particular XML-RPC type is +indicated by a nested element in the <value> element. The encoding for +records is always UTF-8. Primitive types are serialized as follows: + +
      +
    • byte: XML tag <ex:i1>. Values: 1-byte unsigned +integers represented in US-ASCII +
    • boolean: XML tag <boolean>. Values: "0" or "1" +
    • int: XML tags <i4> or <int>. Values: 4-byte +signed integers represented in US-ASCII. +
    • long: XML tag <ex:i8>. Values: 8-byte signed integers +represented in US-ASCII. +
    • float: XML tag <ex:float>. Values: Single precision +floating point numbers represented in US-ASCII. +
    • double: XML tag <double>. Values: Double precision +floating point numbers represented in US-ASCII. +
    • ustring: XML tag <;string>. Values: String values +represented as UTF-8. XML does not permit all Unicode characters in literal +data. In particular, NULLs and control chars are not allowed. Additionally, +XML processors are required to replace carriage returns with line feeds and to +replace CRLF sequences with line feeds. Programming languages that we work +with do not impose these restrictions on string types. To work around these +restrictions, disallowed characters and CRs are percent escaped in strings. +The '%' character is also percent escaped. +
    • buffer: XML tag <string&>. Values: Arbitrary binary +data. Represented as hexBinary, each byte is replaced by its 2-byte +hexadecimal representation. +
    + +Composite types are serialized as follows: + +
      +
    • class: XML tag <struct>. A struct is a sequence of +<member> elements. Each <member> element has a <name> +element and a <value> element. The <name> is a string that must +match /[a-zA-Z][a-zA-Z0-9_]*/. The value of the member is represented +by a <value> element. + +
    • vector: XML tag <array<. An <array> contains a +single <data> element. The <data> element is a sequence of +<value> elements each of which represents an element of the vector. + +
    • map: XML tag <array>. Same as vector. + +
    + +For example: + +
    
    +class {
    +  int           MY_INT;            // value 5
    +  vector MY_VEC;            // values 0.1, -0.89, 2.45e4
    +  buffer        MY_BUF;            // value '\00\n\tabc%'
    +}
    +
    + +is serialized as + +
    
    +<value>
    +  <struct>
    +    <member>
    +      <name>MY_INT</name>
    +      <value><i4>5</i4></value>
    +    </member>
    +    <member>
    +      <name>MY_VEC</name>
    +      <value>
    +        <array>
    +          <data>
    +            <value><ex:float>0.1</ex:float></value>
    +            <value><ex:float>-0.89</ex:float></value>
    +            <value><ex:float>2.45e4</ex:float></value>
    +          </data>
    +        </array>
    +      </value>
    +    </member>
    +    <member>
    +      <name>MY_BUF</name>
    +      <value><string>%00\n\tabc%25</string></value>
    +    </member>
    +  </struct>
    +</value> 
    +
    + + + diff --git a/src/java/main/org/apache/zookeeper/AsyncCallback.java b/src/java/main/org/apache/zookeeper/AsyncCallback.java new file mode 100644 index 0000000..f02a824 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/AsyncCallback.java @@ -0,0 +1,309 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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. + * An asynchronous callback is deferred to invoke after a function returns. + * Asynchronous calls usually improve system efficiency on IO-related APIs. + *

    + * ZooKeeper provides asynchronous version as equivalent to synchronous APIs. + */ +@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 org.apache.zookeeper.KeeperException.Code#OK}. + *

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

      + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls. + *
    • + *
    • + * {@link org.apache.zookeeper.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 org.apache.zookeeper.data.Stat} object of + * the node on given path. + */ + public void processResult(int rc, String path, Object ctx, Stat stat); + } + + /** + * This callback is used to retrieve the data and stat of the node. + */ + @InterfaceAudience.Public + interface DataCallback extends AsyncCallback { + /** + * Process the result of the asynchronous call. + *

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

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

      + *
    • + * {@link org.apache.zookeeper.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 {@link org.apache.zookeeper.server.DataNode#data} + * of the node. + * @param stat {@link org.apache.zookeeper.data.Stat} object of + * the node on given path. + */ + public 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 org.apache.zookeeper.KeeperException.Code#OK}. + *

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

      + *
    • + * {@link org.apache.zookeeper.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 org.apache.zookeeper.ZooDefs.Ids}. + * @param stat {@link org.apache.zookeeper.data.Stat} object of + * the node on given path. + */ + public 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 org.apache.zookeeper.KeeperException.Code#OK}. + *

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

      + *
    • + * {@link org.apache.zookeeper.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. + */ + public 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. + * See {@link org.apache.zookeeper.AsyncCallback.ChildrenCallback}. + * + * @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 org.apache.zookeeper.data.Stat} object of + * the node on given path. + */ + public void processResult(int rc, String path, Object ctx, + List children, 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 org.apache.zookeeper.KeeperException.Code#OK}. + *

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

      + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#NODEEXISTS} + * - The node on give path already exists for some API calls. + *
    • + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls. + *
    • + *
    • + * {@link + * org.apache.zookeeper.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. + */ + public 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 + * org.apache.zookeeper.ZooKeeper#sync(String, + * org.apache.zookeeper.AsyncCallback.VoidCallback, Object)}. + */ + @InterfaceAudience.Public + interface VoidCallback extends AsyncCallback { + /** + * Process the result of the asynchronous call. + *

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

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

      + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls. + *
    • + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#BADVERSION} + * - The given version doesn't match the node's version + * for some API calls. + *
    • + *
    • + * {@link org.apache.zookeeper.KeeperException.Code#NOTEMPTY} + * - the node has children and some API calls cannnot succeed, + * e.g. {@link + * org.apache.zookeeper.ZooKeeper#delete(String, int, + * org.apache.zookeeper.AsyncCallback.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. + */ + public void processResult(int rc, String path, Object ctx); + } + + /** + * This callback is used to process the multiple results from + * a single multi call. + * See {@link org.apache.zookeeper.ZooKeeper#multi} for more information. + * @since 3.4.7 + */ + @InterfaceAudience.Public + interface MultiCallback extends AsyncCallback { + /** + * Process the result of the asynchronous call. + *

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

    + * On failure, rc is a failure code in + * {@link org.apache.zookeeper.KeeperException.Code}. + * All opResults are + * {@link org.apache.zookeeper.OpResult.ErrorResult}. + * All operations will be rollback-ed 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. + */ + public void processResult(int rc, String path, Object ctx, + List opResults); + } +} diff --git a/src/java/main/org/apache/zookeeper/ClientCnxn.java b/src/java/main/org/apache/zookeeper/ClientCnxn.java new file mode 100644 index 0000000..447a3ee --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ClientCnxn.java @@ -0,0 +1,1481 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.Thread.UncaughtExceptionHandler; +import java.net.ConnectException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.LinkedBlockingQueue; + +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.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +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.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.ZooKeeperSaslClient; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.proto.AuthPacket; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.ExistsResponse; +import org.apache.zookeeper.proto.GetACLResponse; +import org.apache.zookeeper.proto.GetChildren2Response; +import org.apache.zookeeper.proto.GetChildrenResponse; +import org.apache.zookeeper.proto.GetDataResponse; +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.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; + +/** + * 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. + * + */ +public class ClientCnxn { + private static final Logger LOG = LoggerFactory.getLogger(ClientCnxn.class); + + private static final String ZK_SASL_CLIENT_USERNAME = + "zookeeper.sasl.client.username"; + + /* 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; + + /** 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 environment variable "zookeeper.disableAutoWatchReset" to "true" */ + private static boolean disableAutoWatchReset; + static { + // this var should not be public, but otw there is no easy way + // to test + disableAutoWatchReset = + Boolean.getBoolean("zookeeper.disableAutoWatchReset"); + if (LOG.isDebugEnabled()) { + LOG.debug("zookeeper.disableAutoWatchReset is " + + disableAutoWatchReset); + } + } + + static class AuthData { + AuthData(String scheme, byte data[]) { + this.scheme = scheme; + this.data = data; + } + + String scheme; + + byte data[]; + } + + private final CopyOnWriteArraySet authInfo = new CopyOnWriteArraySet(); + + /** + * These are the packets that have been sent and are waiting for a response. + */ + private final LinkedList pendingQueue = new LinkedList(); + + /** + * These are the packets that need to be sent. + */ + private final LinkedList outgoingQueue = new LinkedList(); + + 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; + + 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; + + /** 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("Ignoring 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 { + 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 + */ + public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper, + ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket, + long sessionId, byte[] sessionPasswd, boolean canBeReadOnly) { + 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; + + sendThread = new SendThread(clientCnxnSocket); + eventThread = new EventThread(); + + } + + /** + * tests use this to check on reset of watches + * @return if the auto reset of watches are disabled + */ + public static boolean getDisableAutoResetWatch() { + return disableAutoWatchReset; + } + /** + * tests use this to set the auto reset + * @param b the value to set disable watches to + */ + public static void setDisableAutoResetWatch(boolean b) { + disableAutoWatchReset = b; + } + public void start() { + sendThread.start(); + 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; + } + + class EventThread extends ZooKeeperThread { + 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) { + if (event.getType() == EventType.None + && sessionState == event.getState()) { + return; + } + sessionState = event.getState(); + + // materialize the watchers based on the event + WatcherSetEventPair pair = new WatcherSetEventPair( + watcher.materialize(event.getState(), event.getType(), + event.getPath()), + event); + // queue the pair (watch set & event) for later processing + waitingEvents.add(pair); + } + + 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 + public void run() { + try { + isRunning = true; + while (true) { + Object event = waitingEvents.take(); + if (event == eventOfDeath) { + wasKilled = true; + } else { + 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())); + } + + private void processEvent(Object event) { + try { + if (event instanceof WatcherSetEventPair) { + // each watcher will process the event + WatcherSetEventPair pair = (WatcherSetEventPair) event; + for (Watcher watcher : pair.watchers) { + try { + watcher.process(pair.event); + } catch (Throwable t) { + LOG.error("Error while calling watcher ", t); + } + } + } 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); + } + } 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); + } + } 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 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); + } + } 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 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.cb instanceof VoidCallback) { + VoidCallback cb = (VoidCallback) p.cb; + cb.processResult(rc, clientPath, p.ctx); + } + } + } catch (Throwable t) { + LOG.error("Caught unexpected throwable", t); + } + } + } + + private void finishPacket(Packet p) { + if (p.watchRegistration != null) { + p.watchRegistration.register(p.replyHeader.getErr()); + } + + if (p.cb == null) { + synchronized (p) { + p.finished = true; + p.notifyAll(); + } + } else { + p.finished = true; + eventThread.queuePacket(p); + } + } + + 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); + } + } + + public static final int packetLen = Integer.getInteger("jute.maxbuffer", + 4096 * 1024); + + /** + * 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 Random r = new Random(System.nanoTime()); + 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"); + if (replyHdr.getXid() == -2) { + // -2 is the xid for pings + if (LOG.isDebugEnabled()) { + LOG.debug("Got ping response for sessionid: 0x" + + Long.toHexString(sessionId) + + " after " + + ((System.nanoTime() - lastPingSentNs) / 1000000) + + "ms"); + } + return; + } + if (replyHdr.getXid() == -4) { + // -4 is the xid for AuthPacket + if(replyHdr.getErr() == KeeperException.Code.AUTHFAILED.intValue()) { + state = States.AUTH_FAILED; + eventThread.queueEvent( new WatchedEvent(Watcher.Event.EventType.None, + Watcher.Event.KeeperState.AuthFailed, null) ); + } + if (LOG.isDebugEnabled()) { + LOG.debug("Got auth sessionid:0x" + + Long.toHexString(sessionId)); + } + return; + } + if (replyHdr.getXid() == -1) { + // -1 means notification + if (LOG.isDebugEnabled()) { + LOG.debug("Got notification sessionid: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 " + event.getPath() + + " which is too short for chroot path " + + chrootPath); + } + } + + WatchedEvent we = new WatchedEvent(event); + if (LOG.isDebugEnabled()) { + LOG.debug("Got " + we + " for sessionid 0x" + + Long.toHexString(sessionId)); + } + + eventThread.queueEvent( we ); + return; + } + + // If SASL authentication is currently in progress, construct and + // send a response packet immediately, rather than queuing a + // response as with other packets. + if (clientTunneledAuthenticationInProgress()) { + 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"); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("Reading reply sessionid:0x" + + Long.toHexString(sessionId) + ", packet:: " + packet); + } + } finally { + finishPacket(packet); + } + } + + SendThread(ClientCnxnSocket clientCnxnSocket) { + super(makeThreadName("-SendThread()")); + state = 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 + */ + ZooKeeper.States getZkState() { + return state; + } + + ClientCnxnSocket getClientCnxnSocket() { + return clientCnxnSocket; + } + + void primeConnection() throws IOException { + LOG.info("Socket connection established to " + + clientCnxnSocket.getRemoteSocketAddress() + + ", initiating session"); + isFirstConnect = false; + long sessId = (seenRwServerBefore) ? sessionId : 0; + ConnectRequest conReq = new ConnectRequest(0, lastZxid, + sessionTimeout, sessId, sessionPasswd); + synchronized (outgoingQueue) { + // 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 (!disableAutoWatchReset) { + List dataWatches = zooKeeper.getDataWatches(); + List existWatches = zooKeeper.getExistWatches(); + List childWatches = zooKeeper.getChildWatches(); + if (!dataWatches.isEmpty() + || !existWatches.isEmpty() || !childWatches.isEmpty()) { + + Iterator dataWatchesIter = prependChroot(dataWatches).iterator(); + Iterator existWatchesIter = prependChroot(existWatches).iterator(); + Iterator childWatchesIter = prependChroot(childWatches).iterator(); + long setWatchesLastZxid = lastZxid; + + while (dataWatchesIter.hasNext() + || existWatchesIter.hasNext() || childWatchesIter.hasNext()) { + List dataWatchesBatch = new ArrayList(); + List existWatchesBatch = new ArrayList(); + List childWatchesBatch = 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 { + break; + } + batchLength += watch.length(); + } + + SetWatches sw = new SetWatches(setWatchesLastZxid, + dataWatchesBatch, + existWatchesBatch, + childWatchesBatch); + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setWatches); + h.setXid(-8); + Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); + outgoingQueue.addFirst(packet); + } + } + } + + for (AuthData id : authInfo) { + outgoingQueue.addFirst(new Packet(new RequestHeader(-4, + OpCode.auth), null, new AuthPacket(0, id.scheme, + id.data), null, null)); + } + outgoingQueue.addFirst(new Packet(null, null, conReq, + null, null, readOnly)); + } + clientCnxnSocket.enableReadWriteOnly(); + if (LOG.isDebugEnabled()) { + 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(-2, OpCode.ping); + queuePacket(h, null, null, null, null, null, null, null, null); + } + + private InetSocketAddress rwServerAddress = null; + + private final static int minPingRwTimeout = 100; + + private final static 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; + state = States.CONNECTING; + + setName(getName().replaceAll("\\(.*\\)", + "(" + addr.getHostName() + ":" + addr.getPort() + ")")); + if (ZooKeeperSaslClient.isEnabled()) { + try { + String principalUserName = System.getProperty( + ZK_SASL_CLIENT_USERNAME, "zookeeper"); + zooKeeperSaslClient = + new ZooKeeperSaslClient( + principalUserName+"/"+addr.getHostName()); + } 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: " + e + " Will continue connection to Zookeeper server without " + + "SASL authentication, if Zookeeper server allows it."); + eventThread.queueEvent(new WatchedEvent( + Watcher.Event.EventType.None, + Watcher.Event.KeeperState.AuthFailed, null)); + saslLoginFailed = true; + } + } + logStartConnect(addr); + + clientCnxnSocket.connect(addr); + } + + private void logStartConnect(InetSocketAddress addr) { + String msg = "Opening socket connection to server " + addr; + if (zooKeeperSaslClient != null) { + msg += ". " + zooKeeperSaslClient.getConfigStatus(); + } + LOG.info(msg); + } + + private static final String RETRY_CONN_MSG = + ", closing socket connection and attempting reconnect"; + + @Override + public void run() { + clientCnxnSocket.introduce(this,sessionId); + clientCnxnSocket.updateNow(); + clientCnxnSocket.updateLastSendAndHeard(); + int to; + long lastPingRwServer = Time.currentElapsedTime(); + final int MAX_SEND_PING_INTERVAL = 10000; //10 seconds + InetSocketAddress serverAddress = null; + while (state.isAlive()) { + try { + if (!clientCnxnSocket.isConnected()) { + if(!isFirstConnect){ + try { + Thread.sleep(r.nextInt(1000)); + } catch (InterruptedException e) { + LOG.warn("Unexpected exception", e); + } + } + // don't re-establish connection if we are closing + if (closing || !state.isAlive()) { + break; + } + if (rwServerAddress != null) { + serverAddress = rwServerAddress; + rwServerAddress = null; + } else { + serverAddress = hostProvider.next(1000); + } + startConnect(serverAddress); + clientCnxnSocket.updateLastSendAndHeard(); + } + + 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); + state = 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. + state = States.AUTH_FAILED; + sendAuthEvent = true; + } else { + if (authState == KeeperState.SaslAuthenticated) { + sendAuthEvent = true; + } + } + } + + if (sendAuthEvent == true) { + eventThread.queueEvent(new WatchedEvent( + Watcher.Event.EventType.None, + authState,null)); + } + } + to = readTimeout - clientCnxnSocket.getIdleRecv(); + } else { + to = connectTimeout - clientCnxnSocket.getIdleRecv(); + } + + if (to <= 0) { + String warnInfo; + warnInfo = "Client session timed out, have not heard from server in " + + clientCnxnSocket.getIdleRecv() + + "ms" + + " for sessionid 0x" + + Long.toHexString(sessionId); + LOG.warn(warnInfo); + throw new SessionTimeoutException(warnInfo); + } + 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; + } + } + } + + // 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); + } + + clientCnxnSocket.doTransport(to, pendingQueue, outgoingQueue, ClientCnxn.this); + } catch (Throwable e) { + if (closing) { + if (LOG.isDebugEnabled()) { + // closing so this is expected + LOG.debug("An exception was thrown while closing send thread for session 0x" + + Long.toHexString(getSessionId()) + + " : " + e.getMessage()); + } + break; + } else { + // this is ugly, you have a better way speak up + if (e instanceof SessionExpiredException) { + LOG.info(e.getMessage() + ", closing socket connection"); + } else if (e instanceof SessionTimeoutException) { + LOG.info(e.getMessage() + RETRY_CONN_MSG); + } else if (e instanceof EndOfStreamException) { + LOG.info(e.getMessage() + RETRY_CONN_MSG); + } else if (e instanceof RWServerFoundException) { + LOG.info(e.getMessage()); + } else if (e instanceof SocketException) { + LOG.info("Socket error occurred: {}: {}", serverAddress, e.getMessage()); + } else { + LOG.warn("Session 0x{} for server {}, unexpected error{}", + Long.toHexString(getSessionId()), + serverAddress, + RETRY_CONN_MSG, + e); + } + cleanup(); + if (state.isAlive()) { + eventThread.queueEvent(new WatchedEvent( + Event.EventType.None, + Event.KeeperState.Disconnected, + null)); + } + clientCnxnSocket.updateNow(); + clientCnxnSocket.updateLastSendAndHeard(); + } + } + } + cleanup(); + clientCnxnSocket.close(); + if (state.isAlive()) { + eventThread.queueEvent(new WatchedEvent(Event.EventType.None, + Event.KeeperState.Disconnected, null)); + } + ZooTrace.logTraceMessage(LOG, ZooTrace.getTextTraceLevel(), + "SendThread exited loop for session: 0x" + + Long.toHexString(getSessionId())); + } + + private void pingRwServer() throws RWServerFoundException, UnknownHostException { + String result = null; + InetSocketAddress addr = hostProvider.next(0); + LOG.info("Checking server " + addr + " for being r/w." + + " Timeout " + pingRwTimeout); + + Socket sock = null; + BufferedReader br = null; + try { + sock = new Socket(addr.getHostName(), 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.getMessage(), 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.getHostName() + ":" + addr.getPort()); + } + } + + private void cleanup() { + clientCnxnSocket.cleanup(); + synchronized (pendingQueue) { + for (Packet p : pendingQueue) { + conLossPacket(p); + } + pendingQueue.clear(); + } + synchronized (outgoingQueue) { + for (Packet p : outgoingQueue) { + conLossPacket(p); + } + outgoingQueue.clear(); + } + } + + /** + * 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) { + state = States.CLOSED; + + eventThread.queueEvent(new WatchedEvent( + Watcher.Event.EventType.None, + Watcher.Event.KeeperState.Expired, null)); + eventThread.queueEventOfDeath(); + + String warnInfo; + warnInfo = "Unable to reconnect to ZooKeeper service, session 0x" + + Long.toHexString(sessionId) + " has expired"; + 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; + state = (isRO) ? + States.CONNECTEDREADONLY : States.CONNECTED; + seenRwServerBefore |= !isRO; + LOG.info("Session establishment complete on server " + + clientCnxnSocket.getRemoteSocketAddress() + + ", sessionid = 0x" + Long.toHexString(sessionId) + + ", negotiated timeout = " + negotiatedSessionTimeout + + (isRO ? " (READ-ONLY mode)" : "")); + KeeperState eventState = (isRO) ? + KeeperState.ConnectedReadOnly : KeeperState.SyncConnected; + eventThread.queueEvent(new WatchedEvent( + Watcher.Event.EventType.None, + eventState, null)); + } + + void close() { + state = States.CLOSED; + clientCnxnSocket.wakeupCnxn(); + } + + void testableCloseSocket() throws IOException { + clientCnxnSocket.testableCloseSocket(); + } + + public boolean clientTunneledAuthenticationInProgress() { + // 1. SASL client is disabled. + if (!ZooKeeperSaslClient.isEnabled()) { + return false; + } + + // 2. SASL login failed. + if (saslLoginFailed == true) { + 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() { + if (LOG.isDebugEnabled()) { + LOG.debug("Disconnecting client for session: 0x" + + Long.toHexString(getSessionId())); + } + + sendThread.close(); + eventThread.queueEventOfDeath(); + } + + /** + * Close the connection, which includes; send session disconnect to the + * server, shutdown the send/event threads. + * + * @throws IOException + */ + public void close() throws IOException { + if (LOG.isDebugEnabled()) { + 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(); + } + } + + private 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. + */ + synchronized public int getXid() { + return xid++; + } + + public ReplyHeader submitRequest(RequestHeader h, Record request, + Record response, WatchRegistration watchRegistration) + throws InterruptedException { + ReplyHeader r = new ReplyHeader(); + Packet packet = queuePacket(h, r, request, response, null, null, null, + null, watchRegistration); + synchronized (packet) { + while (!packet.finished) { + packet.wait(); + } + } + return r; + } + + public void enableWrite() { + sendThread.getClientCnxnSocket().enableWrite(); + } + + 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); + } + + Packet queuePacket(RequestHeader h, ReplyHeader r, Record request, + Record response, AsyncCallback cb, String clientPath, + String serverPath, Object ctx, WatchRegistration watchRegistration) + { + 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. + synchronized (outgoingQueue) { + packet = new Packet(h, r, request, response, watchRegistration); + packet.cb = cb; + packet.ctx = ctx; + packet.clientPath = clientPath; + packet.serverPath = serverPath; + 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; + } + outgoingQueue.add(packet); + } + } + sendThread.getClientCnxnSocket().wakeupCnxn(); + return packet; + } + + public void addAuthInfo(String scheme, byte auth[]) { + if (!state.isAlive()) { + return; + } + authInfo.add(new AuthData(scheme, auth)); + queuePacket(new RequestHeader(-4, OpCode.auth), null, + new AuthPacket(0, scheme, auth), null, null, null, null, + null, null); + } + + States getState() { + return state; + } +} diff --git a/src/java/main/org/apache/zookeeper/ClientCnxnSocket.java b/src/java/main/org/apache/zookeeper/ClientCnxnSocket.java new file mode 100644 index 0000000..b676531 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ClientCnxnSocket.java @@ -0,0 +1,175 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.util.LinkedList; +import java.util.List; + +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.common.Time; +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 long sentCount = 0; + protected long recvCount = 0; + protected long lastHeard; + protected long lastSend; + protected long now; + protected ClientCnxn.SendThread sendThread; + + /** + * 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) { + this.sendThread = sendThread; + this.sessionId = sessionId; + } + + void updateNow() { + now = Time.currentElapsedTime(); + } + + int getIdleRecv() { + return (int) (now - lastHeard); + } + + int getIdleSend() { + return (int) (now - lastSend); + } + + long getSentCount() { + return sentCount; + } + + long getRecvCount() { + return recvCount; + } + + void updateLastHeard() { + this.lastHeard = now; + } + + void updateLastSend() { + this.lastSend = now; + } + + void updateLastSendAndHeard() { + this.lastSend = now; + this.lastHeard = now; + } + + protected void readLength() throws IOException { + int len = incomingBuffer.getInt(); + if (len < 0 || len >= ClientCnxn.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) + ","); + } + buf.append("]"); + 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; + + abstract SocketAddress getRemoteSocketAddress(); + + abstract SocketAddress getLocalSocketAddress(); + + abstract void cleanup(); + + abstract void close(); + + abstract void wakeupCnxn(); + + abstract void enableWrite(); + + abstract void disableWrite(); + + abstract void enableReadWriteOnly(); + + abstract void doTransport(int waitTimeOut, List pendingQueue, + LinkedList outgoingQueue, ClientCnxn cnxn) + throws IOException, InterruptedException; + + abstract void testableCloseSocket() throws IOException; + + abstract void sendPacket(Packet p) throws IOException; +} diff --git a/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java b/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java new file mode 100644 index 0000000..720619d --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java @@ -0,0 +1,431 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Set; + +import org.apache.zookeeper.ClientCnxn.EndOfStreamException; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientCnxnSocketNIO extends ClientCnxnSocket { + private static final Logger LOG = LoggerFactory + .getLogger(ClientCnxnSocketNIO.class); + + private final Selector selector = Selector.open(); + + private SelectionKey sockKey; + + ClientCnxnSocketNIO() throws IOException { + super(); + } + + @Override + boolean isConnected() { + return sockKey != null; + } + + /** + * @return true if a packet was received + * @throws InterruptedException + * @throws IOException + */ + void doIO(List pendingQueue, LinkedList outgoingQueue, ClientCnxn cnxn) + throws InterruptedException, IOException { + SocketChannel sock = (SocketChannel) sockKey.channel(); + if (sock == null) { + throw new IOException("Socket is null!"); + } + 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++; + readLength(); + } else if (!initialized) { + readConnectResult(); + enableRead(); + if (findSendablePacket(outgoingQueue, + cnxn.sendThread.clientTunneledAuthenticationInProgress()) != 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; + } else { + sendThread.readResponse(incomingBuffer); + lenBuffer.clear(); + incomingBuffer = lenBuffer; + updateLastHeard(); + } + } + } + if (sockKey.isWritable()) { + synchronized(outgoingQueue) { + Packet p = findSendablePacket(outgoingQueue, + cnxn.sendThread.clientTunneledAuthenticationInProgress()); + + 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()); + } + p.createBB(); + } + sock.write(p.bb); + if (!p.bb.hasRemaining()) { + sentCount++; + outgoingQueue.removeFirstOccurrence(p); + if (p.requestHeader != null + && p.requestHeader.getType() != OpCode.ping + && p.requestHeader.getType() != OpCode.auth) { + synchronized (pendingQueue) { + pendingQueue.add(p); + } + } + } + } + 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(LinkedList outgoingQueue, + boolean clientTunneledAuthenticationInProgress) { + synchronized (outgoingQueue) { + if (outgoingQueue.isEmpty()) { + return null; + } + if (outgoingQueue.getFirst().bb != null // If we've already starting sending the first packet, we better finish + || !clientTunneledAuthenticationInProgress) { + 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. + ListIterator iter = outgoingQueue.listIterator(); + 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.add(0, p); + return p; + } else { + // Non-priming packet: defer it until later, leaving it in the queue + // until authentication completes. + if (LOG.isDebugEnabled()) { + LOG.debug("deferring non-priming packet: " + p + + "until SASL authentication completes."); + } + } + } + // no sendable packet found. + return null; + } + } + + @Override + void cleanup() { + if (sockKey != null) { + SocketChannel sock = (SocketChannel) sockKey.channel(); + sockKey.cancel(); + try { + sock.socket().shutdownInput(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Ignoring exception during shutdown input", e); + } + } + try { + sock.socket().shutdownOutput(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Ignoring exception during shutdown output", + e); + } + } + try { + sock.socket().close(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Ignoring exception during socket close", e); + } + } + try { + sock.close(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Ignoring exception during channel close", e); + } + } + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + if (LOG.isDebugEnabled()) { + 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 + * @param sock the {@link SocketChannel} + * @param addr the address of remote host + * @throws IOException + */ + void registerAndConnect(SocketChannel sock, InetSocketAddress addr) + throws IOException { + sockKey = sock.register(selector, SelectionKey.OP_CONNECT); + boolean immediateConnect = sock.connect(addr); + if (immediateConnect) { + sendThread.primeConnection(); + } + } + + @Override + void connect(InetSocketAddress addr) throws IOException { + SocketChannel sock = createSock(); + try { + registerAndConnect(sock, addr); + } catch (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() { + // a lot could go wrong here, so rather than put in a bunch of code + // to check for nulls all down the chain let's do it the simple + // yet bulletproof way + try { + return ((SocketChannel) sockKey.channel()).socket() + .getRemoteSocketAddress(); + } catch (NullPointerException e) { + return null; + } + } + + /** + * 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() { + // a lot could go wrong here, so rather than put in a bunch of code + // to check for nulls all down the chain let's do it the simple + // yet bulletproof way + try { + return ((SocketChannel) sockKey.channel()).socket() + .getLocalSocketAddress(); + } catch (NullPointerException e) { + return null; + } + } + + @Override + synchronized void wakeupCnxn() { + selector.wakeup(); + } + + @Override + void doTransport(int waitTimeOut, List pendingQueue, LinkedList outgoingQueue, + ClientCnxn cnxn) + throws IOException, InterruptedException { + 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(); + for (SelectionKey k : selected) { + SocketChannel sc = ((SocketChannel) k.channel()); + if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) { + if (sc.finishConnect()) { + updateLastSendAndHeard(); + sendThread.primeConnection(); + } + } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { + doIO(pendingQueue, outgoingQueue, cnxn); + } + } + if (sendThread.getZkState().isConnected()) { + synchronized(outgoingQueue) { + if (findSendablePacket(outgoingQueue, + cnxn.sendThread.clientTunneledAuthenticationInProgress()) != null) { + enableWrite(); + } + } + } + selected.clear(); + } + + //TODO should this be synchronized? + @Override + void testableCloseSocket() throws IOException { + LOG.info("testableCloseSocket() called"); + ((SocketChannel) sockKey.channel()).socket().close(); + } + + @Override + synchronized void enableWrite() { + int i = sockKey.interestOps(); + if ((i & SelectionKey.OP_WRITE) == 0) { + sockKey.interestOps(i | SelectionKey.OP_WRITE); + } + } + + @Override + public synchronized void disableWrite() { + int i = sockKey.interestOps(); + if ((i & SelectionKey.OP_WRITE) != 0) { + sockKey.interestOps(i & (~SelectionKey.OP_WRITE)); + } + } + + synchronized private void enableRead() { + int i = sockKey.interestOps(); + if ((i & SelectionKey.OP_READ) == 0) { + sockKey.interestOps(i | SelectionKey.OP_READ); + } + } + + @Override + synchronized void enableReadWriteOnly() { + 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/src/java/main/org/apache/zookeeper/ClientWatchManager.java b/src/java/main/org/apache/zookeeper/ClientWatchManager.java new file mode 100644 index 0000000..d56374d --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ClientWatchManager.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; + +import java.util.Set; + +/** + */ +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 + */ + public Set materialize(Watcher.Event.KeeperState state, + Watcher.Event.EventType type, String path); +} diff --git a/src/java/main/org/apache/zookeeper/CreateMode.java b/src/java/main/org/apache/zookeeper/CreateMode.java new file mode 100644 index 0000000..84f5be0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/CreateMode.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; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; + +/*** + * 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), + /** + * 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), + /** + * The znode will be deleted upon the client's disconnect. + */ + EPHEMERAL (1, true, 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); + + private static final Logger LOG = LoggerFactory.getLogger(CreateMode.class); + + private boolean ephemeral; + private boolean sequential; + private int flag; + + CreateMode(int flag, boolean ephemeral, boolean sequential) { + this.flag = flag; + this.ephemeral = ephemeral; + this.sequential = sequential; + } + + public boolean isEphemeral() { + return ephemeral; + } + + public boolean isSequential() { + return sequential; + } + + public int toFlag() { + return flag; + } + + /** + * Map an integer value to a CreateMode value + */ + static public 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 ; + + default: + String errMsg = "Received an invalid flag value: " + flag + + " to convert to a CreateMode"; + LOG.error(errMsg); + throw new KeeperException.BadArgumentsException(errMsg); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/Environment.java b/src/java/main/org/apache/zookeeper/Environment.java new file mode 100644 index 0000000..51797a1 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Environment.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; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 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(ArrayList l, String k, String v) { + l.add(new Entry(k,v)); + } + + public static List list() { + ArrayList 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", "")); + + 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/src/java/main/org/apache/zookeeper/JLineZNodeCompletor.java b/src/java/main/org/apache/zookeeper/JLineZNodeCompletor.java new file mode 100644 index 0000000..37a9f0a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/JLineZNodeCompletor.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; + +import java.util.List; + +import jline.Completor; + +class JLineZNodeCompletor implements Completor { + private ZooKeeper zk; + + public JLineZNodeCompletor(ZooKeeper zk) { + this.zk = zk; + } + + @SuppressWarnings(value={"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; + } + return candidates.size() == 0 ? buffer.length() : buffer.lastIndexOf("/") + 1; + } +} diff --git a/src/java/main/org/apache/zookeeper/KeeperException.java b/src/java/main/org/apache/zookeeper/KeeperException.java new file mode 100644 index 0000000..bdf4203 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/KeeperException.java @@ -0,0 +1,728 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@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 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 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 + public static final int Ok = 0; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#SYSTEMERROR} instead + */ + @Deprecated + public static final int SystemError = -1; + /** + * @deprecated deprecated in 3.1.0, use + * {@link Code#RUNTIMEINCONSISTENCY} instead + */ + @Deprecated + public static final int RuntimeInconsistency = -2; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#DATAINCONSISTENCY} + * instead + */ + @Deprecated + public static final int DataInconsistency = -3; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#CONNECTIONLOSS} + * instead + */ + @Deprecated + public static final int ConnectionLoss = -4; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#MARSHALLINGERROR} + * instead + */ + @Deprecated + public static final int MarshallingError = -5; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#UNIMPLEMENTED} + * instead + */ + @Deprecated + public static final int Unimplemented = -6; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#OPERATIONTIMEOUT} + * instead + */ + @Deprecated + public static final int OperationTimeout = -7; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#BADARGUMENTS} + * instead + */ + @Deprecated + public static final int BadArguments = -8; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#APIERROR} instead + */ + @Deprecated + public static final int APIError = -100; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NONODE} instead + */ + @Deprecated + public static final int NoNode = -101; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NOAUTH} instead + */ + @Deprecated + public static final int NoAuth = -102; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#BADVERSION} instead + */ + @Deprecated + public static final int BadVersion = -103; + /** + * @deprecated deprecated in 3.1.0, use + * {@link Code#NOCHILDRENFOREPHEMERALS} + * instead + */ + @Deprecated + public static final int NoChildrenForEphemerals = -108; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NODEEXISTS} instead + */ + @Deprecated + public static final int NodeExists = -110; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NOTEMPTY} instead + */ + @Deprecated + public static final int NotEmpty = -111; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#SESSIONEXPIRED} instead + */ + @Deprecated + public static final int SessionExpired = -112; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#INVALIDCALLBACK} + * instead + */ + @Deprecated + public static final int InvalidCallback = -113; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#INVALIDACL} instead + */ + @Deprecated + public static final int InvalidACL = -114; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#AUTHFAILED} instead + */ + @Deprecated + public static final int AuthFailed = -115; + /** + * This value will be used directly in {@link CODE#SESSIONMOVED} + */ + // public static final int SessionMoved = -118; + } + + /** 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 static 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), + + /** 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 */ + 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); + + 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 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"; + 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) { + 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#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#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#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); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/Login.java b/src/java/main/org/apache/zookeeper/Login.java new file mode 100644 index 0000000..c4975be --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Login.java @@ -0,0 +1,407 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 javax.security.auth.kerberos.KerberosPrincipal; +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 javax.security.auth.callback.CallbackHandler; + +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.security.auth.kerberos.KerberosTicket; +import javax.security.auth.Subject; +import java.util.Date; +import java.util.Random; +import java.util.Set; + +public class Login { + private static final Logger LOG = LoggerFactory.getLogger(Login.class); + 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 boolean isUsingKeytab = false; + + /** Random number generator */ + private static Random rng = new Random(); + + private LoginContext login = null; + private String loginContextName = null; + private String keytabFile = null; + private String principal = null; + + // Initialize 'lastLogin' to do a login at first time + private long lastLogin = Time.currentElapsedTime() - MIN_TIME_BEFORE_RELOGIN; + + /** + * 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(). + * @throws javax.security.auth.login.LoginException + * Thrown if authentication fails. + */ + public Login(final String loginContextName, CallbackHandler callbackHandler) + throws LoginException { + 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("keyTab") != null) { + keytabFile = (String)entry.getOptions().get("keyTab"); + isUsingKeytab = 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: " + expiryDate + "." + + "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 " + principal + "' within " + + "kadmin, or instead, to generate a keytab for " + principal + ". Because the TGT's " + + "expiry cannot be further extended by refreshing, exiting refresh thread now."); + 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 : " + until + " to : " + newuntil + " since " + + "the former is sooner than the minimum refresh interval (" + + MIN_TIME_BEFORE_RELOGIN / 1000 + " seconds) from now."); + } + nextRefresh = Math.max(nextRefresh, now + MIN_TIME_BEFORE_RELOGIN); + } + nextRefreshDate = new Date(nextRefresh); + if (nextRefresh > expiry) { + LOG.error("next refresh: " + nextRefreshDate + " is later than expiry " + expiryDate + + ". This may indicate a clock skew problem. Check that this host and the KDC's " + + "hosts' clocks are in sync. Exiting refresh thread."); + 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:" + nextRefreshDate + " 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."); + break; + } + if (isUsingTicketCache) { + String cmd = "/usr/bin/kinit"; + if (System.getProperty("zookeeper.kinit") != null) { + cmd = System.getProperty("zookeeper.kinit"); + } + 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: '" + cmd + + " " + kinitArgs + "'" + "; exception was:" + e + ". Exiting refresh thread.",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 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 " + ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY + "(=" + + System.getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY, "Client") + ")"); + } + LoginContext loginContext = new LoginContext(loginContextName,callbackHandler); + loginContext.login(); + LOG.info("{} successfully logged in.", loginContextName); + return loginContext; + } + + // 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 * rng.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 " + (MIN_TIME_BEFORE_RELOGIN/1000) + " seconds"+ + " before."); + 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/src/java/main/org/apache/zookeeper/MultiResponse.java b/src/java/main/org/apache/zookeeper/MultiResponse.java new file mode 100644 index 0000000..70f7623 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/MultiResponse.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; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.MultiHeader; +import org.apache.zookeeper.proto.SetDataResponse; +import org.apache.zookeeper.proto.ErrorResponse; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * 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); + + int index = 0; + 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.delete: + case ZooDefs.OpCode.check: + break; + case ZooDefs.OpCode.setData: + new SetDataResponse(((OpResult.SetDataResult) 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.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.error: + //FIXME: 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/src/java/main/org/apache/zookeeper/MultiTransactionRecord.java b/src/java/main/org/apache/zookeeper/MultiTransactionRecord.java new file mode 100644 index 0000000..801969a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/MultiTransactionRecord.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; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.proto.*; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Encodes a composite transaction. In the wire format, each transaction + * consists of a single MultiHeader followed by the appropriate request. + * Each of these MultiHeaders has a type which indicates + * the type of the following transaction or a negative number if no more transactions + * are included. + */ +public class MultiTransactionRecord implements Record, Iterable { + private List ops = new ArrayList(); + + public MultiTransactionRecord() { + } + + public MultiTransactionRecord(Iterable ops) { + for (Op op : ops) { + add(op); + } + } + + @Override + public Iterator iterator() { + return ops.iterator() ; + } + + public void add(Op op) { + ops.add(op); + } + + public int size() { + return ops.size(); + } + + @Override + public void serialize(OutputArchive archive, String tag) throws IOException { + archive.startRecord(this, tag); + int index = 0 ; + for (Op op : ops) { + MultiHeader h = new MultiHeader(op.getType(), false, -1); + h.serialize(archive, tag); + switch (op.getType()) { + case ZooDefs.OpCode.create: + op.toRequestRecord().serialize(archive, tag); + break; + case ZooDefs.OpCode.delete: + op.toRequestRecord().serialize(archive, tag); + break; + case ZooDefs.OpCode.setData: + op.toRequestRecord().serialize(archive, tag); + break; + case ZooDefs.OpCode.check: + 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); + + while (!h.getDone()) { + switch (h.getType()) { + case ZooDefs.OpCode.create: + CreateRequest cr = new CreateRequest(); + cr.deserialize(archive, tag); + add(Op.create(cr.getPath(), cr.getData(), cr.getAcl(), cr.getFlags())); + 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; + default: + throw new IOException("Invalid type of op"); + } + h.deserialize(archive, tag); + } + archive.endRecord(tag); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MultiTransactionRecord)) return false; + + MultiTransactionRecord that = (MultiTransactionRecord) 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/src/java/main/org/apache/zookeeper/Op.java b/src/java/main/org/apache/zookeeper/Op.java new file mode 100644 index 0000000..1bc2061 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Op.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 org.apache.jute.Record; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.proto.CheckVersionRequest; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.SetDataRequest; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +/** + * Represents a single operation in a multi-operation transaction. Each operation can be a create, update + * or delete or can just be a version check. + * + * 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) + */ +public abstract class Op { + private int type; + private String path; + + // prevent untyped construction + private Op(int type, String path) { + this.type = type; + this.path = path; + } + + /** + * 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. + * @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 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); + } + + /** + * 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; + } + + /** + * 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 { + private byte[] data; + private List acl; + private int flags; + + private Create(String path, byte[] data, List acl, int flags) { + super(ZooDefs.OpCode.create, path); + this.data = data; + this.acl = acl; + this.flags = flags; + } + + private Create(String path, byte[] data, List acl, CreateMode createMode) { + super(ZooDefs.OpCode.create, path); + 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()); + } + } + + public static class Delete extends Op { + private int version; + + private Delete(String path, int version) { + super(ZooDefs.OpCode.delete, path); + 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); + 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); + 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); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/OpResult.java b/src/java/main/org/apache/zookeeper/OpResult.java new file mode 100644 index 0000000..45331d1 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/OpResult.java @@ -0,0 +1,185 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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. + * @see ZooDefs.OpCode + * @return an integer identifying what kind of operation this result came from. + */ + 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; + + public CreateResult(String path) { + super(ZooDefs.OpCode.create); + this.path = path; + } + + public String getPath() { + return path; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CreateResult)) return false; + + CreateResult other = (CreateResult) o; + return getType() == other.getType() && path.equals(other.getPath()); + } + + @Override + public int hashCode() { + return getType() * 35 + path.hashCode(); + } + } + + /** + * 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(); + } + } + + /** + * 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/src/java/main/org/apache/zookeeper/Quotas.java b/src/java/main/org/apache/zookeeper/Quotas.java new file mode 100644 index 0000000..b82e339 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Quotas.java @@ -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. + */ + +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/src/java/main/org/apache/zookeeper/SaslClientCallbackHandler.java b/src/java/main/org/apache/zookeeper/SaslClientCallbackHandler.java new file mode 100644 index 0000000..d6f5549 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/SaslClientCallbackHandler.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; + +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.", + new Object[]{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"); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/java/main/org/apache/zookeeper/ServerAdminClient.java b/src/java/main/org/apache/zookeeper/ServerAdminClient.java new file mode 100644 index 0000000..9464f1c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ServerAdminClient.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.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.StringTokenizer; + + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.ZooTrace; + +@InterfaceAudience.Public +public class ServerAdminClient { + private static final Logger LOG = LoggerFactory.getLogger(ServerAdminClient.class); + + private static long getMask(String mask) { + long retv = 0; + if (mask.equalsIgnoreCase("CLIENT_REQUEST_TRACE_MASK")) { + retv = ZooTrace.CLIENT_REQUEST_TRACE_MASK; + } else if (mask.equalsIgnoreCase("CLIENT_DATA_PACKET_TRACE_MASK")) { + retv = ZooTrace.CLIENT_DATA_PACKET_TRACE_MASK; + } else if (mask.equalsIgnoreCase("CLIENT_PING_TRACE_MASK")) { + retv = ZooTrace.CLIENT_PING_TRACE_MASK; + } else if (mask.equalsIgnoreCase("SERVER_PACKET_TRACE_MASK")) { + retv = ZooTrace.SERVER_PACKET_TRACE_MASK; + } else if (mask.equalsIgnoreCase("SESSION_TRACE_MASK")) { + retv = ZooTrace.SESSION_TRACE_MASK; + } else if (mask.equalsIgnoreCase("EVENT_DELIVERY_TRACE_MASK")) { + retv = ZooTrace.EVENT_DELIVERY_TRACE_MASK; + } else if (mask.equalsIgnoreCase("SERVER_PING_TRACE_MASK")) { + retv = ZooTrace.SERVER_PING_TRACE_MASK; + } else if (mask.equalsIgnoreCase("WARNING_TRACE_MASK")) { + retv = ZooTrace.WARNING_TRACE_MASK; + } + return retv; + } + + private static long getMasks(String masks) { + long retv = 0; + StringTokenizer st = new StringTokenizer(masks, "|"); + while (st.hasMoreTokens()) { + String mask = st.nextToken().trim(); + retv = retv | getMask(mask); + } + return retv; + } + + 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/src/java/main/org/apache/zookeeper/Shell.java b/src/java/main/org/apache/zookeeper/Shell.java new file mode 100644 index 0000000..246170e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Shell.java @@ -0,0 +1,477 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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.log4j.Logger; +import org.apache.zookeeper.common.Time; + +/** + * 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. + */ +abstract public class Shell { + + Logger LOG = Logger.getLogger(Shell.class); + + /** a Unix command to get the current user's name */ + public final static 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 = 0; // 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 != 0) { + 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 & 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/src/java/main/org/apache/zookeeper/StatsTrack.java b/src/java/main/org/apache/zookeeper/StatsTrack.java new file mode 100644 index 0000000..623c6d4 --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/zookeeper/Testable.java b/src/java/main/org/apache/zookeeper/Testable.java new file mode 100644 index 0000000..b38d2c3 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Testable.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; + +/** + * 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(); +} diff --git a/src/java/main/org/apache/zookeeper/Transaction.java b/src/java/main/org/apache/zookeeper/Transaction.java new file mode 100644 index 0000000..d8f0e01 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Transaction.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; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.data.ACL; +import java.util.ArrayList; +import java.util.List; + +/** + * 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/src/java/main/org/apache/zookeeper/Version.java b/src/java/main/org/apache/zookeeper/Version.java new file mode 100644 index 0000000..1f5cf1a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Version.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; + +public class Version implements org.apache.zookeeper.version.Info { + + /* + * Since the SVN to Git port this field doesn't return the revision anymore + * TODO: remove this method and associated field declaration in VerGen + * @see {@link #getHashRevision()} + * @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; + } + + 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."); + System.exit(1); + } + + /** + * 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 SVN + * repository revision "1.2.3-94" + *
    • --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()); + System.exit(0); + } + if (args[0].equals("--short")) + System.out.println(getVersion()); + else if (args[0].equals("--revision")) + System.out.println(getVersionRevision()); + else + printUsage(); + System.exit(0); + } +} diff --git a/src/java/main/org/apache/zookeeper/WatchedEvent.java b/src/java/main/org/apache/zookeeper/WatchedEvent.java new file mode 100644 index 0000000..851fc6c --- /dev/null +++ b/src/java/main/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.proto.WatcherEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; + +/** + * 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 { + final private KeeperState keeperState; + final private 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/src/java/main/org/apache/zookeeper/Watcher.java b/src/java/main/org/apache/zookeeper/Watcher.java new file mode 100644 index 0000000..8119fd9 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/Watcher.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 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 + public interface Event { + /** + * Enumeration of states the ZooKeeper may be at the event + */ + @InterfaceAudience.Public + 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); + + 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; + + default: + throw new RuntimeException("Invalid integer value for conversion to KeeperState"); + } + } + } + + /** + * Enumeration of types of events that may occur on the ZooKeeper + */ + @InterfaceAudience.Public + public enum EventType { + //监å¬çš„事件 + None (-1),//没有监å¬äº‹ä»¶ + NodeCreated (1),//å­èŠ‚点 + NodeDeleted (2),//本身的 + NodeDataChanged (3),//本身 + NodeChildrenChanged (4);//å­èŠ‚点 + + 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; + + default: + throw new RuntimeException("Invalid integer value for conversion to EventType"); + } + } + } + } + + abstract public void process(WatchedEvent event); +} diff --git a/src/java/main/org/apache/zookeeper/ZKUtil.java b/src/java/main/org/apache/zookeeper/ZKUtil.java new file mode 100644 index 0000000..e901832 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ZKUtil.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; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.common.PathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZKUtil { + private static final Logger LOG = LoggerFactory.getLogger(ZKUtil.class); + /** + * 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. + * + * See {@link #delete(String, int)} for more details. + * + * @throws IllegalArgumentException if an invalid path is specified + */ + public static void deleteRecursive(ZooKeeper zk, final String pathRoot) + throws InterruptedException, KeeperException + { + PathUtils.validatePath(pathRoot); + + List tree = listSubTreeBFS(zk, pathRoot); + LOG.debug("Deleting " + tree); + LOG.debug("Deleting " + tree.size() + " subnodes "); + 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); //Delete all versions of the node with -1. + } + } + + + /** + * 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); + LOG.debug("Deleting " + tree.size() + " subnodes "); + 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. + } + } + + /** + * 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 { + Deque queue = new LinkedList(); + List tree = new ArrayList(); + queue.add(pathRoot); + tree.add(pathRoot); + while (true) { + String node = queue.pollFirst(); + if (node == null) { + break; + } + List children = zk.getChildren(node, false); + for (final String child : children) { + final String childPath = node + "/" + child; + queue.add(childPath); + tree.add(childPath); + } + } + return tree; + } + +} diff --git a/src/java/main/org/apache/zookeeper/ZooDefs.java b/src/java/main/org/apache/zookeeper/ZooDefs.java new file mode 100644 index 0000000..09bc95c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ZooDefs.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; + +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 { + + @InterfaceAudience.Public + public interface OpCode { + public final int notification = 0; + + public final int create = 1; + + public final int delete = 2; + + public final int exists = 3; + + public final int getData = 4; + + public final int setData = 5; + + public final int getACL = 6; + + public final int setACL = 7; + + public final int getChildren = 8; + + public final int sync = 9; + + public final int ping = 11; + + public final int getChildren2 = 12; + + public final int check = 13; + + public final int multi = 14; + + public final int auth = 100; + + public final int setWatches = 101; + + public final int sasl = 102; + + public final int createSession = -10; + + public final int closeSession = -11; + + public final 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. + */ + public final 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. + */ + public final Id AUTH_IDS = new Id("auth", ""); + + /** + * This is a completely open ACL . + */ + public final 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. + */ + public final ArrayList CREATOR_ALL_ACL = new ArrayList( + Collections.singletonList(new ACL(Perms.ALL, AUTH_IDS))); + + /** + * This ACL gives the world the ability to read. + */ + public final ArrayList READ_ACL_UNSAFE = new ArrayList( + Collections + .singletonList(new ACL(Perms.READ, ANYONE_ID_UNSAFE))); + } + + final public static String[] opNames = { "notification", "create", + "delete", "exists", "getData", "setData", "getACL", "setACL", + "getChildren", "getChildren2", "getMaxChildren", "setMaxChildren", "ping" }; +} diff --git a/src/java/main/org/apache/zookeeper/ZooKeeper.java b/src/java/main/org/apache/zookeeper/ZooKeeper.java new file mode 100644 index 0000000..3e46ee7 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ZooKeeper.java @@ -0,0 +1,1857 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.AsyncCallback.*; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.client.ConnectStringParser; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.StaticHostProvider; +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.*; +import org.apache.zookeeper.server.DataTree; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.SocketAddress; +import java.util.*; + +/** + * 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. + * + */ +@InterfaceAudience.Public +public class ZooKeeper { + + public static final String ZOOKEEPER_CLIENT_CNXN_SOCKET = "zookeeper.clientCnxnSocket"; + + 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); + } + + + public ZooKeeperSaslClient getSaslClient() { + return cnxn.zooKeeperSaslClient; + } + + private final ZKWatchManager watchManager = new ZKWatchManager(); + + List getDataWatches() { + synchronized(watchManager.dataWatches) { + List rc = new ArrayList(watchManager.dataWatches.keySet()); + return rc; + } + } + List getExistWatches() { + synchronized(watchManager.existWatches) { + List rc = new ArrayList(watchManager.existWatches.keySet()); + return rc; + } + } + List getChildWatches() { + synchronized(watchManager.childWatches) { + List rc = new ArrayList(watchManager.childWatches.keySet()); + return rc; + } + } + + /** + * Manage watchers & 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. + */ + private static class ZKWatchManager implements ClientWatchManager { + private final Map> dataWatches = + new HashMap>(); + private final Map> existWatches = + new HashMap>(); + private final Map> childWatches = + new HashMap>(); + + private volatile Watcher defaultWatcher; + + final private void addTo(Set from, Set to) { + if (from != null) { + to.addAll(from); + } + } + + /* (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) + { + Set result = new HashSet(); + + switch (type) { + case None: + result.add(defaultWatcher); + boolean clear = ClientCnxn.getDisableAutoResetWatch() && + 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(); + } + } + + return result; + case NodeDataChanged: + case NodeCreated: + synchronized (dataWatches) { + addTo(dataWatches.remove(clientPath), result); + } + synchronized (existWatches) { + addTo(existWatches.remove(clientPath), result); + } + break; + case NodeChildrenChanged: + synchronized (childWatches) { + addTo(childWatches.remove(clientPath), result); + } + break; + case NodeDeleted: + synchronized (dataWatches) { + addTo(dataWatches.remove(clientPath), result); + } + // XXX 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); + } + break; + default: + String msg = "Unhandled watch event type " + type + + " with state " + state + " on path " + clientPath; + LOG.error(msg); + throw new RuntimeException(msg); + } + + return result; + } + } + + /** + * Register a watcher for a particular path. + */ + abstract class WatchRegistration { + private Watcher watcher; + private String clientPath; + public WatchRegistration(Watcher watcher, String clientPath) + { + this.watcher = watcher; + this.clientPath = clientPath; + } + + abstract protected 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; + } + } + + @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 + { + 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 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 + { + LOG.info("Initiating client connection, connectString=" + connectString + + " sessionTimeout=" + sessionTimeout + " watcher=" + watcher); + + watchManager.defaultWatcher = watcher; + + ConnectStringParser connectStringParser = new ConnectStringParser( + connectString); + HostProvider hostProvider = new StaticHostProvider( + connectStringParser.getServerAddresses()); + cnxn = new ClientCnxn(connectStringParser.getChrootPath(), + hostProvider, sessionTimeout, this, watchManager, + getClientCnxnSocket(), canBeReadOnly); + 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. + * + * @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. + * + * @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 + { + LOG.info("Initiating client connection, connectString=" + connectString + + " sessionTimeout=" + sessionTimeout + + " watcher=" + watcher + + " sessionId=" + Long.toHexString(sessionId) + + " sessionPasswd=" + + (sessionPasswd == null ? "" : "")); + + watchManager.defaultWatcher = watcher; + + ConnectStringParser connectStringParser = new ConnectStringParser( + connectString); + HostProvider hostProvider = new StaticHostProvider( + connectStringParser.getServerAddresses()); + cnxn = new ClientCnxn(connectStringParser.getChrootPath(), + hostProvider, sessionTimeout, this, watchManager, + getClientCnxnSocket(), sessionId, sessionPasswd, canBeReadOnly); + cnxn.seenRwServerBefore = true; // since user has provided sessionId + cnxn.start(); + } + + // VisibleForTesting + public Testable getTestable() { + return new ZooKeeperTestable(this, cnxn); + } + + /** + * 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. + * + * @throws InterruptedException + */ + public synchronized void close() throws InterruptedException { + if (!cnxn.getState().isAlive()) { + if (LOG.isDebugEnabled()) { + LOG.debug("Close called on already closed client"); + } + return; + } + + if (LOG.isDebugEnabled()) { + LOG.debug("Closing session: 0x" + Long.toHexString(getSessionId())); + } + + try { + cnxn.close(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Ignoring unexpected exception during close", e); + } + } + + LOG.info("Session: 0x" + Long.toHexString(getSessionId()) + " closed"); + } + + /** + * 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()); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.create); + CreateRequest request = new CreateRequest(); + CreateResponse response = new CreateResponse(); + request.setData(data); + request.setFlags(createMode.toFlag()); + request.setPath(serverPath); + if (acl != null && acl.size() == 0) { + throw new KeeperException.InvalidACLException(); + } + request.setAcl(acl); + 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()); + } + } + + /** + * 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()); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(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); + } + + /** + * 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 or none of them. + *

    + * 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}. + * @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 + * + * @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) + * @since 3.4.7 + */ + 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("IllegalArgumentException: " + iae.getMessage()); + ErrorResult err = new ErrorResult( + KeeperException.Code.BADARGUMENTS.intValue()); + results.add(err); + error = true; + continue; + } catch (KeeperException ke) { + LOG.error("KeeperException: " + ke.getMessage()); + 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 (false == error) { + results.clear(); + } + return results; + } + + private MultiTransactionRecord generateMultiTransaction(Iterable ops) { + List transaction = new ArrayList(); + + for (Op op : ops) { + transaction.add(withRootPrefix(op)); + } + return new MultiTransactionRecord(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(MultiTransactionRecord request, MultiCallback cb, Object ctx) { + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.multi); + MultiResponse response = new MultiResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, null, null, ctx, null); + } + + protected List multiInternal(MultiTransactionRecord request) + throws InterruptedException, KeeperException { + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.multi); + 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(); + + 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. + * + * @since 3.4.0 + * + * @return a Transaction builder object + */ + 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); + } + + /** + * 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); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setACL); + SetACLRequest request = new SetACLRequest(); + request.setPath(serverPath); + if (acl != null && acl.size() == 0) { + throw new KeeperException.InvalidACLException(clientPath); + } + 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 willbe + * 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 willbe + * 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 willbe + * 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. + * + * @since 3.3.0 + * + * @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 + */ + 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 willbe + * 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. + * + * @since 3.3.0 + * + * @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. + */ + 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. + * + * @since 3.3.0 + * + * @see #getChildren(String, Watcher, Stat) + */ + 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. + * + * @since 3.3.0 + * + * @see #getChildren(String, boolean, Stat) + */ + public void getChildren(String path, boolean watch, Children2Callback cb, + Object ctx) + { + getChildren(path, watch ? watchManager.defaultWatcher : null, 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); + } + + 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!!! + * + * @since 3.3.0 + * + * @param wait max wait in milliseconds + * @return true iff all threads are shutdown, otw false + */ + protected boolean testableWaitForShutdown(int wait) + throws InterruptedException + { + cnxn.sendThread.join(wait); + if (cnxn.sendThread.isAlive()) return false; + cnxn.eventThread.join(wait); + if (cnxn.eventThread.isAlive()) return false; + return true; + } + + /** + * 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!!! + * + * @since 3.3.0 + * + * @return ip address of the remote side of the connection or null if + * not connected + */ + 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!!! + * + * @since 3.3.0 + * + * @return ip address of the remote side of the connection or null if + * not connected + */ + protected SocketAddress testableLocalSocketAddress() { + return cnxn.sendThread.getClientCnxnSocket().getLocalSocketAddress(); + } + + private static ClientCnxnSocket getClientCnxnSocket() throws IOException { + String clientCnxnSocketName = System + .getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET); + if (clientCnxnSocketName == null) { + clientCnxnSocketName = ClientCnxnSocketNIO.class.getName(); + } + try { + return (ClientCnxnSocket) Class.forName(clientCnxnSocketName).getDeclaredConstructor() + .newInstance(); + } catch (Exception e) { + IOException ioe = new IOException("Couldn't instantiate " + + clientCnxnSocketName); + ioe.initCause(e); + throw ioe; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/ZooKeeperMain.java b/src/java/main/org/apache/zookeeper/ZooKeeperMain.java new file mode 100644 index 0000000..6ca538b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ZooKeeperMain.java @@ -0,0 +1,871 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 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( ); + + protected MyCommandOptions cl = new MyCommandOptions(); + protected HashMap history = new HashMap( ); + protected int commandCount = 0; + protected boolean printWatches = true; + + protected ZooKeeper zk; + protected String host = ""; + + public boolean getPrintWatches( ) { + return printWatches; + } + + static { + commandMap.put("connect", "host:port"); + commandMap.put("close",""); + commandMap.put("create", "[-s] [-e] path data acl"); + commandMap.put("delete","path [version]"); + commandMap.put("rmr","path"); + commandMap.put("set","path data [version]"); + commandMap.put("get","path [watch]"); + commandMap.put("ls","path [watch]"); + commandMap.put("ls2","path [watch]"); + commandMap.put("getAcl","path"); + commandMap.put("setAcl","path acl"); + commandMap.put("stat","path [watch]"); + commandMap.put("sync","path"); + commandMap.put("setquota","-n|-b val path"); + commandMap.put("listquota","path"); + commandMap.put("delquota","[-n|-b] path"); + commandMap.put("history",""); + commandMap.put("redo","cmdno"); + commandMap.put("printwatches", "on|off"); + commandMap.put("quit",""); + commandMap.put("addauth", "scheme auth"); + } + + static void usage() { + System.err.println("ZooKeeper -server host:port cmd args"); + for (Map.Entry entry : commandMap.entrySet()) { + System.err.println("\t" + entry.getKey() + " " + entry.getValue()); + } + } + + private class MyWatcher implements Watcher { + public void process(WatchedEvent event) { + if (getPrintWatches()) { + ZooKeeperMain.printMessage("WATCHER::"); + ZooKeeperMain.printMessage(event.toString()); + } + } + } + + static private 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; + } + + private static void printStat(Stat stat) { + System.err.println("cZxid = 0x" + Long.toHexString(stat.getCzxid())); + System.err.println("ctime = " + new Date(stat.getCtime()).toString()); + System.err.println("mZxid = 0x" + Long.toHexString(stat.getMzxid())); + System.err.println("mtime = " + new Date(stat.getMtime()).toString()); + System.err.println("pZxid = 0x" + Long.toHexString(stat.getPzxid())); + System.err.println("cversion = " + stat.getCversion()); + System.err.println("dataVersion = " + stat.getVersion()); + System.err.println("aclVersion = " + stat.getAversion()); + System.err.println("ephemeralOwner = 0x" + + Long.toHexString(stat.getEphemeralOwner())); + System.err.println("dataLength = " + stat.getDataLength()); + System.err.println("numChildren = " + stat.getNumChildren()); + } + + /** + * 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"); + } + } 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() { + return new LinkedList(commandMap.keySet()); + } + + 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; + zk = new ZooKeeper(host, + Integer.parseInt(cl.getOption("timeout")), + new MyWatcher(), readOnly); + } + + public static void main(String args[]) + throws KeeperException, 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")); + //zk = new ZooKeeper(cl.getOption("server"), +// Integer.parseInt(cl.getOption("timeout")), new MyWatcher()); + } + + public ZooKeeperMain(ZooKeeper zk) { + this.zk = zk; + } + + @SuppressWarnings("unchecked") + void run() throws KeeperException, 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.ConsoleReader"); + Class completorC = + Class.forName("org.apache.zookeeper.JLineZNodeCompletor"); + + System.out.println("JLine support is enabled"); + + Object console = + consoleC.getConstructor().newInstance(); + + Object completor = + completorC.getConstructor(ZooKeeper.class).newInstance(zk); + Method addCompletor = consoleC.getMethod("addCompletor", + Class.forName("jline.Completor")); + 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 e) { + LOG.debug("Unable to start jline", e); + jlinemissing = true; + } catch (NoSuchMethodException e) { + LOG.debug("Unable to start jline", e); + jlinemissing = true; + } catch (InvocationTargetException e) { + LOG.debug("Unable to start jline", e); + jlinemissing = true; + } catch (IllegalAccessException e) { + LOG.debug("Unable to start jline", e); + jlinemissing = true; + } catch (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); + } + } + + public void executeLine(String line) + throws InterruptedException, IOException, KeeperException { + if (!line.equals("")) { + cl.parseCommand(line); + addToHistory(commandCount,line); + processCmd(cl); + commandCount++; + } + } + + private static DataCallback dataCallback = new DataCallback() { + + public void processResult(int rc, String path, Object ctx, byte[] data, + Stat stat) { + System.out.println("rc = " + rc + " path = " + path + " data = " + + (data == null ? "null" : new String(data)) + " stat = "); + printStat(stat); + } + + }; + + /** + * 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 sucessful + * @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; + } + } + + /** + * 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 + { + 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(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; + } + + 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 (Quotas.limitNode.equals(child)) { + throw new IllegalArgumentException(path + " has a parent " + + quotaPath + " which has a quota"); + } + } + } + } + + /** + * this method creates a quota node for the path + * @param zk the ZooKeeper client + * @param path the path for which quota needs to be created + * @param bytes the limit of bytes on this path + * @param numNodes the limit of number of nodes on this path + * @return true if its successful and false if not. + */ + public static boolean createQuota(ZooKeeper zk, String path, + long bytes, int numNodes) + throws KeeperException, IOException, InterruptedException + { + // check if the path exists. We cannot create + // quota for a path that already exists in zookeeper + // for now. + Stat initStat = zk.exists(path, false); + 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_qutoas + // are not the children then this path + // is an ancestor of some path that + // already has quota + String realPath = Quotas.quotaZookeeper + path; + try { + List children = zk.getChildren(realPath, false); + for (String child: children) { + if (!child.startsWith("zookeeper_")) { + throw new IllegalArgumentException(path + " has child " + + child + " which has a quota"); + } + } + } catch(KeeperException.NoNodeException ne) { + // this is fine + } + + //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, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + zk.create(Quotas.quotaZookeeper, null, 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 2; + String path = null; + List acl = Ids.OPEN_ACL_UNSAFE; + LOG.debug("Processing " + cmd); + + if (cmd.equals("quit")) { + System.out.println("Quitting..."); + zk.close(); + System.exit(0); + } 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 + System.out.println("Command index out of range"); + return false; + } + cl.parseCommand(history.get(i)); + if (cl.getCommand().equals( "redo" )){ + System.out.println("No redoing redos"); + return false; + } + 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; + } + + if (cmd.equals("create") && args.length >= 3) { + int first = 0; + CreateMode flags = CreateMode.PERSISTENT; + if ((args[1].equals("-e") && args[2].equals("-s")) + || (args[1]).equals("-s") && (args[2].equals("-e"))) { + first+=2; + flags = CreateMode.EPHEMERAL_SEQUENTIAL; + } else if (args[1].equals("-e")) { + first++; + flags = CreateMode.EPHEMERAL; + } else if (args[1].equals("-s")) { + first++; + flags = CreateMode.PERSISTENT_SEQUENTIAL; + } + if (args.length == first + 4) { + acl = parseACLs(args[first+3]); + } + path = args[first + 1]; + String newPath = zk.create(path, args[first+2].getBytes(), acl, + flags); + System.err.println("Created " + newPath); + } else if (cmd.equals("delete") && args.length >= 2) { + path = args[1]; + zk.delete(path, watch ? Integer.parseInt(args[2]) : -1); + } else if (cmd.equals("rmr") && args.length >= 2) { + path = args[1]; + ZKUtil.deleteRecursive(zk, path); + } else if (cmd.equals("set") && args.length >= 3) { + path = args[1]; + stat = zk.setData(path, args[2].getBytes(), + args.length > 3 ? Integer.parseInt(args[3]) : -1); + printStat(stat); + } else if (cmd.equals("aget") && args.length >= 2) { + path = args[1]; + zk.getData(path, watch, dataCallback, path); + } else if (cmd.equals("get") && args.length >= 2) { + path = args[1]; + byte data[] = zk.getData(path, watch, stat); + data = (data == null)? "null".getBytes() : data; + System.out.println(new String(data)); + printStat(stat); + } else if (cmd.equals("ls") && args.length >= 2) { + path = args[1]; + List children = zk.getChildren(path, watch); + System.out.println(children); + } else if (cmd.equals("ls2") && args.length >= 2) { + path = args[1]; + List children = zk.getChildren(path, watch, stat); + System.out.println(children); + printStat(stat); + } else if (cmd.equals("getAcl") && args.length >= 2) { + path = args[1]; + acl = zk.getACL(path, stat); + for (ACL a : acl) { + System.out.println(a.getId() + ": " + + getPermString(a.getPerms())); + } + } else if (cmd.equals("setAcl") && args.length >= 3) { + path = args[1]; + stat = zk.setACL(path, parseACLs(args[2]), + args.length > 4 ? Integer.parseInt(args[3]) : -1); + printStat(stat); + } else if (cmd.equals("stat") && args.length >= 2) { + path = args[1]; + stat = zk.exists(path, watch); + if (stat == null) { + throw new KeeperException.NoNodeException(path); + } + printStat(stat); + } else if (cmd.equals("listquota") && args.length >= 2) { + path = args[1]; + String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; + byte[] data = null; + try { + System.err.println("absolute path is " + absolutePath); + data = zk.getData(absolutePath, false, stat); + StatsTrack st = new StatsTrack(new String(data)); + System.out.println("Output quota for " + path + " " + + st.toString()); + + data = zk.getData(Quotas.quotaZookeeper + path + "/" + + Quotas.statNode, false, stat); + System.out.println("Output stat for " + path + " " + + new StatsTrack(new String(data)).toString()); + } catch(KeeperException.NoNodeException ne) { + System.err.println("quota for " + path + " does not exist."); + } + } else if (cmd.equals("setquota") && args.length >= 4) { + String option = args[1]; + String val = args[2]; + path = args[3]; + System.err.println("Comment: the parts are " + + "option " + option + + " val " + val + + " path " + path); + if ("-b".equals(option)) { + // we are setting the bytes quota + createQuota(zk, path, Long.parseLong(val), -1); + } else if ("-n".equals(option)) { + // we are setting the num quota + createQuota(zk, path, -1L, Integer.parseInt(val)); + } else { + usage(); + } + + } else if (cmd.equals("delquota") && args.length >= 2) { + //if neither option -n or -b is specified, we delete + // the quota node for thsi node. + if (args.length == 3) { + //this time we have an option + String option = args[1]; + path = args[2]; + if ("-b".equals(option)) { + delQuota(zk, path, true, false); + } else if ("-n".equals(option)) { + delQuota(zk, path, false, true); + } + } else if (args.length == 2) { + path = args[1]; + // we dont have an option specified. + // just delete whole quota node + delQuota(zk, path, true, true); + } else if (cmd.equals("help")) { + usage(); + } + } else if (cmd.equals("close")) { + zk.close(); + } else if (cmd.equals("sync") && args.length >= 2) { + path = args[1]; + zk.sync(path, new AsyncCallback.VoidCallback() { public void processResult(int rc, String path, Object ctx) { System.out.println("Sync returned " + rc); } }, null ); + } else if (cmd.equals("addauth") && args.length >=2 ) { + byte[] b = null; + if (args.length >= 3) + b = args[2].getBytes(); + + zk.addAuthInfo(args[1], b); + } else if (!commandMap.containsKey(cmd)) { + usage(); + } + return watch; + } + + private static String getPermString(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(); + } + + private static List parseACLs(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; + } +} diff --git a/src/java/main/org/apache/zookeeper/ZooKeeperTestable.java b/src/java/main/org/apache/zookeeper/ZooKeeperTestable.java new file mode 100644 index 0000000..775d1a2 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/ZooKeeperTestable.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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ZooKeeperTestable implements Testable { + private static final Logger LOG = LoggerFactory + .getLogger(ZooKeeperTestable.class); + + private final ZooKeeper zooKeeper; + private final ClientCnxn clientCnxn; + + ZooKeeperTestable(ZooKeeper zooKeeper, ClientCnxn clientCnxn) { + this.zooKeeper = zooKeeper; + 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.sendThread.getClientCnxnSocket().wakeupCnxn(); + clientCnxn.state = ZooKeeper.States.CLOSED; + } +} diff --git a/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.java new file mode 100644 index 0000000..ff0425a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/client/ConnectStringParser.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.client; + +import java.net.InetSocketAddress; +import java.util.ArrayList; + +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(); + + /** + * + * @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; + } + + String hostsList[] = connectString.split(","); + for (String host : hostsList) { + int port = DEFAULT_PORT; + 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; + } +} \ No newline at end of file diff --git a/src/java/main/org/apache/zookeeper/client/FourLetterWordMain.java b/src/java/main/org/apache/zookeeper/client/FourLetterWordMain.java new file mode 100644 index 0000000..a4175e4 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/client/FourLetterWordMain.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.client; + +import org.apache.log4j.Logger; + +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 org.apache.yetus.audience.InterfaceAudience; + +@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 = Logger.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 + */ + public static String send4LetterWord(String host, int port, String cmd) + throws IOException + { + return send4LetterWord(host, port, cmd, DEFAULT_SOCKET_TIMEOUT); + } + /** + * Send the 4letterword + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @param timeout in milliseconds, maximum time to wait while connecting/reading data + * @return server response + * @throws java.io.IOException + */ + public static String send4LetterWord(String host, int port, String cmd, int timeout) + throws IOException + { + LOG.info("connecting to " + host + " " + port); + Socket sock = new Socket(); + InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : + new InetSocketAddress(InetAddress.getByName(null), port); + BufferedReader reader = null; + try { + sock.setSoTimeout(timeout); + sock.connect(hostaddress, timeout); + 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 = new StringBuilder(); + String line; + while((line = reader.readLine()) != null) { + sb.append(line + "\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 + { + if (args.length != 3) { + System.out.println("Usage: FourLetterWordMain "); + } else { + System.out.println(send4LetterWord(args[0], Integer.parseInt(args[1]), args[2])); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/client/HostProvider.java b/src/java/main/org/apache/zookeeper/client/HostProvider.java new file mode 100644 index 0000000..61d9108 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/client/HostProvider.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.client; + +import org.apache.yetus.audience.InterfaceAudience; + +import java.net.InetSocketAddress; +import java.net.UnknownHostException; + +/** + * 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(), + * but it's up to the HostProvider, when it wants to do the resolving. + * + * 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 { + public 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. + * @return The next host to try to connect to with resolved address. If the host is not resolvable, the unresolved + * address will be returned. + */ + public InetSocketAddress next(long spinDelay); + + /** + * Notify the HostProvider of a successful connection. + * + * The HostProvider may use this notification to reset it's inner state. + */ + public void onConnected(); +} diff --git a/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java b/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java new file mode 100644 index 0000000..0005942 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java @@ -0,0 +1,179 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 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 final List serverAddresses = new ArrayList(5); + + private int lastIndex = -1; + + private int currentIndex = -1; + + 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) { + this.resolver = new Resolver() { + @Override + public InetAddress[] getAllByName(String name) throws UnknownHostException { + return InetAddress.getAllByName(name); + } + }; + init(serverAddresses); + } + + /** + * 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 + * @throws IllegalArgumentException + * if serverAddresses is empty or resolves to an empty list + */ + public StaticHostProvider(Collection serverAddresses, Resolver resolver) { + this.resolver = resolver; + init(serverAddresses); + } + + /** + * Common init method for all constructors. + * Resolve all unresolved server addresses, put them in a list and shuffle. + */ + private void init(Collection serverAddresses) { + if (serverAddresses.isEmpty()) { + throw new IllegalArgumentException( + "A HostProvider may not be empty!"); + } + + this.serverAddresses.addAll(serverAddresses); + Collections.shuffle(this.serverAddresses); + } + + /** + * Evaluate to a hostname if one is available and otherwise it returns the + * string representation of the IP address. + * + * In Java 7, we have a method getHostString, but earlier versions do not support it. + * This method is to provide a replacement for InetSocketAddress.getHostString(). + * + * @param addr + * @return Hostname string of address parameter + */ + private String getHostString(InetSocketAddress addr) { + String hostString = ""; + + if (addr == null) { + return hostString; + } + if (!addr.isUnresolved()) { + InetAddress ia = addr.getAddress(); + + // If the string starts with '/', then it has no hostname + // and we want to avoid the reverse lookup, so we return + // the string representation of the address. + if (ia.toString().startsWith("/")) { + hostString = ia.getHostAddress(); + } else { + hostString = addr.getHostName(); + } + } else { + // According to the Java 6 documentation, if the hostname is + // unresolved, then the string before the colon is the hostname. + String addrString = addr.toString(); + hostString = addrString.substring(0, addrString.lastIndexOf(':')); + } + + return hostString; + } + + public int size() { + return serverAddresses.size(); + } + + public InetSocketAddress next(long spinDelay) { + currentIndex = ++currentIndex % serverAddresses.size(); + if (currentIndex == lastIndex && spinDelay > 0) { + try { + Thread.sleep(spinDelay); + } catch (InterruptedException e) { + LOG.warn("Unexpected exception", e); + } + } else if (lastIndex == -1) { + // We don't want to sleep on the first ever connect attempt. + lastIndex = 0; + } + + InetSocketAddress curAddr = serverAddresses.get(currentIndex); + try { + String curHostString = getHostString(curAddr); + List resolvedAddresses = new ArrayList(Arrays.asList(this.resolver.getAllByName(curHostString))); + if (resolvedAddresses.isEmpty()) { + return curAddr; + } + Collections.shuffle(resolvedAddresses); + return new InetSocketAddress(resolvedAddresses.get(0), curAddr.getPort()); + } catch (UnknownHostException e) { + return curAddr; + } + } + + @Override + public void onConnected() { + lastIndex = currentIndex; + } +} diff --git a/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java b/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java new file mode 100644 index 0000000..af3303c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java @@ -0,0 +1,439 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.Environment; +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 { + public static final String LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.clientconfig"; + public static final String ENABLE_CLIENT_SASL_KEY = "zookeeper.sasl.client"; + public static final String ENABLE_CLIENT_SASL_DEFAULT = "true"; + private static 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 If the SASL client is enabled. + */ + public static boolean isEnabled() { + return Boolean.valueOf(System.getProperty(ENABLE_CLIENT_SASL_KEY, ENABLE_CLIENT_SASL_DEFAULT)); + } + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperSaslClient.class); + private static Login login = null; + private SaslClient saslClient; + private boolean isSASLConfigured = true; + + 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) + throws LoginException { + /** + * ZOOKEEPER-1373: allow system property to specify the JAAS + * configuration section that the zookeeper client should use. + * Default to "Client". + */ + String clientSection = System.getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY, "Client"); + // 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 = System.getProperty(ZooKeeperSaslClient.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: '" + + System.getProperty(Environment.JAAS_CONF_KEY) + "' 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 (System.getProperty(Environment.JAAS_CONF_KEY) != 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 '" + + System.getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY, "Client") + + "' section of the supplied JAAS configuration: '" + + System.getProperty(Environment.JAAS_CONF_KEY) + "' because of a " + + "RuntimeException: " + runtimeException); + } else { + throw new LoginException("No JAAS configuration section named '" + + System.getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY, "Client") + + "' was found in specified JAAS configuration file: '" + + System.getProperty(Environment.JAAS_CONF_KEY) + "'."); + } + } + } + } + + /** + * @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 (ZooKeeperSaslClient.class) { + if (login == null) { + if (LOG.isDebugEnabled()) { + 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")); + 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() + "' with exception: {}", 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.enableWrite(); + } + } + + 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().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 client's JVMFLAGS environment."; + } + error += " Zookeeper Client will go to AUTH_FAILED state."; + LOG.error(error); + saslState = SaslState.FAILED; + throw new SaslException(error); + } + } + } + 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{ + if (LOG.isDebugEnabled()) { + 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 { + if (LOG.isDebugEnabled()) { + 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 ((System.getProperty(Environment.JAAS_CONF_KEY) != null) || + ((javax.security.auth.login.Configuration.getConfiguration() != null) && + (javax.security.auth.login.Configuration.getConfiguration(). + getAppConfigurationEntry(System. + getProperty(ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY,"Client")) + != 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() == false) && + (isFailed() == false)) { + return true; + } + + // 2. SASL authentication has succeeded or failed.. + if (isComplete() || isFailed()) { + if (gotLastPacket == false) { + // ..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. + if (LOG.isDebugEnabled() == true) { + LOG.debug("Could not retrieve login configuration: " + e); + } + return false; + } + } + + +} diff --git a/src/java/main/org/apache/zookeeper/common/AtomicFileOutputStream.java b/src/java/main/org/apache/zookeeper/common/AtomicFileOutputStream.java new file mode 100644 index 0000000..2584d3f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/AtomicFileOutputStream.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.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 { + private static final String TMP_EXTENSION = ".tmp"; + + private final static 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).getChannel().force(true); + + 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/src/java/main/org/apache/zookeeper/common/IOUtils.java b/src/java/main/org/apache/zookeeper/common/IOUtils.java new file mode 100644 index 0000000..16aea4e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/IOUtils.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.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/src/java/main/org/apache/zookeeper/common/PathTrie.java b/src/java/main/org/apache/zookeeper/common/PathTrie.java new file mode 100644 index 0000000..73053e0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/PathTrie.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.common; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +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 { + /** + * the logger for this class + */ + private static final Logger LOG = LoggerFactory.getLogger(PathTrie.class); + + /** + * the root node of PathTrie + */ + private final TrieNode rootNode ; + + static class TrieNode { + boolean property = false; + final HashMap children; + TrieNode parent = null; + /** + * create a trienode with parent + * as parameter + * @param parent the parent of this trienode + */ + private TrieNode(TrieNode parent) { + children = new HashMap(); + this.parent = parent; + } + + /** + * 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 getProperty() { + return this.property; + } + /** + * 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) { + synchronized(children) { + if (children.containsKey(childName)) { + return; + } + children.put(childName, node); + } + } + + /** + * delete child from this node + * @param childName the string name of the child to + * be deleted + */ + void deleteChild(String childName) { + synchronized(children) { + if (!children.containsKey(childName)) { + return; + } + TrieNode childNode = children.get(childName); + // this is the only child node. + if (childNode.getChildren().length == 1) { + childNode.setParent(null); + children.remove(childName); + } + else { + // their are more child nodes + // so just reset property. + childNode.setProperty(false); + } + } + } + + /** + * return the child of a node mapping + * to the input childname + * @param childName the name of the child + * @return the child of a node + */ + TrieNode getChild(String childName) { + synchronized(children) { + if (!children.containsKey(childName)) { + return null; + } + else { + return children.get(childName); + } + } + } + + /** + * get the list of children of this + * trienode. + * @param node to get its children + * @return the string list of its children + */ + String[] getChildren() { + synchronized(children) { + return children.keySet().toArray(new String[0]); + } + } + + /** + * get the string representation + * for this node + */ + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Children of trienode: "); + synchronized(children) { + for (String str: children.keySet()) { + sb.append(" " + str); + } + } + return sb.toString(); + } + } + + /** + * construct a new PathTrie with + * a root node of / + */ + public PathTrie() { + this.rootNode = new TrieNode(null); + } + + /** + * add a path to the path trie + * @param path + */ + public void addPath(String path) { + if (path == null) { + return; + } + String[] pathComponents = path.split("/"); + TrieNode parent = rootNode; + String part = null; + if (pathComponents.length <= 1) { + throw new IllegalArgumentException("Invalid path " + path); + } + for (int i=1; i components = new ArrayList(); + if (pathComponents.length <= 1) { + throw new IllegalArgumentException("Invalid path " + path); + } + int i = 1; + String part = null; + StringBuilder sb = new StringBuilder(); + int lastindex = -1; + while((i < pathComponents.length)) { + if (parent.getChild(pathComponents[i]) != null) { + part = pathComponents[i]; + parent = parent.getChild(part); + components.add(part); + if (parent.getProperty()) { + lastindex = i-1; + } + } + else { + break; + } + i++; + } + for (int j=0; j< (lastindex+1); j++) { + sb.append("/" + components.get(j)); + } + return sb.toString(); + } + + /** + * clear all nodes + */ + public void clear() { + for(String child : rootNode.getChildren()) { + rootNode.deleteChild(child); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/common/PathUtils.java b/src/java/main/org/apache/zookeeper/common/PathUtils.java new file mode 100644 index 0000000..2a6c7ef --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/PathUtils.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.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); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/common/Time.java b/src/java/main/org/apache/zookeeper/common/Time.java new file mode 100644 index 0000000..83e53f0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/common/Time.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.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); + } +} \ No newline at end of file diff --git a/src/java/main/org/apache/zookeeper/jmx/CommonNames.java b/src/java/main/org/apache/zookeeper/jmx/CommonNames.java new file mode 100644 index 0000000..986d69d --- /dev/null +++ b/src/java/main/org/apache/zookeeper/jmx/CommonNames.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.jmx; + +/** + * A bunch of constants. + * TODO: will get rid of it eventually. + */ +public class CommonNames { + public static final String DOMAIN="org.apache.ZooKeeperService"; + public static final String DATA_TREE_KEY="DataTree"; + public static final String STANDALONE_SERVER_KEY="StandaloneServer"; +} diff --git a/src/java/main/org/apache/zookeeper/jmx/MBeanRegistry.java b/src/java/main/org/apache/zookeeper/jmx/MBeanRegistry.java new file mode 100644 index 0000000..0fcfe2f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/jmx/MBeanRegistry.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.jmx; + +import java.lang.management.ManagementFactory; +import java.util.Map; +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 { + private static final Logger LOG = LoggerFactory.getLogger(MBeanRegistry.class); + + private static volatile MBeanRegistry instance = new MBeanRegistry(); + + private Map mapBean2Path = + new ConcurrentHashMap(); + + private Map mapName2Bean = + new ConcurrentHashMap(); + + private MBeanServer mBeanServer; + + 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 { + mBeanServer.registerMBean(bean, oname); + mapBean2Path.put(bean, path); + mapName2Bean.put(bean.getName(), bean); + } 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()) { + try { + mBeanServer.unregisterMBean(makeObjectName(path, bean)); + } catch (JMException e) { + LOG.warn("Failed to unregister MBean " + bean.getName()); + throw e; + } + } + } + + /** + * Unregister MBean. + * @param bean + */ + public void unregister(ZKMBeanInfo bean) { + if(bean==null) + return; + String path=mapBean2Path.get(bean); + try { + unregister(path,bean); + } catch (JMException e) { + LOG.warn("Error during unregister", e); + } + mapBean2Path.remove(bean); + mapName2Bean.remove(bean.getName()); + } + /** + * Unregister all currently registered MBeans + */ + public void unregisterAll() { + for(Map.Entry e: mapBean2Path.entrySet()) { + try { + unregister(e.getValue(), e.getKey()); + } catch (JMException e1) { + LOG.warn("Error during unregister", e1); + } + } + mapBean2Path.clear(); + mapName2Bean.clear(); + } + /** + * 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(CommonNames.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 \"" + beanName.toString() + "\" for class " + + bean.getClass().toString()); + throw e; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/jmx/ManagedUtil.java b/src/java/main/org/apache/zookeeper/jmx/ManagedUtil.java new file mode 100644 index 0000000..2f04281 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/jmx/ManagedUtil.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.jmx; + +import java.util.Enumeration; + +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.log4j.jmx.HierarchyDynamicMBean; +import org.apache.log4j.spi.LoggerRepository; + +/** + * Shared utilities + */ +public class ManagedUtil { + /** + * Register the log4j JMX mbeans. Set environment variable + * "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 (Boolean.getBoolean("zookeeper.jmx.log4j.disable") == true) { + return; + } + + MBeanServer mbs = MBeanRegistry.getInstance().getPlatformMBeanServer(); + + // Create and Register the top level Log4J MBean + HierarchyDynamicMBean hdm = new HierarchyDynamicMBean(); + + ObjectName mbo = new ObjectName("log4j:hiearchy=default"); + mbs.registerMBean(hdm, mbo); + + // Add the root logger to the Hierarchy MBean + Logger rootLogger = Logger.getRootLogger(); + hdm.addLoggerMBean(rootLogger.getName()); + + // Get each logger from the Log4J Repository and add it to + // the Hierarchy MBean created above. + LoggerRepository r = LogManager.getLoggerRepository(); + Enumeration enumer = r.getCurrentLoggers(); + Logger logger = null; + + while (enumer.hasMoreElements()) { + logger = (Logger) enumer.nextElement(); + hdm.addLoggerMBean(logger.getName()); + } + } + +} diff --git a/src/java/main/org/apache/zookeeper/jmx/ZKMBeanInfo.java b/src/java/main/org/apache/zookeeper/jmx/ZKMBeanInfo.java new file mode 100644 index 0000000..1e87d92 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/jmx/ZKMBeanInfo.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.jmx; + +/** + * Zookeeper MBean info interface. MBeanRegistry uses the interface to generate + * JMX object name. + */ +public interface ZKMBeanInfo { + /** + * @return a string identifying the MBean + */ + public 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. + */ + public boolean isHidden(); +} diff --git a/src/java/main/org/apache/zookeeper/server/ByteBufferInputStream.java b/src/java/main/org/apache/zookeeper/server/ByteBufferInputStream.java new file mode 100644 index 0000000..e5ac120 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ByteBufferInputStream.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 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 { + long newPos = bb.position() + n; + if (newPos > bb.remaining()) { + n = bb.remaining(); + } + bb.position(bb.position() + (int) n); + return n; + } + + static public void byteBuffer2Record(ByteBuffer bb, Record record) + throws IOException { + BinaryInputArchive ia; + ia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb)); + record.deserialize(ia, "request"); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/ByteBufferOutputStream.java b/src/java/main/org/apache/zookeeper/server/ByteBufferOutputStream.java new file mode 100644 index 0000000..a2fcc95 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ByteBufferOutputStream.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; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; + +import org.apache.jute.BinaryInputArchive; +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); + } + static public void record2ByteBuffer(Record record, ByteBuffer bb) + throws IOException { + BinaryOutputArchive oa; + oa = BinaryOutputArchive.getArchive(new ByteBufferOutputStream(bb)); + record.serialize(oa, "request"); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ConnectionBean.java b/src/java/main/org/apache/zookeeper/server/ConnectionBean.java new file mode 100644 index 0000000..58917e0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ConnectionBean.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; + +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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * 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/src/java/main/org/apache/zookeeper/server/ConnectionMXBean.java b/src/java/main/org/apache/zookeeper/server/ConnectionMXBean.java new file mode 100644 index 0000000..8bdec9a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ConnectionMXBean.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; + +/** + * This MBean represents a client connection. + */ +public interface ConnectionMXBean { + /** + * @return source (client) IP address + */ + public String getSourceIP(); + /** + * @return client's session id + */ + public String getSessionId(); + /** + * @return time the connection was started + */ + public String getStartedTime(); + /** + * @return number of ephemeral nodes owned by this connection + */ + public String[] getEphemeralNodes(); + /** + * @return packets received from this client + */ + public long getPacketsReceived(); + /** + * @return number of packets sent to this client + */ + public long getPacketsSent(); + /** + * @return number of requets being processed + */ + public long getOutstandingRequests(); + /** + * @return session timeout in ms + */ + public int getSessionTimeout(); + + /** + * Terminate this client session. The client will reconnect with a different + * session id. + */ + public void terminateSession(); + /** + * Terminate thei client connection. The client will immediately attempt to + * reconnect with the same session id. + */ + public 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/src/java/main/org/apache/zookeeper/server/DataNode.java b/src/java/main/org/apache/zookeeper/server/DataNode.java new file mode 100644 index 0000000..8efdaf8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/DataNode.java @@ -0,0 +1,180 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.HashSet; +import java.util.Set; +import java.util.Collections; + +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. + * + */ +public class DataNode implements Record { + /** the parent of this datanode */ + DataNode parent; + + /** 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 parent + * the parent of this DataNode + * @param data + * the data to be set + * @param acl + * the acls for this node + * @param stat + * the stat for this node. + */ + public DataNode(DataNode parent, byte data[], Long acl, StatPersisted stat) { + this.parent = parent; + 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 + */ + public synchronized Set getChildren() { + if (children == null) { + return EMPTY_SET; + } + + return Collections.unmodifiableSet(children); + } + + synchronized public 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(stat.getEphemeralOwner()); + 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); + } + + synchronized public 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"); + } + + synchronized public 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"); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/DataTree.java b/src/java/main/org/apache/zookeeper/server/DataTree.java new file mode 100644 index 0000000..f2537f3 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/DataTree.java @@ -0,0 +1,1237 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +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.Code; +import org.apache.zookeeper.KeeperException.NoNodeException; +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.ZooDefs.OpCode; +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.upgrade.DataNodeV1; +import org.apache.zookeeper.txn.CheckVersionTxn; +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.TxnHeader; +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); + + /** + * This hashtable provides a fast lookup to the datanodes. The tree is the + * source of truth and is where all the locking occurs + */ + private final ConcurrentHashMap nodes = + new ConcurrentHashMap(); + + private final WatchManager dataWatches = new WatchManager(); + + private final WatchManager childWatches = new WatchManager(); + + /** 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 path trie that keeps track fo the quota nodes in this datatree + */ + private final PathTrie pTrie = new PathTrie(); + + /** + * This hashtable lists the paths of the ephemeral nodes of a session. + */ + private final Map> ephemerals = + new ConcurrentHashMap>(); + + private final ReferenceCountedACLCache aclCache = new ReferenceCountedACLCache(); + + @SuppressWarnings("unchecked") + public HashSet getEphemerals(long sessionId) { + HashSet retv = ephemerals.get(sessionId); + if (retv == null) { + return new HashSet(); + } + HashSet cloned = null; + synchronized (retv) { + cloned = (HashSet) retv.clone(); + } + return cloned; + } + + public Map> getEphemeralsMap() { + return ephemerals; + } + + + public Collection getSessions() { + return ephemerals.keySet(); + } + + /** + * just an accessor method to allow raw creation of datatree's from a bunch + * of datanodes + * + * @param path + * the path of the datanode + * @param node + * the datanode corresponding to this path + */ + public void addDataNode(String path, DataNode node) { + nodes.put(path, node); + } + + 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() { + Map> map = this.getEphemeralsMap(); + int result = 0; + for (HashSet set : map.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 += entry.getKey().length(); + result += (value.data == null ? 0 + : value.data.length); + } + } + return result; + } + + /** + * 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(null, new byte[0], -1L, + new StatPersisted()); + + /** + * create a /zookeeper filesystem that is the proc filesystem of zookeeper + */ + private DataNode procDataNode = new DataNode(root, new byte[0], -1L, + new StatPersisted()); + + /** + * create a /zookeeper/quota node for maintaining quota properties for + * zookeeper + */ + private DataNode quotaDataNode = new DataNode(procDataNode, new byte[0], + -1L, new StatPersisted()); + + public DataTree() { + /* Rather than fight it, let root have an alias */ + nodes.put("", root); + nodes.put(rootZookeeper, root); + + /** add the proc node and quota node */ + root.addChild(procChildZookeeper); + nodes.put(procZookeeper, procDataNode); + + procDataNode.addChild(quotaChildZookeeper); + nodes.put(quotaZookeeper, quotaDataNode); + } + + /** + * 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) { + if (rootZookeeper.equals(path) || procZookeeper.equals(path) + || quotaZookeeper.equals(path)) { + return true; + } + return false; + } + + static public 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()); + } + + static public 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 of this stat datanode + * + * @param lastPrefix + * the path of the node that is quotaed. + * @param diff + * the diff to be added to the count + */ + public void updateCount(String lastPrefix, int diff) { + 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() + diff); + 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: " + lastPrefix + " count=" + + updatedStat.getCount() + " limit=" + + thisStats.getCount()); + } + } + + /** + * update the count of bytes of this stat datanode + * + * @param lastPrefix + * the path of the node that is quotaed + * @param diff + * the diff to added to number of bytes + * @throws IOException + * if path is not found + */ + public void updateBytes(String lastPrefix, long diff) { + String statNode = Quotas.statPath(lastPrefix); + DataNode node = nodes.get(statNode); + if (node == null) { + // should never be null but just to make + // findbugs happy + LOG.error("Missing stat node for bytes " + statNode); + return; + } + StatsTrack updatedStat = null; + synchronized (node) { + updatedStat = new StatsTrack(new String(node.data)); + updatedStat.setBytes(updatedStat.getBytes() + diff); + node.data = updatedStat.toString().getBytes(); + } + // now check if the bytes match the quota + String quotaNode = Quotas.quotaPath(lastPrefix); + node = nodes.get(quotaNode); + if (node == null) { + // should never be null but just to make + // findbugs happy + LOG.error("Missing quota node for bytes " + quotaNode); + return; + } + StatsTrack thisStats = null; + synchronized (node) { + thisStats = new StatsTrack(new String(node.data)); + } + if (thisStats.getBytes() > -1 && (thisStats.getBytes() < updatedStat.getBytes())) { + LOG + .warn("Quota exceeded: " + lastPrefix + " bytes=" + + updatedStat.getBytes() + " limit=" + + thisStats.getBytes()); + } + } + + /** + * @param path + * @param data + * @param acl + * @param ephemeralOwner + * the session id that owns this node. -1 indicates this is not + * an ephemeral node. + * @param zxid + * @param time + * @return the patch of the created node + * @throws KeeperException + */ + public String createNode(String path, byte data[], List acl, + long ephemeralOwner, int parentCVersion, long zxid, long time) + throws KeeperException.NoNodeException, + KeeperException.NodeExistsException { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + String childName = path.substring(lastSlash + 1); + 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); + DataNode parent = nodes.get(parentName); + if (parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (parent) { + Set children = parent.getChildren(); + if (children.contains(childName)) { + throw new KeeperException.NodeExistsException(); + } + + if (parentCVersion == -1) { + parentCVersion = parent.stat.getCversion(); + parentCVersion++; + } + parent.stat.setCversion(parentCVersion); + parent.stat.setPzxid(zxid); + Long longval = aclCache.convertAcls(acl); + DataNode child = new DataNode(parent, data, longval, stat); + parent.addChild(childName); + nodes.put(path, child); + if (ephemeralOwner != 0) { + HashSet list = ephemerals.get(ephemeralOwner); + if (list == null) { + list = new HashSet(); + ephemerals.put(ephemeralOwner, list); + } + synchronized (list) { + list.add(path); + } + } + } + // 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; + if((lastPrefix = getMaxPrefixWithQuota(path)) != null) { + // ok we have some match and need to update + updateCount(lastPrefix, 1); + updateBytes(lastPrefix, data == null ? 0 : data.length); + } + dataWatches.triggerWatch(path, Event.EventType.NodeCreated); + childWatches.triggerWatch(parentName.equals("") ? "/" : parentName, + Event.EventType.NodeChildrenChanged); + return path; + } + + /** + * 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); + DataNode node = nodes.get(path); + if (node == null) { + throw new KeeperException.NoNodeException(); + } + nodes.remove(path); + synchronized (node) { + aclCache.removeUsage(node.acl); + } + DataNode parent = nodes.get(parentName); + if (parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (parent) { + parent.removeChild(childName); + parent.stat.setPzxid(zxid); + long eowner = node.stat.getEphemeralOwner(); + if (eowner != 0) { + HashSet nodes = ephemerals.get(eowner); + if (nodes != null) { + synchronized (nodes) { + nodes.remove(path); + } + } + } + node.parent = null; + } + if (parentName.startsWith(procZookeeper)) { + // delete the node in the trie. + if (Quotas.limitNode.equals(childName)) { + // 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; + if((lastPrefix = getMaxPrefixWithQuota(path)) != null) { + // ok we have some match and need to update + updateCount(lastPrefix, -1); + int bytes = 0; + synchronized (node) { + bytes = (node.data == null ? 0 : -(node.data.length)); + } + updateBytes(lastPrefix, bytes); + } + 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); + } + Set processed = dataWatches.triggerWatch(path, + EventType.NodeDeleted); + childWatches.triggerWatch(path, EventType.NodeDeleted, processed); + childWatches.triggerWatch(parentName.equals("") ? "/" : parentName, + EventType.NodeChildrenChanged); + } + + public Stat setData(String path, byte data[], int version, long zxid, + long time) throws KeeperException.NoNodeException { + Stat s = new Stat(); + DataNode n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + byte lastdata[] = null; + synchronized (n) { + lastdata = n.data; + n.data = data; + n.stat.setMtime(time); + n.stat.setMzxid(zxid); + n.stat.setVersion(version); + n.copyStat(s); + } + // now update if the path is in a quota subtree. + String lastPrefix; + if((lastPrefix = getMaxPrefixWithQuota(path)) != null) { + this.updateBytes(lastPrefix, (data == null ? 0 : data.length) + - (lastdata == null ? 0 : lastdata.length)); + } + 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) && !("".equals(lastPrefix))) { + return lastPrefix; + } + else { + return null; + } + } + + public byte[] getData(String path, Stat stat, Watcher watcher) + throws KeeperException.NoNodeException { + DataNode n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + if (watcher != null) { + dataWatches.addWatch(path, watcher); + } + return n.data; + } + } + + public Stat statNode(String path, Watcher watcher) + throws KeeperException.NoNodeException { + Stat stat = new Stat(); + DataNode n = nodes.get(path); + if (watcher != null) { + dataWatches.addWatch(path, watcher); + } + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + 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(); + } + synchronized (n) { + if (stat != null) { + n.copyStat(stat); + } + List children = new ArrayList(n.getChildren()); + + if (watcher != null) { + childWatches.addWatch(path, watcher); + } + return children; + } + } + + 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); + n.stat.setAversion(version); + n.acl = aclCache.convertAcls(acl); + n.copyStat(stat); + return stat; + } + } + + @SuppressWarnings("unchecked") + public List getACL(String path, Stat stat) + throws KeeperException.NoNodeException { + DataNode n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + return new ArrayList(aclCache.convertLong(n.acl)); + } + } + + public List getACL(DataNode node) { + synchronized (node) { + return aclCache.convertLong(node.acl); + } + } + + public Long getACL(DataNodeV1 oldDataNode) { + synchronized (oldDataNode) { + return aclCache.convertAcls(oldDataNode.acl); + } + } + + public int aclCacheSize() { + return aclCache.size(); + } + + static public 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) + { + 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()) { + case OpCode.create: + CreateTxn createTxn = (CreateTxn) txn; + rc.path = createTxn.getPath(); + createNode( + createTxn.getPath(), + createTxn.getData(), + createTxn.getAcl(), + createTxn.getEphemeral() ? header.getClientId() : 0, + createTxn.getParentCVersion(), + header.getZxid(), header.getTime()); + break; + case OpCode.delete: + DeleteTxn deleteTxn = (DeleteTxn) txn; + rc.path = deleteTxn.getPath(); + deleteNode(deleteTxn.getPath(), header.getZxid()); + break; + 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: + killSession(header.getClientId(), 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.delete: + 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); + } + + if (failed) { + assert(subtxn.getType() == OpCode.error) ; + } + + TxnHeader subHdr = new TxnHeader(header.getClientId(), header.getCxid(), + header.getZxid(), header.getTime(), + subtxn.getType()); + ProcessTxnResult subRc = processTxn(subHdr, record); + rc.multiResult.add(subRc); + if (subRc.err != 0 && rc.err == 0) { + rc.err = subRc.err ; + } + } + break; + } + } catch (KeeperException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Failed: " + header + ":" + txn, e); + } + rc.err = e.code().intValue(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Failed: " + header + ":" + txn, e); + } + } + /* + * 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; + } + + /* + * Snapshots are taken lazily. It can happen that the child + * znodes of a parent are created after the parent + * is serialized. Therefore, while replaying logs during restore, a + * create might fail because the node was already + * created. + * + * After seeing this failure, we should increment + * the cversion of the parent znode since the parent was serialized + * before its children. + * + * 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: " + header.getType() + + " path:" + rc.path + " err: " + 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: " + header.getType() + + " : error: " + rc.err); + } + 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. + HashSet list = ephemerals.remove(session); + if (list != null) { + for (String path : list) { + try { + deleteNode(path, zxid); + if (LOG.isDebugEnabled()) { + LOG + .debug("Deleting ephemeral node " + path + + " for session 0x" + + Long.toHexString(session)); + } + } catch (NoNodeException e) { + LOG.warn("Ignoring NoNodeException for path " + path + + " while removing ephemeral for dead session 0x" + + Long.toHexString(session)); + } + } + } + } + + /** + * 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) { + node.data = strack.toString().getBytes(); + } + } + + /** + * 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 + * @throws InterruptedException + */ + 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) { + scount++; + 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.parent, node.data, node.acl, statCopy); + Set childs = node.getChildren(); + children = childs.toArray(new String[childs.size()]); + } + oa.writeString(pathString, "path"); + oa.writeRecord(nodeCopy, "node"); + 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); + } + } + + int scount; + + public boolean initialized = false; + + public void serialize(OutputArchive oa, String tag) throws IOException { + scount = 0; + aclCache.serialize(oa); + 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 deserialize(InputArchive ia, String tag) throws IOException { + aclCache.deserialize(ia); + nodes.clear(); + pTrie.clear(); + String path = ia.readString("path"); + while (!path.equals("/")) { + 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); + node.parent = nodes.get(parentPath); + if (node.parent == null) { + throw new IOException("Invalid Datatree, unable to find " + + "parent " + parentPath + " of path " + path); + } + node.parent.addChild(path.substring(lastSlash + 1)); + long eowner = node.stat.getEphemeralOwner(); + if (eowner != 0) { + HashSet list = ephemerals.get(eowner); + if (list == null) { + list = new HashSet(); + ephemerals.put(eowner, list); + } + list.add(path); + } + } + path = ia.readString("path"); + } + nodes.put("/", root); + // we are done with deserializing the + // the datatree + // update the quotas - create path trie + // and also update the stat nodes + setupQuota(); + + 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); + } + + /** + * Write a text dump of all the ephemerals in the datatree. + * @param pwriter the output to write to + */ + public void dumpEphemerals(PrintWriter pwriter) { + Set>> entrySet = ephemerals.entrySet(); + pwriter.println("Sessions with Ephemerals (" + + entrySet.size() + "):"); + for (Map.Entry> entry : entrySet) { + pwriter.print("0x" + Long.toHexString(entry.getKey())); + pwriter.println(":"); + HashSet tmp = entry.getValue(); + if (tmp != null) { + synchronized (tmp) { + for (String path : tmp) { + pwriter.println("\t" + path); + } + } + } + } + } + + public void removeCnxn(Watcher watcher) { + dataWatches.removeWatcher(watcher); + childWatches.removeWatcher(watcher); + } + + public void clear() { + root = null; + nodes.clear(); + ephemerals.clear(); + } + + public void setWatches(long relativeZxid, List dataWatches, + List existWatches, List childWatches, + Watcher watcher) { + for (String path : dataWatches) { + DataNode node = getNode(path); + 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); + } + } + } + + /** + * 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()) { + node.stat.setCversion(newCversion); + node.stat.setPzxid(zxid); + } + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/DataTreeBean.java b/src/java/main/org/apache/zookeeper/server/DataTreeBean.java new file mode 100644 index 0000000..433c13f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/DataTreeBean.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 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.approximateDataSize(); + } + + 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/src/java/main/org/apache/zookeeper/server/DataTreeMXBean.java b/src/java/main/org/apache/zookeeper/server/DataTreeMXBean.java new file mode 100644 index 0000000..0048b6b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/DataTreeMXBean.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; + +/** + * Zookeeper data tree MBean. + */ +public interface DataTreeMXBean { + /** + * @return number of znodes in the data tree. + */ + public int getNodeCount(); + /** + * @return the most recent zxid processed by the data tree. + */ + public String getLastZxid(); + /** + * @return number of watches set. + */ + public int getWatchCount(); + + /** + * @return data tree size in bytes. The size includes the znode path and + * its value. + */ + public long approximateDataSize(); + /** + * @return number of ephemeral nodes in the data tree + */ + public int countEphemerals(); +} diff --git a/src/java/main/org/apache/zookeeper/server/DatadirCleanupManager.java b/src/java/main/org/apache/zookeeper/server/DatadirCleanupManager.java new file mode 100644 index 0000000..e8dbeb7 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/DatadirCleanupManager.java @@ -0,0 +1,192 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 String snapDir; + + private final String 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(String snapDir, String 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; + } + + timer = new Timer("PurgeTask", true); + TimerTask task = new PurgeTask(dataLogDir, snapDir, snapRetainCount); + timer.scheduleAtFixedRate(task, 0, TimeUnit.HOURS.toMillis(purgeInterval)); + + 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 String logsDir; + private String snapsDir; + private int snapRetainCount; + + public PurgeTask(String dataDir, String snapDir, int count) { + logsDir = dataDir; + snapsDir = snapDir; + snapRetainCount = count; + } + + @Override + public void run() { + LOG.info("Purge task started."); + try { + PurgeTxnLog.purge(new File(logsDir), new File(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 String getSnapDir() { + return snapDir; + } + + /** + * Returns transaction log directory. + * + * @return the transaction log directory. + */ + public String 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/src/java/main/org/apache/zookeeper/server/ExitCode.java b/src/java/main/org/apache/zookeeper/server/ExitCode.java new file mode 100644 index 0000000..02d96cb --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ExitCode.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.server; + +/** + * Exit code used to exit server + */ +public class ExitCode { + + /* Represents unexpected error */ + public final static int UNEXPECTED_ERROR = 1; +} diff --git a/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java new file mode 100644 index 0000000..65f7ac0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java @@ -0,0 +1,406 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.List; + +import org.apache.jute.Record; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.MultiResponse; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.CreateResponse; +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.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.ReplyHeader; +import org.apache.zookeeper.proto.SetACLResponse; +import org.apache.zookeeper.proto.SetDataResponse; +import org.apache.zookeeper.proto.SetWatches; +import org.apache.zookeeper.proto.SyncRequest; +import org.apache.zookeeper.proto.SyncResponse; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.TxnHeader; + +import org.apache.zookeeper.MultiTransactionRecord; +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.SetDataResult; +import org.apache.zookeeper.OpResult.ErrorResult; + +/** + * 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); + + ZooKeeperServer zks; + + public FinalRequestProcessor(ZooKeeperServer zks) { + this.zks = zks; + } + + public void processRequest(Request request) { + if (LOG.isDebugEnabled()) { + 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, ""); + } + ProcessTxnResult rc = null; + synchronized (zks.outstandingChanges) { + while (!zks.outstandingChanges.isEmpty() + && zks.outstandingChanges.get(0).zxid <= request.zxid) { + ChangeRecord cr = zks.outstandingChanges.remove(0); + if (cr.zxid < request.zxid) { + LOG.warn("Zxid outstanding " + + cr.zxid + + " is less than current " + request.zxid); + } + if (zks.outstandingChangesForPath.get(cr.path) == cr) { + zks.outstandingChangesForPath.remove(cr.path); + } + } + if (request.hdr != null) { + TxnHeader hdr = request.hdr; + Record txn = request.txn; + + rc = zks.processTxn(hdr, txn); + } + // do not add non quorum packets to the queue. + if (Request.isQuorum(request.type)) { + zks.getZKDatabase().addCommittedProposal(request); + } + } + + if (request.hdr != null && request.hdr.getType() == OpCode.closeSession) { + ServerCnxnFactory scxn = zks.getServerCnxnFactory(); + // this might be possible since + // we might just be playing diffs from the leader + if (scxn != null && request.cnxn == null) { + // calling this if we have the cnxn results in the client's + // close session response being lost - we've already closed + // the session/socket here before we can send the closeSession + // in the switch block below + scxn.closeSession(request.sessionId); + return; + } + } + + if (request.cnxn == null) { + return; + } + ServerCnxn cnxn = request.cnxn; + + String lastOp = "NA"; + zks.decInProcess(); + Code err = Code.OK; + Record rsp = null; + boolean closeSession = false; + try { + if (request.hdr != null && request.hdr.getType() == OpCode.error) { + throw KeeperException.create(KeeperException.Code.get(( + (ErrorTxn) request.txn).getErr())); + } + + KeeperException ke = request.getException(); + if (ke != null && request.type != OpCode.multi) { + throw ke; + } + + if (LOG.isDebugEnabled()) { + LOG.debug("{}",request); + } + switch (request.type) { + case OpCode.ping: { + zks.serverStats().updateLatency(request.createTime); + + lastOp = "PING"; + cnxn.updateStatsForResponse(request.cxid, request.zxid, lastOp, + request.createTime, Time.currentElapsedTime()); + + cnxn.sendResponse(new ReplyHeader(-2, + zks.getZKDatabase().getDataTreeLastProcessedZxid(), 0), null, "response"); + return; + } + case OpCode.createSession: { + zks.serverStats().updateLatency(request.createTime); + + lastOp = "SESS"; + cnxn.updateStatsForResponse(request.cxid, request.zxid, lastOp, + request.createTime, Time.currentElapsedTime()); + + zks.finishSessionInit(request.cnxn, true); + return; + } + case OpCode.multi: { + lastOp = "MULT"; + rsp = new MultiResponse() ; + + 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.delete: + subResult = new DeleteResult(); + break; + case OpCode.setData: + subResult = new SetDataResult(subTxnResult.stat); + break; + case OpCode.error: + subResult = new ErrorResult(subTxnResult.err) ; + break; + default: + throw new IOException("Invalid type of op"); + } + + ((MultiResponse)rsp).add(subResult); + } + + break; + } + case OpCode.create: { + lastOp = "CREA"; + rsp = new CreateResponse(rc.path); + err = Code.get(rc.err); + break; + } + case OpCode.delete: { + lastOp = "DELE"; + err = Code.get(rc.err); + break; + } + case OpCode.setData: { + lastOp = "SETD"; + rsp = new SetDataResponse(rc.stat); + err = Code.get(rc.err); + break; + } + case OpCode.setACL: { + lastOp = "SETA"; + rsp = new SetACLResponse(rc.stat); + err = Code.get(rc.err); + break; + } + case OpCode.closeSession: { + lastOp = "CLOS"; + closeSession = true; + 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()); + 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); + String path = existsRequest.getPath(); + if (path.indexOf('\0') != -1) { + throw new KeeperException.BadArgumentsException(); + } + Stat stat = zks.getZKDatabase().statNode(path, existsRequest + .getWatch() ? cnxn : null); + rsp = new ExistsResponse(stat); + break; + } + case OpCode.getData: { + lastOp = "GETD"; + GetDataRequest getDataRequest = new GetDataRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, + getDataRequest); + DataNode n = zks.getZKDatabase().getNode(getDataRequest.getPath()); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + PrepRequestProcessor.checkACL(zks, zks.getZKDatabase().aclForNode(n), + ZooDefs.Perms.READ, + request.authInfo); + Stat stat = new Stat(); + byte b[] = zks.getZKDatabase().getData(getDataRequest.getPath(), stat, + getDataRequest.getWatch() ? cnxn : null); + rsp = new GetDataResponse(b, stat); + break; + } + case OpCode.setWatches: { + lastOp = "SETW"; + SetWatches setWatches = new SetWatches(); + // XXX 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(), cnxn); + break; + } + case OpCode.getACL: { + lastOp = "GETA"; + GetACLRequest getACLRequest = new GetACLRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, + getACLRequest); + Stat stat = new Stat(); + List acl = + zks.getZKDatabase().getACL(getACLRequest.getPath(), stat); + rsp = new GetACLResponse(acl, stat); + break; + } + case OpCode.getChildren: { + lastOp = "GETC"; + GetChildrenRequest getChildrenRequest = new GetChildrenRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, + getChildrenRequest); + DataNode n = zks.getZKDatabase().getNode(getChildrenRequest.getPath()); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + PrepRequestProcessor.checkACL(zks, zks.getZKDatabase().aclForNode(n), + ZooDefs.Perms.READ, + request.authInfo); + List children = zks.getZKDatabase().getChildren( + getChildrenRequest.getPath(), null, getChildrenRequest + .getWatch() ? cnxn : null); + rsp = new GetChildrenResponse(children); + break; + } + case OpCode.getChildren2: { + lastOp = "GETC"; + GetChildren2Request getChildren2Request = new GetChildren2Request(); + ByteBufferInputStream.byteBuffer2Record(request.request, + getChildren2Request); + Stat stat = new Stat(); + DataNode n = zks.getZKDatabase().getNode(getChildren2Request.getPath()); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + PrepRequestProcessor.checkACL(zks, zks.getZKDatabase().aclForNode(n), + ZooDefs.Perms.READ, + request.authInfo); + List children = zks.getZKDatabase().getChildren( + getChildren2Request.getPath(), stat, getChildren2Request + .getWatch() ? cnxn : null); + rsp = new GetChildren2Response(children, stat); + 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; + } + + long lastZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + ReplyHeader hdr = + new ReplyHeader(request.cxid, lastZxid, err.intValue()); + + zks.serverStats().updateLatency(request.createTime); + cnxn.updateStatsForResponse(request.cxid, lastZxid, lastOp, + request.createTime, Time.currentElapsedTime()); + + try { + cnxn.sendResponse(hdr, rsp, "response"); + if (closeSession) { + cnxn.sendCloseSession(); + } + } catch (IOException e) { + LOG.error("FIXMSG",e); + } + } + + public void shutdown() { + // we are the final link in the chain + LOG.info("shutdown of request processor complete"); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/LogFormatter.java b/src/java/main/org/apache/zookeeper/server/LogFormatter.java new file mode 100644 index 0000000..c5274e5 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/LogFormatter.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; + +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 org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +@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"); + System.exit(2); + } + 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]); + System.exit(2); + } + System.out.println("ZooKeeper Transactional Log File with dbid " + + fhdr.getDbid() + " txnlog format version " + + fhdr.getVersion()); + + 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()); + } + TxnHeader hdr = new TxnHeader(); + Record txn = SerializeUtils.deserializeTxn(bytes, hdr); + 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()) + + " " + TraceFormatter.op2String(hdr.getType()) + " " + txn); + if (logStream.readByte("EOR") != 'B') { + LOG.error("Last transaction was partial."); + throw new EOFException("Last transaction was partial."); + } + count++; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/NIOServerCnxn.java b/src/java/main/org/apache/zookeeper/server/NIOServerCnxn.java new file mode 100644 index 0000000..24cc7b4 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/NIOServerCnxn.java @@ -0,0 +1,1218 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.ByteArrayOutputStream; +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.util.HashSet; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.quorum.ProposalStats; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.WatcherEvent; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.apache.zookeeper.server.util.OSMXBean; + +/** + * 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 { + static final Logger LOG = LoggerFactory.getLogger(NIOServerCnxn.class); + + NIOServerCnxnFactory factory; + + final SocketChannel sock; + + protected final SelectionKey sk; + + boolean initialized; + + ByteBuffer lenBuffer = ByteBuffer.allocate(4); + + ByteBuffer incomingBuffer = lenBuffer; + + LinkedBlockingQueue outgoingBuffers = new LinkedBlockingQueue(); + + int sessionTimeout; + + protected final ZooKeeperServer zkServer; + + /** + * The number of requests that have been submitted but not yet responded to. + */ + int outstandingRequests; + + /** + * 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. + */ + long sessionId; + + static long nextSessionId = 1; + int outstandingLimit = 1; + + public NIOServerCnxn(ZooKeeperServer zk, SocketChannel sock, + SelectionKey sk, NIOServerCnxnFactory factory) throws IOException { + this.zkServer = zk; + this.sock = sock; + this.sk = sk; + this.factory = factory; + if (this.factory.login != null) { + this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login); + } + if (zk != null) { + outstandingLimit = zk.getGlobalOutstandingLimit(); + } + 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(); + authInfo.add(new Id("ip", addr.getHostAddress())); + sk.interestOps(SelectionKey.OP_READ); + } + + /* 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 + */ + sock.configureBlocking(true); + if (bb != ServerCnxnFactory.closeConn) { + if (sock.isOpen()) { + sock.write(bb); + } + packetSent(); + } + } catch (IOException ie) { + LOG.error("Error sending data synchronously ", ie); + } + } + + public void sendBuffer(ByteBuffer bb) { + try { + internalSendBuffer(bb); + } catch(Exception e) { + LOG.error("Unexpected Exception: ", e); + } + } + + /** + * This method implements the internals of sendBuffer. We + * have separated it from send buffer to be able to catch + * exceptions when testing. + * + * @param bb Buffer to send. + */ + protected void internalSendBuffer(ByteBuffer bb) { + if (bb != ServerCnxnFactory.closeConn) { + // We check if write interest here because if it is NOT set, + // nothing is queued, so we can try to send the buffer right + // away without waking up the selector + if(sk.isValid() && + ((sk.interestOps() & SelectionKey.OP_WRITE) == 0)) { + try { + sock.write(bb); + } catch (IOException e) { + // we are just doing best effort right now + } + } + // if there is nothing left to send, we are done + if (bb.remaining() == 0) { + packetSent(); + return; + } + } + + synchronized(this.factory){ + sk.selector().wakeup(); + if (LOG.isTraceEnabled()) { + LOG.trace("Add a buffer to outgoingBuffers, sk " + sk + + " is valid: " + sk.isValid()); + } + outgoingBuffers.add(bb); + if (sk.isValid()) { + sk.interestOps(sk.interestOps() | SelectionKey.OP_WRITE); + } + } + } + + /** Read the request payload (everything following the length prefix) */ + private void readPayload() throws IOException, InterruptedException { + if (incomingBuffer.remaining() != 0) { // have we read length bytes? + int rc = sock.read(incomingBuffer); // sock is non-blocking, so ok + if (rc < 0) { + throw new EndOfStreamException( + "Unable to read additional data from client sessionid 0x" + + Long.toHexString(sessionId) + + ", likely client has closed socket"); + } + } + + if (incomingBuffer.remaining() == 0) { // have we read length bytes? + packetReceived(); + incomingBuffer.flip(); + if (!initialized) { + readConnectRequest(); + } else { + readRequest(); + } + lenBuffer.clear(); + incomingBuffer = lenBuffer; + } + } + + /** + * Only used in order to allow testing + */ + protected boolean isSocketOpen() { + return sock.isOpen(); + } + + @Override + public InetAddress getSocketAddress() { + if (sock == null) { + return null; + } + + return sock.socket().getInetAddress(); + } + + /** + * Handles read/write IO on connection. + */ + void doIO(SelectionKey k) throws InterruptedException { + try { + if (isSocketOpen() == false) { + LOG.warn("trying to do i/o on a null socket for session:0x" + + Long.toHexString(sessionId)); + + return; + } + if (k.isReadable()) { + int rc = sock.read(incomingBuffer); + if (rc < 0) { + throw new EndOfStreamException( + "Unable to read additional data from client sessionid 0x" + + Long.toHexString(sessionId) + + ", likely client has closed socket"); + } + if (incomingBuffer.remaining() == 0) { + boolean isPayload; + if (incomingBuffer == lenBuffer) { // start of next request + incomingBuffer.flip(); + isPayload = readLength(k); + incomingBuffer.clear(); + } else { + // continuation + isPayload = true; + } + if (isPayload) { // not the case for 4letterword + readPayload(); + } + else { + // four letter words take care + // need not do anything else + return; + } + } + } + if (k.isWritable()) { + // ZooLog.logTraceMessage(LOG, + // ZooLog.CLIENT_DATA_PACKET_TRACE_MASK + // "outgoingBuffers.size() = " + + // outgoingBuffers.size()); + if (outgoingBuffers.size() > 0) { + // ZooLog.logTraceMessage(LOG, + // ZooLog.CLIENT_DATA_PACKET_TRACE_MASK, + // "sk " + k + " is valid: " + + // k.isValid()); + + /* + * 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 = factory.directBuffer; + 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(); + + int sent = sock.write(directBuffer); + ByteBuffer bb; + + // Remove the buffers that we have sent + while (outgoingBuffers.size() > 0) { + bb = outgoingBuffers.peek(); + if (bb == ServerCnxnFactory.closeConn) { + throw new CloseRequestException("close requested"); + } + int left = bb.remaining() - sent; + if (left > 0) { + /* + * We only partially sent this buffer, so we update + * the position and exit the loop. + */ + bb.position(bb.position() + sent); + break; + } + packetSent(); + /* We've sent the whole buffer, so drop the buffer */ + sent -= bb.remaining(); + outgoingBuffers.remove(); + } + // ZooLog.logTraceMessage(LOG, + // ZooLog.CLIENT_DATA_PACKET_TRACE_MASK, "after send, + // outgoingBuffers.size() = " + outgoingBuffers.size()); + } + + synchronized(this.factory){ + if (outgoingBuffers.size() == 0) { + if (!initialized + && (sk.interestOps() & SelectionKey.OP_READ) == 0) { + throw new CloseRequestException("responded to info probe"); + } + sk.interestOps(sk.interestOps() + & (~SelectionKey.OP_WRITE)); + } else { + sk.interestOps(sk.interestOps() + | SelectionKey.OP_WRITE); + } + } + } + } catch (CancelledKeyException e) { + LOG.warn("CancelledKeyException causing close of session 0x" + + Long.toHexString(sessionId)); + if (LOG.isDebugEnabled()) { + LOG.debug("CancelledKeyException stack trace", e); + } + close(); + } catch (CloseRequestException e) { + // expecting close to log session closure + close(); + } catch (EndOfStreamException e) { + LOG.warn(e.getMessage()); + if (LOG.isDebugEnabled()) { + LOG.debug("EndOfStreamException stack trace", e); + } + // expecting close to log session closure + close(); + } catch (IOException e) { + LOG.warn("Exception causing close of session 0x" + + Long.toHexString(sessionId) + ": " + e.getMessage()); + if (LOG.isDebugEnabled()) { + LOG.debug("IOException stack trace", e); + } + close(); + } + } + + private void readRequest() throws IOException { + zkServer.processPacket(this, incomingBuffer); + } + + protected void incrOutstandingRequests(RequestHeader h) { + if (h.getXid() >= 0) { + synchronized (this) { + outstandingRequests++; + } + synchronized (this.factory) { + // check throttling + if (zkServer.getInProcess() > outstandingLimit) { + if (LOG.isDebugEnabled()) { + LOG.debug("Throttling recv " + zkServer.getInProcess()); + } + disableRecv(); + // following lines should not be needed since we are + // already reading + // } else { + // enableRecv(); + } + } + } + + } + + public void disableRecv() { + sk.interestOps(sk.interestOps() & (~SelectionKey.OP_READ)); + } + + public void enableRecv() { + synchronized (this.factory) { + sk.selector().wakeup(); + if (sk.isValid()) { + int interest = sk.interestOps(); + if ((interest & SelectionKey.OP_READ) == 0) { + sk.interestOps(interest | SelectionKey.OP_READ); + } + } + } + } + + private void readConnectRequest() throws IOException, InterruptedException { + if (!isZKServerRunning()) { + throw new IOException("ZooKeeperServer not running"); + } + zkServer.processConnectRequest(this, incomingBuffer); + initialized = true; + } + + /** + * 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 + */ + private void cleanupWriterSocket(PrintWriter pwriter) { + try { + if (pwriter != null) { + pwriter.flush(); + pwriter.close(); + } + } catch (Exception e) { + LOG.info("Error closing PrintWriter ", e); + } finally { + try { + close(); + } catch (Exception e) { + LOG.error("Error closing a command socket ", e); + } + } + } + + /** + * 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); + } + } + + private static final String ZK_NOT_SERVING = + "This ZooKeeper instance is not currently serving requests"; + + /** + * Set of threads for commmand 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. + */ + private abstract class CommandThread extends Thread { + PrintWriter pw; + + CommandThread(PrintWriter pw) { + this.pw = pw; + } + + public void run() { + try { + commandRun(); + } catch (IOException ie) { + LOG.error("Error in running command ", ie); + } finally { + cleanupWriterSocket(pw); + } + } + + public abstract void commandRun() throws IOException; + } + + private class RuokCommand extends CommandThread { + public RuokCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + pw.print("imok"); + + } + } + + private class TraceMaskCommand extends CommandThread { + TraceMaskCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + long traceMask = ZooTrace.getTextTraceLevel(); + pw.print(traceMask); + } + } + + private class SetTraceMaskCommand extends CommandThread { + long trace = 0; + SetTraceMaskCommand(PrintWriter pw, long trace) { + super(pw); + this.trace = trace; + } + + @Override + public void commandRun() { + pw.print(trace); + } + } + + private class EnvCommand extends CommandThread { + EnvCommand(PrintWriter pw) { + super(pw); + } + + @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()); + } + + } + } + + private class ConfCommand extends CommandThread { + ConfCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + zkServer.dumpConf(pw); + } + } + } + + private class StatResetCommand extends CommandThread { + public StatResetCommand(PrintWriter pw) { + super(pw); + } + + @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."); + } + } + } + + private class CnxnStatResetCommand extends CommandThread { + public CnxnStatResetCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + synchronized(factory.cnxns){ + for(ServerCnxn c : factory.cnxns){ + c.resetStats(); + } + } + pw.println("Connection stats reset."); + } + } + } + + private class DumpCommand extends CommandThread { + public DumpCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } + else { + pw.println("SessionTracker dump:"); + zkServer.sessionTracker.dumpSessions(pw); + pw.println("ephemeral nodes dump:"); + zkServer.dumpEphemerals(pw); + } + } + } + + private class StatCommand extends CommandThread { + int len; + public StatCommand(PrintWriter pw, int len) { + super(pw); + this.len = len; + } + + @SuppressWarnings("unchecked") + @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 == statCmd) { + LOG.info("Stat command output"); + pw.println("Clients:"); + // clone should be faster than iteration + // ie give up the cnxns lock faster + HashSet cnxnset; + synchronized(factory.cnxns){ + cnxnset = (HashSet)factory + .cnxns.clone(); + } + for(NIOServerCnxn c : cnxnset){ + 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(); + ProposalStats proposalStats = leader.getProposalStats(); + pw.printf("Proposal sizes last/min/max: %s%n", proposalStats.toString()); + } + } + + } + } + + private class ConsCommand extends CommandThread { + public ConsCommand(PrintWriter pw) { + super(pw); + } + + @SuppressWarnings("unchecked") + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + // clone should be faster than iteration + // ie give up the cnxns lock faster + HashSet cnxns; + synchronized (factory.cnxns) { + cnxns = (HashSet) factory.cnxns.clone(); + } + for (NIOServerCnxn c : cnxns) { + c.dumpConnectionInfo(pw, false); + pw.println(); + } + pw.println(); + } + } + } + + private class WatchCommand extends CommandThread { + int len = 0; + public WatchCommand(PrintWriter pw, int len) { + super(pw); + this.len = len; + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + if (len == wchsCmd) { + dt.dumpWatchesSummary(pw); + } else if (len == wchpCmd) { + dt.dumpWatches(pw, true); + } else { + dt.dumpWatches(pw, false); + } + pw.println(); + } + } + } + + private class MonitorCommand extends CommandThread { + + MonitorCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if(!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + return; + } + ZKDatabase zkdb = zkServer.getZKDatabase(); + ServerStats stats = zkServer.serverStats(); + + print("version", Version.getFullVersion()); + + print("avg_latency", stats.getAvgLatency()); + print("max_latency", stats.getMaxLatency()); + print("min_latency", stats.getMinLatency()); + + print("packets_received", stats.getPacketsReceived()); + print("packets_sent", stats.getPacketsSent()); + print("num_alive_connections", stats.getNumAliveClientConnections()); + + print("outstanding_requests", stats.getOutstandingRequests()); + + print("server_state", stats.getServerState()); + print("znode_count", zkdb.getNodeCount()); + + print("watch_count", zkdb.getDataTree().getWatchCount()); + print("ephemerals_count", zkdb.getDataTree().getEphemeralsCount()); + print("approximate_data_size", zkdb.getDataTree().approximateDataSize()); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean != null && osMbean.getUnix() == true) { + print("open_file_descriptor_count", osMbean.getOpenFileDescriptorCount()); + print("max_file_descriptor_count", osMbean.getMaxFileDescriptorCount()); + } + + print("fsync_threshold_exceed_count", stats.getFsyncThresholdExceedCount()); + + if(stats.getServerState().equals("leader")) { + Leader leader = ((LeaderZooKeeperServer)zkServer).getLeader(); + + print("followers", leader.getLearners().size()); + print("synced_followers", leader.getForwardingFollowers().size()); + print("pending_syncs", leader.getNumPendingSyncs()); + + print("last_proposal_size", leader.getProposalStats().getLastProposalSize()); + print("max_proposal_size", leader.getProposalStats().getMaxProposalSize()); + print("min_proposal_size", leader.getProposalStats().getMinProposalSize()); + } + } + + private void print(String key, long number) { + print(key, "" + number); + } + + private void print(String key, String value) { + pw.print("zk_"); + pw.print(key); + pw.print("\t"); + pw.println(value); + } + + } + + private class IsroCommand extends CommandThread { + + public IsroCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.print("null"); + } else if (zkServer instanceof ReadOnlyZooKeeperServer) { + pw.print("ro"); + } else { + pw.print("rw"); + } + } + } + + private class NopCommand extends CommandThread { + private String msg; + + public NopCommand(PrintWriter pw, String msg) { + super(pw); + this.msg = msg; + } + + @Override + public void commandRun() { + pw.println(msg); + } + } + + /** 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 (!ServerCnxn.isKnown(len)) { + return false; + } + + packetReceived(); + + /** 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())); + + String cmd = ServerCnxn.getCommandString(len); + // ZOOKEEPER-2693: don't execute 4lw if it's not enabled. + if (!ServerCnxn.isEnabled(cmd)) { + LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd); + NopCommand nopCmd = new NopCommand(pwriter, cmd + " is not executed because it is not in the whitelist."); + nopCmd.start(); + return true; + } + + LOG.info("Processing " + cmd + " command from " + + sock.socket().getRemoteSocketAddress()); + + if (len == ruokCmd) { + RuokCommand ruok = new RuokCommand(pwriter); + ruok.start(); + return true; + } else if (len == getTraceMaskCmd) { + TraceMaskCommand tmask = new TraceMaskCommand(pwriter); + tmask.start(); + return true; + } else if (len == 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, traceMask); + setMask.start(); + return true; + } else if (len == enviCmd) { + EnvCommand env = new EnvCommand(pwriter); + env.start(); + return true; + } else if (len == confCmd) { + ConfCommand ccmd = new ConfCommand(pwriter); + ccmd.start(); + return true; + } else if (len == srstCmd) { + StatResetCommand strst = new StatResetCommand(pwriter); + strst.start(); + return true; + } else if (len == crstCmd) { + CnxnStatResetCommand crst = new CnxnStatResetCommand(pwriter); + crst.start(); + return true; + } else if (len == dumpCmd) { + DumpCommand dump = new DumpCommand(pwriter); + dump.start(); + return true; + } else if (len == statCmd || len == srvrCmd) { + StatCommand stat = new StatCommand(pwriter, len); + stat.start(); + return true; + } else if (len == consCmd) { + ConsCommand cons = new ConsCommand(pwriter); + cons.start(); + return true; + } else if (len == wchpCmd || len == wchcCmd || len == wchsCmd) { + WatchCommand wcmd = new WatchCommand(pwriter, len); + wcmd.start(); + return true; + } else if (len == mntrCmd) { + MonitorCommand mntr = new MonitorCommand(pwriter); + mntr.start(); + return true; + } else if (len == isroCmd) { + IsroCommand isro = new IsroCommand(pwriter); + isro.start(); + return true; + } + return false; + } + + /** 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"); + } + incomingBuffer = ByteBuffer.allocate(len); + return true; + } + + public long getOutstandingRequests() { + synchronized (this) { + synchronized (this.factory) { + return outstandingRequests; + } + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#getSessionTimeout() + */ + public int getSessionTimeout() { + return sessionTimeout; + } + + @Override + public String toString() { + return "NIOServerCnxn object with sock = " + sock + " and sk = " + sk; + } + + /* + * Close the cnxn and remove it from the factory cnxns list. + * + * This function returns immediately if the cnxn is not on the cnxns list. + */ + @Override + public void close() { + factory.removeCnxn(this); + + if (zkServer != null) { + zkServer.removeCnxn(this); + } + + closeSock(); + + if (sk != null) { + try { + // need to cancel this selection key from the selector + sk.cancel(); + } catch (Exception e) { + if (LOG.isDebugEnabled()) { + LOG.debug("ignoring exception during selectionkey cancel", e); + } + } + } + } + + /** + * Close resources associated with the sock of this cnxn. + */ + private void closeSock() { + if (sock.isOpen() == false) { + return; + } + + LOG.info("Closed socket connection for client " + + sock.socket().getRemoteSocketAddress() + + (sessionId != 0 ? + " which had sessionid 0x" + Long.toHexString(sessionId) : + " (no session established for client)")); + 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 + if (LOG.isDebugEnabled()) { + 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 + if (LOG.isDebugEnabled()) { + LOG.debug("ignoring exception during input shutdown", e); + } + } + try { + sock.socket().close(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("ignoring exception during socket close", e); + } + } + try { + sock.close(); + // XXX The next line doesn't seem to be needed, but some posts + // to forums suggest that it is needed. Keep in mind if errors in + // this section arise. + // factory.selector.wakeup(); + } catch (IOException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("ignoring exception during socketchannel close", e); + } + } + } + + private final static byte fourBytes[] = new byte[4]; + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#sendResponse(org.apache.zookeeper.proto.ReplyHeader, + * org.apache.jute.Record, java.lang.String) + */ + @Override + synchronized public void sendResponse(ReplyHeader h, Record r, String tag) { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // Make space for length + BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); + try { + baos.write(fourBytes); + bos.writeRecord(h, "header"); + if (r != null) { + bos.writeRecord(r, tag); + } + baos.close(); + } catch (IOException e) { + LOG.error("Error serializing response"); + } + byte b[] = baos.toByteArray(); + ByteBuffer bb = ByteBuffer.wrap(b); + bb.putInt(b.length - 4).rewind(); + sendBuffer(bb); + if (h.getXid() > 0) { + synchronized(this){ + outstandingRequests--; + } + // check throttling + synchronized (this.factory) { + if (zkServer.getInProcess() < outstandingLimit + || outstandingRequests < 1) { + sk.selector().wakeup(); + enableRecv(); + } + } + } + } catch(Exception e) { + LOG.warn("Unexpected exception. Destruction averted.", e); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#process(org.apache.zookeeper.proto.WatcherEvent) + */ + @Override + synchronized public void process(WatchedEvent event) { + ReplyHeader h = new ReplyHeader(-1, -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(); + + sendResponse(h, e, "notification"); + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#getSessionId() + */ + @Override + public long getSessionId() { + return sessionId; + } + + @Override + public void setSessionId(long sessionId) { + this.sessionId = sessionId; + this.factory.addSession(sessionId, this); + } + + @Override + public void setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + } + + @Override + public int getInterestOps() { + return sk.isValid() ? sk.interestOps() : 0; + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + if (sock.isOpen() == false) { + return null; + } + return (InetSocketAddress) sock.socket().getRemoteSocketAddress(); + } + + @Override + protected ServerStats serverStats() { + if (!isZKServerRunning()) { + return null; + } + return zkServer.serverStats(); + } + + /** + * @return true if the server is running, false otherwise. + */ + boolean isZKServerRunning() { + return zkServer != null && zkServer.isRunning(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/NIOServerCnxnFactory.java b/src/java/main/org/apache/zookeeper/server/NIOServerCnxnFactory.java new file mode 100644 index 0000000..d7581a4 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/NIOServerCnxnFactory.java @@ -0,0 +1,320 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.InetAddress; +import java.net.InetSocketAddress; +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.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NIOServerCnxnFactory extends ServerCnxnFactory implements Runnable { + private static final Logger LOG = LoggerFactory.getLogger(NIOServerCnxnFactory.class); + + static { + /** + * 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); + } + } + + ServerSocketChannel ss; + + final Selector selector = Selector.open(); + + /** + * We use this buffer to do efficient socket I/O. Since there is a single + * sender thread per NIOServerCnxn instance, we can use a member variable to + * only allocate it once. + */ + final ByteBuffer directBuffer = ByteBuffer.allocateDirect(64 * 1024); + + final HashMap> ipMap = + new HashMap>( ); + + int maxClientCnxns = 60; + + /** + * 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. + * @throws IOException + */ + public NIOServerCnxnFactory() throws IOException { + } + + Thread thread; + @Override + public void configure(InetSocketAddress addr, int maxcc) throws IOException { + configureSaslLogin(); + + thread = new ZooKeeperThread(this, "NIOServerCxn.Factory:" + addr); + thread.setDaemon(true); + maxClientCnxns = maxcc; + this.ss = ServerSocketChannel.open(); + ss.socket().setReuseAddress(true); + LOG.info("binding to port " + addr); + ss.socket().bind(addr); + ss.configureBlocking(false); + ss.register(selector, SelectionKey.OP_ACCEPT); + } + + /** {@inheritDoc} */ + public int getMaxClientCnxnsPerHost() { + return maxClientCnxns; + } + + /** {@inheritDoc} */ + public void setMaxClientCnxnsPerHost(int max) { + maxClientCnxns = max; + } + + @Override + public void start() { + // ensure thread is started once and only once + if (thread.getState() == Thread.State.NEW) { + thread.start(); + } + } + + @Override + public void startup(ZooKeeperServer zks) throws IOException, + InterruptedException { + start(); + setZooKeeperServer(zks); + zks.startdata(); + zks.startup(); + } + + @Override + public InetSocketAddress getLocalAddress(){ + return (InetSocketAddress)ss.socket().getLocalSocketAddress(); + } + + @Override + public int getLocalPort(){ + return ss.socket().getLocalPort(); + } + + private void addCnxn(NIOServerCnxn cnxn) { + synchronized (cnxns) { + cnxns.add(cnxn); + synchronized (ipMap){ + InetAddress addr = cnxn.sock.socket().getInetAddress(); + Set s = ipMap.get(addr); + if (s == 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 + s = new HashSet(2); + s.add(cnxn); + ipMap.put(addr,s); + } else { + s.add(cnxn); + } + } + } + } + + public void removeCnxn(NIOServerCnxn cnxn) { + synchronized(cnxns) { + // Remove the related session from the sessionMap. + long sessionId = cnxn.getSessionId(); + if (sessionId != 0) { + sessionMap.remove(sessionId); + } + + // if this is not in cnxns then it's already closed + if (!cnxns.remove(cnxn)) { + return; + } + + synchronized (ipMap) { + Set s = + ipMap.get(cnxn.getSocketAddress()); + s.remove(cnxn); + } + + unregisterConnection(cnxn); + } + } + + protected NIOServerCnxn createConnection(SocketChannel sock, + SelectionKey sk) throws IOException { + return new NIOServerCnxn(zkServer, sock, sk, this); + } + + private int getClientCnxnCount(InetAddress cl) { + // The ipMap lock covers both the map, and its contents + // (that is, the cnxn sets shouldn't be modified outside of + // this lock) + synchronized (ipMap) { + Set s = ipMap.get(cl); + if (s == null) return 0; + return s.size(); + } + } + + public void run() { + while (!ss.socket().isClosed()) { + try { + selector.select(1000); + Set selected; + synchronized (this) { + selected = selector.selectedKeys(); + } + ArrayList selectedList = new ArrayList( + selected); + Collections.shuffle(selectedList); + for (SelectionKey k : selectedList) { + if ((k.readyOps() & SelectionKey.OP_ACCEPT) != 0) { + SocketChannel sc = ((ServerSocketChannel) k + .channel()).accept(); + InetAddress ia = sc.socket().getInetAddress(); + int cnxncount = getClientCnxnCount(ia); + if (maxClientCnxns > 0 && cnxncount >= maxClientCnxns){ + LOG.warn("Too many connections from " + ia + + " - max is " + maxClientCnxns ); + sc.close(); + } else { + LOG.info("Accepted socket connection from " + + sc.socket().getRemoteSocketAddress()); + sc.configureBlocking(false); + SelectionKey sk = sc.register(selector, + SelectionKey.OP_READ); + NIOServerCnxn cnxn = createConnection(sc, sk); + sk.attach(cnxn); + addCnxn(cnxn); + } + } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { + NIOServerCnxn c = (NIOServerCnxn) k.attachment(); + c.doIO(k); + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Unexpected ops in select " + + k.readyOps()); + } + } + } + selected.clear(); + } catch (RuntimeException e) { + LOG.warn("Ignoring unexpected runtime exception", e); + } catch (Exception e) { + LOG.warn("Ignoring exception", e); + } + } + closeAll(); + LOG.info("NIOServerCnxn factory exited run method"); + } + + /** + * clear all the connections in the selector + * + */ + @Override + @SuppressWarnings("unchecked") + synchronized public void closeAll() { + selector.wakeup(); + HashSet cnxns; + synchronized (this.cnxns) { + cnxns = (HashSet)this.cnxns.clone(); + } + // got to clear all the connections that we have in the selector + for (NIOServerCnxn cnxn: cnxns) { + try { + // don't hold this.cnxns lock as deadlock may occur + cnxn.close(); + } catch (Exception e) { + LOG.warn("Ignoring exception closing cnxn sessionid 0x" + + Long.toHexString(cnxn.sessionId), e); + } + } + } + + public void shutdown() { + try { + ss.close(); + closeAll(); + thread.interrupt(); + thread.join(); + 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); + } + try { + selector.close(); + } catch (IOException e) { + LOG.warn("Selector closing", e); + } + if (zkServer != null) { + zkServer.shutdown(); + } + } + + @Override + public synchronized void closeSession(long sessionId) { + selector.wakeup(); + closeSessionWithoutWakeup(sessionId); + } + + @SuppressWarnings("unchecked") + private void closeSessionWithoutWakeup(long sessionId) { + NIOServerCnxn cnxn = (NIOServerCnxn) sessionMap.remove(sessionId); + if (cnxn != null) { + try { + cnxn.close(); + } catch (Exception e) { + LOG.warn("exception during session close", e); + } + } + } + + @Override + public void join() throws InterruptedException { + thread.join(); + } + + @Override + public Iterable getConnections() { + return cnxns; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/NettyServerCnxn.java b/src/java/main/org/apache/zookeeper/server/NettyServerCnxn.java new file mode 100644 index 0000000..271fc19 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/NettyServerCnxn.java @@ -0,0 +1,897 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.jboss.netty.buffer.ChannelBuffers.wrappedBuffer; + +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.util.AbstractSet; +import java.util.HashSet; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.quorum.ProposalStats; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.WatcherEvent; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.apache.zookeeper.server.util.OSMXBean; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.MessageEvent; + +public class NettyServerCnxn extends ServerCnxn { + Logger LOG = LoggerFactory.getLogger(NettyServerCnxn.class); + Channel channel; + ChannelBuffer queuedBuffer; + volatile boolean throttled; + ByteBuffer bb; + ByteBuffer bbLen = ByteBuffer.allocate(4); + long sessionId; + int sessionTimeout; + 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. + */ + private volatile ZooKeeperServer zkServer; + + NettyServerCnxnFactory factory; + boolean initialized; + + NettyServerCnxn(Channel channel, ZooKeeperServer zks, NettyServerCnxnFactory factory) { + this.channel = channel; + this.zkServer = zks; + this.factory = factory; + if (this.factory.login != null) { + this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login); + } + } + + @Override + public void close() { + if (LOG.isDebugEnabled()) { + LOG.debug("close called for sessionid:0x" + + Long.toHexString(sessionId)); + } + + // ZOOKEEPER-2743: + // Always unregister connection upon close to prevent + // connection bean leak under certain race conditions. + factory.unregisterConnection(this); + + factory.removeCnxn(this); + + if (channel.isOpen()) { + channel.close(); + } + } + + @Override + public long getSessionId() { + return sessionId; + } + + @Override + public int getSessionTimeout() { + return sessionTimeout; + } + + @Override + public void process(WatchedEvent event) { + ReplyHeader h = new ReplyHeader(-1, -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) { + if (LOG.isDebugEnabled()) { + LOG.debug("Problem sending to " + getRemoteSocketAddress(), e1); + } + close(); + } + } + + private static final byte[] fourBytes = new byte[4]; + static class ResumeMessageEvent implements MessageEvent { + Channel channel; + ResumeMessageEvent(Channel channel) { + this.channel = channel; + } + @Override + public Object getMessage() {return null;} + @Override + public SocketAddress getRemoteAddress() {return null;} + @Override + public Channel getChannel() {return channel;} + @Override + public ChannelFuture getFuture() {return null;} + }; + + @Override + public void sendResponse(ReplyHeader h, Record r, String tag) + throws IOException { + if (!channel.isOpen()) { + return; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // Make space for length + BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); + try { + baos.write(fourBytes); + bos.writeRecord(h, "header"); + if (r != null) { + bos.writeRecord(r, tag); + } + baos.close(); + } catch (IOException e) { + LOG.error("Error serializing response"); + } + byte b[] = baos.toByteArray(); + ByteBuffer bb = ByteBuffer.wrap(b); + bb.putInt(b.length - 4).rewind(); + sendBuffer(bb); + if (h.getXid() > 0) { + // zks cannot be null otherwise we would not have gotten here! + if (!zkServer.shouldThrottle(outstandingCount.decrementAndGet())) { + enableRecv(); + } + } + } + + @Override + public void setSessionId(long sessionId) { + this.sessionId = sessionId; + factory.addSession(sessionId, this); + } + + @Override + public void enableRecv() { + if (throttled) { + throttled = false; + if (LOG.isDebugEnabled()) { + LOG.debug("Sending unthrottle event " + this); + } + channel.getPipeline().sendUpstream(new ResumeMessageEvent(channel)); + } + } + + @Override + public void sendBuffer(ByteBuffer sendBuffer) { + if (sendBuffer == ServerCnxnFactory.closeConn) { + close(); + return; + } + channel.write(wrappedBuffer(sendBuffer)); + packetSent(); + } + + @Override + public InetAddress getSocketAddress() { + if (channel == null) { + return null; + } + + return ((InetSocketAddress)(channel.getRemoteAddress())).getAddress(); + } + + /** + * 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 + */ + private void cleanupWriterSocket(PrintWriter pwriter) { + try { + if (pwriter != null) { + pwriter.flush(); + pwriter.close(); + } + } catch (Exception e) { + LOG.info("Error closing PrintWriter ", e); + } finally { + try { + close(); + } catch (Exception e) { + LOG.error("Error closing a command socket ", e); + } + } + } + + /** + * 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); + } + } + + private static final String ZK_NOT_SERVING = + "This ZooKeeper instance is not currently serving requests"; + + /** + * Set of threads for commmand ports. All the 4 + * letter commands are run via a thread. Each class + * maps to a correspoding 4 letter command. CommandThread + * is the abstract class from which all the others inherit. + */ + private abstract class CommandThread /*extends Thread*/ { + PrintWriter pw; + + CommandThread(PrintWriter pw) { + this.pw = pw; + } + + public void start() { + run(); + } + + public void run() { + try { + commandRun(); + } catch (IOException ie) { + LOG.error("Error in running command ", ie); + } finally { + cleanupWriterSocket(pw); + } + } + + public abstract void commandRun() throws IOException; + } + + private class RuokCommand extends CommandThread { + public RuokCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + pw.print("imok"); + + } + } + + private class TraceMaskCommand extends CommandThread { + TraceMaskCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + long traceMask = ZooTrace.getTextTraceLevel(); + pw.print(traceMask); + } + } + + private class SetTraceMaskCommand extends CommandThread { + long trace = 0; + SetTraceMaskCommand(PrintWriter pw, long trace) { + super(pw); + this.trace = trace; + } + + @Override + public void commandRun() { + pw.print(trace); + } + } + + private class EnvCommand extends CommandThread { + EnvCommand(PrintWriter pw) { + super(pw); + } + + @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()); + } + + } + } + + private class ConfCommand extends CommandThread { + ConfCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + zkServer.dumpConf(pw); + } + } + } + + private class StatResetCommand extends CommandThread { + public StatResetCommand(PrintWriter pw) { + super(pw); + } + + @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."); + } + } + } + + private class CnxnStatResetCommand extends CommandThread { + public CnxnStatResetCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + synchronized(factory.cnxns){ + for(ServerCnxn c : factory.cnxns){ + c.resetStats(); + } + } + pw.println("Connection stats reset."); + } + } + } + + private class DumpCommand extends CommandThread { + public DumpCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } + else { + pw.println("SessionTracker dump:"); + zkServer.sessionTracker.dumpSessions(pw); + pw.println("ephemeral nodes dump:"); + zkServer.dumpEphemerals(pw); + } + } + } + + private class StatCommand extends CommandThread { + int len; + public StatCommand(PrintWriter pw, int len) { + super(pw); + 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 == statCmd) { + LOG.info("Stat command output"); + pw.println("Clients:"); + // clone should be faster than iteration + // ie give up the cnxns lock faster + HashSet cnxns; + synchronized(factory.cnxns){ + cnxns = new HashSet(factory.cnxns); + } + for(ServerCnxn c : cnxns){ + 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(); + ProposalStats proposalStats = leader.getProposalStats(); + pw.printf("Proposal sizes last/min/max: %s%n", proposalStats.toString()); + } + } + } + } + + private class ConsCommand extends CommandThread { + public ConsCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + // clone should be faster than iteration + // ie give up the cnxns lock faster + AbstractSet cnxns; + synchronized (factory.cnxns) { + cnxns = new HashSet(factory.cnxns); + } + for (ServerCnxn c : cnxns) { + c.dumpConnectionInfo(pw, false); + pw.println(); + } + pw.println(); + } + } + } + + private class WatchCommand extends CommandThread { + int len = 0; + public WatchCommand(PrintWriter pw, int len) { + super(pw); + this.len = len; + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + if (len == wchsCmd) { + dt.dumpWatchesSummary(pw); + } else if (len == wchpCmd) { + dt.dumpWatches(pw, true); + } else { + dt.dumpWatches(pw, false); + } + pw.println(); + } + } + } + + private class MonitorCommand extends CommandThread { + + MonitorCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if(!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + return; + } + ZKDatabase zkdb = zkServer.getZKDatabase(); + ServerStats stats = zkServer.serverStats(); + + print("version", Version.getFullVersion()); + + print("avg_latency", stats.getAvgLatency()); + print("max_latency", stats.getMaxLatency()); + print("min_latency", stats.getMinLatency()); + + print("packets_received", stats.getPacketsReceived()); + print("packets_sent", stats.getPacketsSent()); + print("num_alive_connections", stats.getNumAliveClientConnections()); + + print("outstanding_requests", stats.getOutstandingRequests()); + + print("server_state", stats.getServerState()); + print("znode_count", zkdb.getNodeCount()); + + print("watch_count", zkdb.getDataTree().getWatchCount()); + print("ephemerals_count", zkdb.getDataTree().getEphemeralsCount()); + print("approximate_data_size", zkdb.getDataTree().approximateDataSize()); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean != null && osMbean.getUnix() == true) { + print("open_file_descriptor_count", osMbean.getOpenFileDescriptorCount()); + print("max_file_descriptor_count", osMbean.getMaxFileDescriptorCount()); + } + + print("fsync_threshold_exceed_count", stats.getFsyncThresholdExceedCount()); + + if(stats.getServerState().equals("leader")) { + Leader leader = ((LeaderZooKeeperServer)zkServer).getLeader(); + + print("followers", leader.getLearners().size()); + print("synced_followers", leader.getForwardingFollowers().size()); + print("pending_syncs", leader.getNumPendingSyncs()); + + print("last_proposal_size", leader.getProposalStats().getLastProposalSize()); + print("max_proposal_size", leader.getProposalStats().getMaxProposalSize()); + print("min_proposal_size", leader.getProposalStats().getMinProposalSize()); + } + } + + private void print(String key, long number) { + print(key, "" + number); + } + + private void print(String key, String value) { + pw.print("zk_"); + pw.print(key); + pw.print("\t"); + pw.println(value); + } + + } + + private class IsroCommand extends CommandThread { + + public IsroCommand(PrintWriter pw) { + super(pw); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.print("null"); + } else if (zkServer instanceof ReadOnlyZooKeeperServer) { + pw.print("ro"); + } else { + pw.print("rw"); + } + } + } + + private class NopCommand extends CommandThread { + private String msg; + + public NopCommand(PrintWriter pw, String msg) { + super(pw); + this.msg = msg; + } + + @Override + public void commandRun() { + pw.println(msg); + } + } + + /** Return if four letter word found and responded to, otw false **/ + private boolean checkFourLetterWord(final Channel channel, + ChannelBuffer message, 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 (!ServerCnxn.isKnown(len)) { + return false; + } + + channel.setInterestOps(0).awaitUninterruptibly(); + packetReceived(); + + final PrintWriter pwriter = new PrintWriter( + new BufferedWriter(new SendBufferWriter())); + + String cmd = ServerCnxn.getCommandString(len); + // ZOOKEEPER-2693: don't execute 4lw if it's not enabled. + if (!ServerCnxn.isEnabled(cmd)) { + LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd); + NopCommand nopCmd = new NopCommand(pwriter, cmd + " is not executed because it is not in the whitelist."); + nopCmd.start(); + return true; + } + + LOG.info("Processing " + cmd + " command from " + channel.getRemoteAddress()); + + if (len == ruokCmd) { + RuokCommand ruok = new RuokCommand(pwriter); + ruok.start(); + return true; + } else if (len == getTraceMaskCmd) { + TraceMaskCommand tmask = new TraceMaskCommand(pwriter); + tmask.start(); + return true; + } else if (len == setTraceMaskCmd) { + ByteBuffer mask = ByteBuffer.allocate(8); + message.readBytes(mask); + mask.flip(); + long traceMask = mask.getLong(); + ZooTrace.setTextTraceLevel(traceMask); + SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, traceMask); + setMask.start(); + return true; + } else if (len == enviCmd) { + EnvCommand env = new EnvCommand(pwriter); + env.start(); + return true; + } else if (len == confCmd) { + ConfCommand ccmd = new ConfCommand(pwriter); + ccmd.start(); + return true; + } else if (len == srstCmd) { + StatResetCommand strst = new StatResetCommand(pwriter); + strst.start(); + return true; + } else if (len == crstCmd) { + CnxnStatResetCommand crst = new CnxnStatResetCommand(pwriter); + crst.start(); + return true; + } else if (len == dumpCmd) { + DumpCommand dump = new DumpCommand(pwriter); + dump.start(); + return true; + } else if (len == statCmd || len == srvrCmd) { + StatCommand stat = new StatCommand(pwriter, len); + stat.start(); + return true; + } else if (len == consCmd) { + ConsCommand cons = new ConsCommand(pwriter); + cons.start(); + return true; + } else if (len == wchpCmd || len == wchcCmd || len == wchsCmd) { + WatchCommand wcmd = new WatchCommand(pwriter, len); + wcmd.start(); + return true; + } else if (len == mntrCmd) { + MonitorCommand mntr = new MonitorCommand(pwriter); + mntr.start(); + return true; + } else if (len == isroCmd) { + IsroCommand isro = new IsroCommand(pwriter); + isro.start(); + return true; + } + return false; + } + + public void receiveMessage(ChannelBuffer message) { + try { + while(message.readable() && !throttled) { + if (bb != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("message readable " + message.readableBytes() + + " bb len " + bb.remaining() + " " + bb); + ByteBuffer dat = bb.duplicate(); + dat.flip(); + LOG.trace(Long.toHexString(sessionId) + + " bb 0x" + + ChannelBuffers.hexDump( + ChannelBuffers.copiedBuffer(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 " + + message.readableBytes() + + " bb len " + bb.remaining() + " " + bb); + ByteBuffer dat = bb.duplicate(); + dat.flip(); + LOG.trace("after readbytes " + + Long.toHexString(sessionId) + + " bb 0x" + + ChannelBuffers.hexDump( + ChannelBuffers.copiedBuffer(dat))); + } + if (bb.remaining() == 0) { + packetReceived(); + bb.flip(); + + ZooKeeperServer zks = this.zkServer; + if (zks == null || !zks.isRunning()) { + throw new IOException("ZK down"); + } + if (initialized) { + zks.processPacket(this, bb); + + if (zks.shouldThrottle(outstandingCount.incrementAndGet())) { + disableRecvNoWait(); + } + } 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 " + + message.readableBytes() + + " bblenrem " + bbLen.remaining()); + ByteBuffer dat = bbLen.duplicate(); + dat.flip(); + LOG.trace(Long.toHexString(sessionId) + + " bbLen 0x" + + ChannelBuffers.hexDump( + ChannelBuffers.copiedBuffer(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(Long.toHexString(sessionId) + + " bbLen 0x" + + ChannelBuffers.hexDump( + ChannelBuffers.copiedBuffer(bbLen))); + } + int len = bbLen.getInt(); + if (LOG.isTraceEnabled()) { + LOG.trace(Long.toHexString(sessionId) + + " bbLen len is " + len); + } + + bbLen.clear(); + if (!initialized) { + if (checkFourLetterWord(channel, message, len)) { + return; + } + } + if (len < 0 || len > BinaryInputArchive.maxBuffer) { + throw new IOException("Len error " + len); + } + bb = ByteBuffer.allocate(len); + } + } + } + } catch(IOException e) { + LOG.warn("Closing connection to " + getRemoteSocketAddress(), e); + close(); + } + } + + @Override + public void disableRecv() { + disableRecvNoWait().awaitUninterruptibly(); + } + + private ChannelFuture disableRecvNoWait() { + throttled = true; + if (LOG.isDebugEnabled()) { + LOG.debug("Throttling - disabling recv " + this); + } + return channel.setReadable(false); + } + + @Override + public long getOutstandingRequests() { + return outstandingCount.longValue(); + } + + @Override + public void setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + } + + @Override + public int getInterestOps() { + return channel.getInterestOps(); + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + return (InetSocketAddress)channel.getRemoteAddress(); + } + + /** Send close connection packet to the client. + */ + @Override + public void sendCloseSession() { + sendBuffer(ServerCnxnFactory.closeConn); + } + + @Override + protected ServerStats serverStats() { + if (!isZKServerRunning()) { + return null; + } + return zkServer.serverStats(); + } + + /** + * @return true if the server is running, false otherwise. + */ + boolean isZKServerRunning() { + return zkServer != null && zkServer.isRunning(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/NettyServerCnxnFactory.java b/src/java/main/org/apache/zookeeper/server/NettyServerCnxnFactory.java new file mode 100644 index 0000000..a34a398 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/NettyServerCnxnFactory.java @@ -0,0 +1,422 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.jboss.netty.buffer.ChannelBuffers.dynamicBuffer; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.Executors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandler.Sharable; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.ExceptionEvent; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.SimpleChannelHandler; +import org.jboss.netty.channel.WriteCompletionEvent; +import org.jboss.netty.channel.group.ChannelGroup; +import org.jboss.netty.channel.group.DefaultChannelGroup; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; + +public class NettyServerCnxnFactory extends ServerCnxnFactory { + Logger LOG = LoggerFactory.getLogger(NettyServerCnxnFactory.class); + + ServerBootstrap bootstrap; + Channel parentChannel; + ChannelGroup allChannels = new DefaultChannelGroup("zkServerCnxns"); + HashMap> ipMap = + new HashMap>( ); + InetSocketAddress localAddress; + int maxClientCnxns = 60; + + /** + * This is an inner class since we need to extend SimpleChannelHandler, but + * NettyServerCnxnFactory already extends ServerCnxnFactory. By making it inner + * this class gets access to the member variables and methods. + */ + @Sharable + class CnxnChannelHandler extends SimpleChannelHandler { + + @Override + public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) + throws Exception + { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel closed " + e); + } + allChannels.remove(ctx.getChannel()); + } + + @Override + public void channelConnected(ChannelHandlerContext ctx, + ChannelStateEvent e) throws Exception + { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel connected " + e); + } + allChannels.add(ctx.getChannel()); + NettyServerCnxn cnxn = new NettyServerCnxn(ctx.getChannel(), + zkServer, NettyServerCnxnFactory.this); + ctx.setAttachment(cnxn); + addCnxn(cnxn); + } + + @Override + public void channelDisconnected(ChannelHandlerContext ctx, + ChannelStateEvent e) throws Exception + { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel disconnected " + e); + } + NettyServerCnxn cnxn = (NettyServerCnxn) ctx.getAttachment(); + if (cnxn != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel disconnect caused close " + e); + } + cnxn.close(); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) + throws Exception + { + LOG.warn("Exception caught " + e, e.getCause()); + NettyServerCnxn cnxn = (NettyServerCnxn) ctx.getAttachment(); + if (cnxn != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Closing " + cnxn); + } + cnxn.close(); + } + } + + @Override + public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) + throws Exception + { + if (LOG.isTraceEnabled()) { + LOG.trace("message received called " + e.getMessage()); + } + try { + if (LOG.isDebugEnabled()) { + LOG.debug("New message " + e.toString() + + " from " + ctx.getChannel()); + } + NettyServerCnxn cnxn = (NettyServerCnxn)ctx.getAttachment(); + synchronized(cnxn) { + processMessage(e, cnxn); + } + } catch(Exception ex) { + LOG.error("Unexpected exception in receive", ex); + throw ex; + } + } + + private void processMessage(MessageEvent e, NettyServerCnxn cnxn) { + if (LOG.isDebugEnabled()) { + LOG.debug(Long.toHexString(cnxn.sessionId) + " queuedBuffer: " + + cnxn.queuedBuffer); + } + + if (e instanceof NettyServerCnxn.ResumeMessageEvent) { + LOG.debug("Received ResumeMessageEvent"); + if (cnxn.queuedBuffer != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("processing queue " + + Long.toHexString(cnxn.sessionId) + + " queuedBuffer 0x" + + ChannelBuffers.hexDump(cnxn.queuedBuffer)); + } + cnxn.receiveMessage(cnxn.queuedBuffer); + if (!cnxn.queuedBuffer.readable()) { + LOG.debug("Processed queue - no bytes remaining"); + cnxn.queuedBuffer = null; + } else { + LOG.debug("Processed queue - bytes remaining"); + } + } else { + LOG.debug("queue empty"); + } + cnxn.channel.setReadable(true); + } else { + ChannelBuffer buf = (ChannelBuffer)e.getMessage(); + if (LOG.isTraceEnabled()) { + LOG.trace(Long.toHexString(cnxn.sessionId) + + " buf 0x" + + ChannelBuffers.hexDump(buf)); + } + + if (cnxn.throttled) { + LOG.debug("Received message while throttled"); + // we are throttled, so we need to queue + if (cnxn.queuedBuffer == null) { + LOG.debug("allocating queue"); + cnxn.queuedBuffer = dynamicBuffer(buf.readableBytes()); + } + cnxn.queuedBuffer.writeBytes(buf); + if (LOG.isTraceEnabled()) { + LOG.trace(Long.toHexString(cnxn.sessionId) + + " queuedBuffer 0x" + + ChannelBuffers.hexDump(cnxn.queuedBuffer)); + } + } else { + LOG.debug("not throttled"); + if (cnxn.queuedBuffer != null) { + if (LOG.isTraceEnabled()) { + LOG.trace(Long.toHexString(cnxn.sessionId) + + " queuedBuffer 0x" + + ChannelBuffers.hexDump(cnxn.queuedBuffer)); + } + cnxn.queuedBuffer.writeBytes(buf); + if (LOG.isTraceEnabled()) { + LOG.trace(Long.toHexString(cnxn.sessionId) + + " queuedBuffer 0x" + + ChannelBuffers.hexDump(cnxn.queuedBuffer)); + } + + cnxn.receiveMessage(cnxn.queuedBuffer); + if (!cnxn.queuedBuffer.readable()) { + LOG.debug("Processed queue - no bytes remaining"); + cnxn.queuedBuffer = null; + } else { + LOG.debug("Processed queue - bytes remaining"); + } + } else { + cnxn.receiveMessage(buf); + if (buf.readable()) { + if (LOG.isTraceEnabled()) { + LOG.trace("Before copy " + buf); + } + cnxn.queuedBuffer = dynamicBuffer(buf.readableBytes()); + cnxn.queuedBuffer.writeBytes(buf); + if (LOG.isTraceEnabled()) { + LOG.trace("Copy is " + cnxn.queuedBuffer); + LOG.trace(Long.toHexString(cnxn.sessionId) + + " queuedBuffer 0x" + + ChannelBuffers.hexDump(cnxn.queuedBuffer)); + } + } + } + } + } + } + + @Override + public void writeComplete(ChannelHandlerContext ctx, + WriteCompletionEvent e) throws Exception + { + if (LOG.isTraceEnabled()) { + LOG.trace("write complete " + e); + } + } + + } + + CnxnChannelHandler channelHandler = new CnxnChannelHandler(); + + NettyServerCnxnFactory() { + bootstrap = new ServerBootstrap( + new NioServerSocketChannelFactory( + Executors.newCachedThreadPool(), + Executors.newCachedThreadPool())); + // parent channel + bootstrap.setOption("reuseAddress", true); + // child channels + bootstrap.setOption("child.tcpNoDelay", true); + /* set socket linger to off, so that socket close does not block */ + bootstrap.setOption("child.soLinger", -1); + + bootstrap.getPipeline().addLast("servercnxnfactory", channelHandler); + } + + @Override + public void closeAll() { + if (LOG.isDebugEnabled()) { + LOG.debug("closeAll()"); + } + + NettyServerCnxn[] allCnxns = null; + synchronized (cnxns) { + allCnxns = cnxns.toArray(new NettyServerCnxn[cnxns.size()]); + } + // got to clear all the connections that we have in the selector + for (NettyServerCnxn cnxn : allCnxns) { + try { + cnxn.close(); + } catch (Exception e) { + LOG.warn("Ignoring exception closing cnxn sessionid 0x" + + Long.toHexString(cnxn.getSessionId()), e); + } + } + if (LOG.isDebugEnabled()) { + LOG.debug("allChannels size:" + allChannels.size() + " cnxns size:" + + allCnxns.length); + } + } + + @Override + public void closeSession(long sessionId) { + if (LOG.isDebugEnabled()) { + LOG.debug("closeSession sessionid:0x" + sessionId); + } + + NettyServerCnxn cnxn = (NettyServerCnxn) sessionMap.remove(sessionId); + if (cnxn != null) { + try { + cnxn.close(); + } catch (Exception e) { + LOG.warn("exception during session close", e); + } + } + } + + @Override + public void configure(InetSocketAddress addr, int maxClientCnxns) + throws IOException + { + configureSaslLogin(); + localAddress = addr; + this.maxClientCnxns = maxClientCnxns; + } + + /** {@inheritDoc} */ + public int getMaxClientCnxnsPerHost() { + return maxClientCnxns; + } + + /** {@inheritDoc} */ + public void setMaxClientCnxnsPerHost(int max) { + maxClientCnxns = max; + } + + @Override + public int getLocalPort() { + return localAddress.getPort(); + } + + boolean killed; + @Override + public void join() throws InterruptedException { + synchronized(this) { + while(!killed) { + wait(); + } + } + } + + @Override + public void shutdown() { + LOG.info("shutdown called " + localAddress); + if (login != null) { + login.shutdown(); + } + // null if factory never started + if (parentChannel != null) { + parentChannel.close().awaitUninterruptibly(); + closeAll(); + allChannels.close().awaitUninterruptibly(); + bootstrap.releaseExternalResources(); + } + + if (zkServer != null) { + zkServer.shutdown(); + } + synchronized(this) { + killed = true; + notifyAll(); + } + } + + @Override + public void start() { + LOG.info("binding to port " + localAddress); + parentChannel = bootstrap.bind(localAddress); + } + + @Override + public void startup(ZooKeeperServer zks) throws IOException, + InterruptedException { + start(); + setZooKeeperServer(zks); + zks.startdata(); + zks.startup(); + } + + @Override + public Iterable getConnections() { + return cnxns; + } + + @Override + public InetSocketAddress getLocalAddress() { + return localAddress; + } + + private void addCnxn(NettyServerCnxn cnxn) { + synchronized (cnxns) { + cnxns.add(cnxn); + synchronized (ipMap){ + InetAddress addr = + ((InetSocketAddress)cnxn.channel.getRemoteAddress()) + .getAddress(); + Set s = ipMap.get(addr); + if (s == null) { + s = new HashSet(); + } + s.add(cnxn); + ipMap.put(addr,s); + } + } + } + + public void removeCnxn(ServerCnxn cnxn) { + synchronized(cnxns){ + // if this is not in cnxns then it's already closed + if (!cnxns.remove(cnxn)) { + if (LOG.isDebugEnabled()) { + LOG.debug("cnxns size:" + cnxns.size()); + } + return; + } + if (LOG.isDebugEnabled()) { + LOG.debug("close in progress for sessionid:0x" + + Long.toHexString(cnxn.getSessionId())); + } + + synchronized (ipMap) { + Set s = + ipMap.get(cnxn.getSocketAddress()); + s.remove(cnxn); + } + } + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/ObserverBean.java b/src/java/main/org/apache/zookeeper/server/ObserverBean.java new file mode 100644 index 0000000..72d724e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ObserverBean.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; + +import org.apache.zookeeper.server.quorum.Observer; +import org.apache.zookeeper.server.quorum.ObserverMXBean; + +/** + * 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(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java new file mode 100644 index 0000000..825c22a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java @@ -0,0 +1,774 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Locale; +import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.jute.Record; +import org.apache.jute.BinaryOutputArchive; + +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.BadArgumentsException; +import org.apache.zookeeper.MultiTransactionRecord; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.StatPersisted; +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.proto.CheckVersionRequest; +import org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord; +import org.apache.zookeeper.server.auth.AuthenticationProvider; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.quorum.Leader.XidRolloverException; +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.CheckVersionTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * 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); + + static boolean skipACL; + static { + skipACL = System.getProperty("zookeeper.skipACL", "no").equals("yes"); + if (skipACL) { + LOG.info("zookeeper.skipACL==\"yes\", ACL checks will be skipped"); + } + } + + /** + * this is only for testing purposes. + * should never be useed otherwise + */ + private static boolean failCreate = false; + + LinkedBlockingQueue submittedRequests = new LinkedBlockingQueue(); + + RequestProcessor nextProcessor; + + ZooKeeperServer zks; + + public PrepRequestProcessor(ZooKeeperServer zks, + RequestProcessor nextProcessor) { + super("ProcessThread(sid:" + zks.getServerId() + " cport:" + + zks.getClientPort() + "):", zks.getZooKeeperServerListener()); + this.nextProcessor = nextProcessor; + this.zks = zks; + } + + /** + * method for tests to set failCreate + * @param b + */ + public static void setFailCreate(boolean b) { + failCreate = b; + } + @Override + public void run() { + try { + while (true) { + Request request = submittedRequests.take(); + 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; + } + pRequest(request); + } + } catch (RequestProcessorException e) { + if (e.getCause() instanceof XidRolloverException) { + LOG.info(e.getCause().getMessage()); + } + handleException(this.getName(), e); + } catch (Exception e) { + handleException(this.getName(), e); + } + LOG.info("PrepRequestProcessor exited loop!"); + } + + 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 (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); + } + } + + void addChangeRecord(ChangeRecord c) { + synchronized (zks.outstandingChanges) { + zks.outstandingChanges.add(c); + zks.outstandingChangesForPath.put(c.path, c); + } + } + + /** + * 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. + */ + HashMap getPendingChanges(MultiTransactionRecord multiRequest) { + HashMap 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, HashMappendingChangeRecords) { + synchronized (zks.outstandingChanges) { + // Grab a list iterator starting at the END of the list so we can iterate in reverse + ListIterator iter = zks.outstandingChanges.listIterator(zks.outstandingChanges.size()); + while (iter.hasPrevious()) { + ChangeRecord c = iter.previous(); + 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.get(0).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); + } + } + } + + static void checkACL(ZooKeeperServer zks, List acl, int perm, + List ids) throws KeeperException.NoAuthException { + if (skipACL) { + return; + } + 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; + } + AuthenticationProvider ap = ProviderRegistry.getProvider(id + .getScheme()); + if (ap != null) { + for (Id authId : ids) { + if (authId.getScheme().equals(id.getScheme()) + && ap.matches(authId.getId(), id.getId())) { + return; + } + } + } + } + } + throw new KeeperException.NoAuthException(); + } + + /** + * 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 + */ + @SuppressWarnings("unchecked") + protected void pRequest2Txn(int type, long zxid, Request request, Record record, boolean deserialize) + throws KeeperException, IOException, RequestProcessorException + { + request.hdr = new TxnHeader(request.sessionId, request.cxid, zxid, + Time.currentWallTime(), type); + + switch (type) { + case OpCode.create: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + CreateRequest createRequest = (CreateRequest)record; + if(deserialize) + ByteBufferInputStream.byteBuffer2Record(request.request, createRequest); + String path = createRequest.getPath(); + int lastSlash = path.lastIndexOf('/'); + if (lastSlash == -1 || path.indexOf('\0') != -1 || failCreate) { + LOG.info("Invalid path " + path + " with session 0x" + + Long.toHexString(request.sessionId)); + throw new KeeperException.BadArgumentsException(path); + } + List listACL = removeDuplicates(createRequest.getAcl()); + if (!fixupACL(request.authInfo, listACL)) { + throw new KeeperException.InvalidACLException(path); + } + String parentPath = path.substring(0, lastSlash); + ChangeRecord parentRecord = getRecordForPath(parentPath); + + checkACL(zks, parentRecord.acl, ZooDefs.Perms.CREATE, + request.authInfo); + int parentCVersion = parentRecord.stat.getCversion(); + CreateMode createMode = + CreateMode.fromFlag(createRequest.getFlags()); + 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 = parentRecord.stat.getEphemeralOwner() != 0; + if (ephemeralParent) { + throw new KeeperException.NoChildrenForEphemeralsException(path); + } + int newCversion = parentRecord.stat.getCversion()+1; + request.txn = new CreateTxn(path, createRequest.getData(), + listACL, + createMode.isEphemeral(), newCversion); + StatPersisted s = new StatPersisted(); + if (createMode.isEphemeral()) { + s.setEphemeralOwner(request.sessionId); + } + parentRecord = parentRecord.duplicate(request.hdr.getZxid()); + parentRecord.childCount++; + parentRecord.stat.setCversion(newCversion); + addChangeRecord(parentRecord); + addChangeRecord(new ChangeRecord(request.hdr.getZxid(), path, s, + 0, listACL)); + break; + case OpCode.delete: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + DeleteRequest deleteRequest = (DeleteRequest)record; + if(deserialize) + ByteBufferInputStream.byteBuffer2Record(request.request, deleteRequest); + path = deleteRequest.getPath(); + lastSlash = path.lastIndexOf('/'); + if (lastSlash == -1 || path.indexOf('\0') != -1 + || zks.getZKDatabase().isSpecialPath(path)) { + throw new KeeperException.BadArgumentsException(path); + } + parentPath = path.substring(0, lastSlash); + parentRecord = getRecordForPath(parentPath); + ChangeRecord nodeRecord = getRecordForPath(path); + checkACL(zks, parentRecord.acl, ZooDefs.Perms.DELETE, + request.authInfo); + int version = deleteRequest.getVersion(); + if (version != -1 && nodeRecord.stat.getVersion() != version) { + throw new KeeperException.BadVersionException(path); + } + if (nodeRecord.childCount > 0) { + throw new KeeperException.NotEmptyException(path); + } + request.txn = new DeleteTxn(path); + parentRecord = parentRecord.duplicate(request.hdr.getZxid()); + parentRecord.childCount--; + addChangeRecord(parentRecord); + addChangeRecord(new ChangeRecord(request.hdr.getZxid(), path, + null, -1, null)); + 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); + checkACL(zks, nodeRecord.acl, ZooDefs.Perms.WRITE, + request.authInfo); + version = setDataRequest.getVersion(); + int currentVersion = nodeRecord.stat.getVersion(); + if (version != -1 && version != currentVersion) { + throw new KeeperException.BadVersionException(path); + } + version = currentVersion + 1; + request.txn = new SetDataTxn(path, setDataRequest.getData(), version); + nodeRecord = nodeRecord.duplicate(request.hdr.getZxid()); + nodeRecord.stat.setVersion(version); + 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); + listACL = removeDuplicates(setAclRequest.getAcl()); + if (!fixupACL(request.authInfo, listACL)) { + throw new KeeperException.InvalidACLException(path); + } + nodeRecord = getRecordForPath(path); + checkACL(zks, nodeRecord.acl, ZooDefs.Perms.ADMIN, + request.authInfo); + version = setAclRequest.getVersion(); + currentVersion = nodeRecord.stat.getAversion(); + if (version != -1 && version != currentVersion) { + throw new KeeperException.BadVersionException(path); + } + version = currentVersion + 1; + request.txn = new SetACLTxn(path, listACL, version); + nodeRecord = nodeRecord.duplicate(request.hdr.getZxid()); + nodeRecord.stat.setAversion(version); + addChangeRecord(nodeRecord); + break; + case OpCode.createSession: + request.request.rewind(); + int to = request.request.getInt(); + request.txn = new CreateSessionTxn(to); + request.request.rewind(); + zks.sessionTracker.addSession(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()); + HashSet es = zks.getZKDatabase() + .getEphemerals(request.sessionId); + synchronized (zks.outstandingChanges) { + 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) { + addChangeRecord(new ChangeRecord(request.hdr.getZxid(), + path2Delete, null, 0, null)); + } + + zks.sessionTracker.setSessionClosing(request.sessionId); + } + + LOG.info("Processed session termination for sessionid: 0x" + + Long.toHexString(request.sessionId)); + 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); + checkACL(zks, nodeRecord.acl, ZooDefs.Perms.READ, + request.authInfo); + version = checkVersionRequest.getVersion(); + currentVersion = nodeRecord.stat.getVersion(); + if (version != -1 && version != currentVersion) { + throw new KeeperException.BadVersionException(path); + } + version = currentVersion + 1; + request.txn = new CheckVersionTxn(path, version); + break; + default: + LOG.error("Invalid OpCode: {} received by PrepRequestProcessor", type); + } + } + + private void validatePath(String path, long sessionId) throws BadArgumentsException { + try { + PathUtils.validatePath(path); + } catch(IllegalArgumentException ie) { + LOG.info("Invalid path " + path + " with session 0x" + Long.toHexString(sessionId) + + ", reason: " + ie.getMessage()); + throw new BadArgumentsException(path); + } + } + + /** + * This method will be called inside the ProcessRequestThread, which is a + * singleton, so there will be a single thread calling this code. + * + * @param request + */ + @SuppressWarnings("unchecked") + protected void pRequest(Request request) throws RequestProcessorException { + // LOG.info("Prep>>> cxid = " + request.cxid + " type = " + + // request.type + " id = 0x" + Long.toHexString(request.sessionId)); + request.hdr = null; + request.txn = null; + + try { + switch (request.type) { + case OpCode.create: + CreateRequest createRequest = new CreateRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, createRequest, true); + break; + 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.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: + MultiTransactionRecord multiRequest = new MultiTransactionRecord(); + try { + ByteBufferInputStream.byteBuffer2Record(request.request, multiRequest); + } catch(IOException e) { + request.hdr = 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 + HashMap pendingChanges = getPendingChanges(multiRequest); + + int index = 0; + for(Op op: multiRequest) { + Record subrequest = op.toRequestRecord() ; + + /* 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) { + request.hdr.setType(OpCode.error); + request.txn = new ErrorTxn(Code.RUNTIMEINCONSISTENCY.intValue()); + } + + /* Prep the request and convert to a Txn */ + else { + try { + pRequest2Txn(op.getType(), zxid, request, subrequest, false); + } catch (KeeperException e) { + ke = e; + request.hdr.setType(OpCode.error); + request.txn = new ErrorTxn(e.code().intValue()); + LOG.info("Got user-level KeeperException when processing " + + request.toString() + " aborting remaining multi ops." + + " Error Path:" + e.getPath() + + " Error:" + e.getMessage()); + + request.setException(e); + + /* Rollback change records from failed multi-op */ + rollbackPendingChanges(zxid, pendingChanges); + } + } + + //FIXME: 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. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + request.txn.serialize(boa, "request") ; + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + + txns.add(new Txn(request.hdr.getType(), bb.array())); + index++; + } + + request.hdr = new TxnHeader(request.sessionId, request.cxid, zxid, + Time.currentWallTime(), request.type); + request.txn = new MultiTxn(txns); + + break; + + //create/close session don't require request record + case OpCode.createSession: + case OpCode.closeSession: + pRequest2Txn(request.type, zks.getNextZxid(), request, null, true); + break; + + //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.getChildren2: + case OpCode.ping: + case OpCode.setWatches: + zks.sessionTracker.checkSession(request.sessionId, + request.getOwner()); + break; + default: + LOG.warn("unknown type " + request.type); + break; + } + } catch (KeeperException e) { + if (request.hdr != null) { + request.hdr.setType(OpCode.error); + request.txn = new ErrorTxn(e.code().intValue()); + } + LOG.info("Got user-level KeeperException when processing " + + request.toString() + + " Error Path:" + e.getPath() + + " Error:" + 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.hdr != null) { + request.hdr.setType(OpCode.error); + request.txn = new ErrorTxn(Code.MARSHALLINGERROR.intValue()); + } + } + request.zxid = zks.getZxid(); + nextProcessor.processRequest(request); + } + + private List removeDuplicates(List acl) { + + ArrayList retval = new ArrayList(); + Iterator it = acl.iterator(); + while (it.hasNext()) { + ACL a = it.next(); + if (retval.contains(a) == false) { + retval.add(a); + } + } + return retval; + } + + + /** + * 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 acl list of ACLs being assigned to the node (create or setACL operation) + * @return + */ + private boolean fixupACL(List authInfo, List acl) { + if (skipACL) { + return true; + } + if (acl == null || acl.size() == 0) { + return false; + } + + Iterator it = acl.iterator(); + LinkedList toAdd = null; + while (it.hasNext()) { + ACL a = it.next(); + Id id = a.getId(); + if (id.getScheme().equals("world") && id.getId().equals("anyone")) { + // wide open + } else if (id.getScheme().equals("auth")) { + // This is the "auth" id, so we have to expand it to the + // authenticated ids of the requestor + it.remove(); + if (toAdd == null) { + toAdd = new LinkedList(); + } + boolean authIdValid = false; + for (Id cid : authInfo) { + AuthenticationProvider ap = + ProviderRegistry.getProvider(cid.getScheme()); + if (ap == null) { + LOG.error("Missing AuthenticationProvider for " + + cid.getScheme()); + } else if (ap.isAuthenticated()) { + authIdValid = true; + toAdd.add(new ACL(a.getPerms(), cid)); + } + } + if (!authIdValid) { + return false; + } + } else { + AuthenticationProvider ap = ProviderRegistry.getProvider(id + .getScheme()); + if (ap == null) { + return false; + } + if (!ap.isValid(id.getId())) { + return false; + } + } + } + if (toAdd != null) { + for (ACL a : toAdd) { + acl.add(a); + } + } + return acl.size() > 0; + } + + public void processRequest(Request request) { + // request.addRQRec(">prep="+zks.outstandingChanges.size()); + submittedRequests.add(request); + } + + public void shutdown() { + LOG.info("Shutting down"); + submittedRequests.clear(); + submittedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/PurgeTxnLog.java b/src/java/main/org/apache/zookeeper/server/PurgeTxnLog.java new file mode 100644 index 0000000..09cffba --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/PurgeTxnLog.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.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.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); + + List snaps = txnLog.findNRecentSnapshots(num); + int numSnaps = snaps.size(); + if (numSnaps > 0) { + purgeOlderSnapshots(txnLog, snaps.get(numSnaps - 1)); + } + } + + // VisibleForTesting + static void purgeOlderSnapshots(FileTxnSnapLog txnLog, File snapShot) { + 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); + if (fZxid >= leastZxidToBeRetain) { + return false; + } + return true; + } + } + // add all non-excluded log files + List files = new ArrayList(); + File[] fileArray = txnLog.getDataDir().listFiles(new MyFileFilter(PREFIX_LOG)); + if (fileArray != null) { + files.addAll(Arrays.asList(fileArray)); + } + + // add all non-excluded snapshot files to the deletion list + fileArray = txnLog.getSnapDir().listFiles(new MyFileFilter(PREFIX_SNAPSHOT)); + if (fileArray != null) { + files.addAll(Arrays.asList(fileArray)); + } + + // remove the old files + for(File f: files) + { + final String msg = "Removing file: "+ + DateFormat.getDateTimeInstance().format(f.lastModified())+ + "\t"+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(); + System.exit(1); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ReferenceCountedACLCache.java b/src/java/main/org/apache/zookeeper/server/ReferenceCountedACLCache.java new file mode 100644 index 0000000..384d23a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ReferenceCountedACLCache.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.server; + +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; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; + +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 synchronized void deserialize(InputArchive ia) throws IOException { + clear(); + int i = ia.readInt("map"); + while (i > 0) { + Long val = ia.readLong("long"); + if (aclIndex < val) { + aclIndex = val; + } + 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(); + } + longKeyMap.put(val, aclList); + aclKeyMap.put(aclList, val); + referenceCounter.put(val, new AtomicLongWithEquals(0)); + i--; + } + } + + public synchronized void serialize(OutputArchive oa) throws IOException { + oa.writeInt(longKeyMap.size(), "map"); + Set>> set = longKeyMap.entrySet(); + for (Map.Entry> val : set) { + 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 " + acl + " as it does not exist in the cache"); + 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 " + acl + " as it does not exist in the cache"); + 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/src/java/main/org/apache/zookeeper/server/Request.java b/src/java/main/org/apache/zookeeper/server/Request.java new file mode 100644 index 0000000..92eb3cc --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/Request.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.nio.ByteBuffer; +import java.util.List; + +import org.apache.jute.Record; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.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 { + private static final Logger LOG = LoggerFactory.getLogger(Request.class); + + public final static Request requestOfDeath = new Request(null, 0, 0, 0, + null, null); + + /** + * @param cnxn + * @param sessionId + * @param xid + * @param type + * @param bb + */ + 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; + + public TxnHeader hdr; + + public Record txn; + + public long zxid = -1; + + public final List authInfo; + + public final long createTime = Time.currentElapsedTime(); + + private Object owner; + + private KeeperException e; + + public Object getOwner() { + return owner; + } + + public void setOwner(Object owner) { + this.owner = owner; + } + + /** + * 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.create: + case OpCode.delete: + case OpCode.createSession: + case OpCode.exists: + case OpCode.getData: + case OpCode.check: + case OpCode.multi: + case OpCode.setData: + case OpCode.sync: + case OpCode.getACL: + case OpCode.setACL: + case OpCode.getChildren: + case OpCode.getChildren2: + case OpCode.ping: + case OpCode.closeSession: + case OpCode.setWatches: + return true; + default: + return false; + } + } + + static boolean isQuorum(int type) { + switch (type) { + case OpCode.exists: + case OpCode.getACL: + case OpCode.getChildren: + case OpCode.getChildren2: + case OpCode.getData: + return false; + case OpCode.error: + case OpCode.closeSession: + case OpCode.create: + case OpCode.createSession: + case OpCode.delete: + case OpCode.setACL: + case OpCode.setData: + case OpCode.check: + case OpCode.multi: + return true; + default: + return false; + } + } + + static String op2String(int op) { + switch (op) { + case OpCode.notification: + return "notification"; + case OpCode.create: + return "create"; + case OpCode.setWatches: + return "setWatches"; + case OpCode.delete: + return "delete"; + case OpCode.exists: + return "exists"; + case OpCode.getData: + return "getData"; + case OpCode.check: + return "check"; + case OpCode.multi: + return "multi"; + case OpCode.setData: + return "setData"; + case OpCode.sync: + return "sync:"; + case OpCode.getACL: + return "getACL"; + case OpCode.setACL: + return "setACL"; + case OpCode.getChildren: + return "getChildren"; + case OpCode.getChildren2: + return "getChildren2"; + case OpCode.ping: + return "ping"; + 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.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; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/RequestProcessor.java b/src/java/main/org/apache/zookeeper/server/RequestProcessor.java new file mode 100644 index 0000000..9c9e5bd --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/RequestProcessor.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; + +/** + * 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") + public static class RequestProcessorException extends Exception { + public RequestProcessorException(String msg, Throwable t) { + super(msg, t); + } + } + + void processRequest(Request request) throws RequestProcessorException; + + void shutdown(); +} diff --git a/src/java/main/org/apache/zookeeper/server/ServerCnxn.java b/src/java/main/org/apache/zookeeper/server/ServerCnxn.java new file mode 100644 index 0000000..dfa1d74 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ServerCnxn.java @@ -0,0 +1,548 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.io.StringWriter; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.HashSet; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.jute.Record; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; + +/** + * 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 + final public static Object me = new Object(); + + protected ArrayList authInfo = new ArrayList(); + + /** + * 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; + + abstract int getSessionTimeout(); + + abstract void close(); + + public abstract void sendResponse(ReplyHeader h, Record r, String tag) + throws IOException; + + /* notify the client the session is closing and close/cleanup socket */ + abstract void sendCloseSession(); + + public abstract void process(WatchedEvent event); + + abstract long getSessionId(); + + abstract void setSessionId(long sessionId); + + /** auth info for the cnxn, returns an unmodifyable list */ + public List getAuthInfo() { + return Collections.unmodifiableList(authInfo); + } + + public void addAuthInfo(Id id) { + if (authInfo.contains(id) == false) { + authInfo.add(id); + } + } + + public boolean removeAuthInfo(Id id) { + return authInfo.remove(id); + } + + abstract void sendBuffer(ByteBuffer closeConn); + + abstract void enableRecv(); + + abstract void disableRecv(); + + abstract void setSessionTimeout(int sessionTimeout); + + /** + * Wrapper method to return the socket address + */ + public abstract InetAddress getSocketAddress(); + + protected ZooKeeperSaslServer zooKeeperSaslServer = null; + + protected static class CloseRequestException extends IOException { + private static final long serialVersionUID = -7854505709816442681L; + + public CloseRequestException(String msg) { + super(msg); + } + } + + protected static class EndOfStreamException extends IOException { + private static final long serialVersionUID = -8255690282104294178L; + + public EndOfStreamException(String msg) { + super(msg); + } + + public String toString() { + return "EndOfStreamException: " + getMessage(); + } + } + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int confCmd = + ByteBuffer.wrap("conf".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int consCmd = + ByteBuffer.wrap("cons".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int crstCmd = + ByteBuffer.wrap("crst".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int dumpCmd = + ByteBuffer.wrap("dump".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int enviCmd = + ByteBuffer.wrap("envi".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int getTraceMaskCmd = + ByteBuffer.wrap("gtmk".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int ruokCmd = + ByteBuffer.wrap("ruok".getBytes()).getInt(); + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int setTraceMaskCmd = + ByteBuffer.wrap("stmk".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int srvrCmd = + ByteBuffer.wrap("srvr".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int srstCmd = + ByteBuffer.wrap("srst".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int statCmd = + ByteBuffer.wrap("stat".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int wchcCmd = + ByteBuffer.wrap("wchc".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int wchpCmd = + ByteBuffer.wrap("wchp".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int wchsCmd = + ByteBuffer.wrap("wchs".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int mntrCmd = ByteBuffer.wrap("mntr".getBytes()) + .getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected final static int isroCmd = ByteBuffer.wrap("isro".getBytes()) + .getInt(); + + final static Map cmd2String = new HashMap(); + + private static final String ZOOKEEPER_4LW_COMMANDS_WHITELIST = "zookeeper.4lw.commands.whitelist"; + + private static final Logger LOG = LoggerFactory.getLogger(ServerCnxn.class); + + private static final Set whiteListedCommands = new HashSet(); + + private static boolean whiteListInitialized = false; + + // @VisibleForTesting + public synchronized static 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 synchronized static 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()); + } + } + } else { + for (Map.Entry entry : cmd2String.entrySet()) { + String cmd = entry.getValue(); + if (cmd.equals("wchc") || cmd.equals("wchp")) { + // ZOOKEEPER-2693 / disable these exploitable commands by default. + continue; + } + whiteListedCommands.add(cmd); + } + } + + // Readonly mode depends on "isro". + 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 : {}", Collections.singletonList(cmd2String)); + LOG.info("The list of enabled four letter word commands is : {}", Collections.singletonList(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(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"); + } + + protected void packetReceived() { + incrPacketsReceived(); + ServerStats serverStats = serverStats(); + if (serverStats != null) { + serverStats().incrementPacketsReceived(); + } + } + + 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; + + public synchronized void resetStats() { + 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 void incrOutstandingRequests(RequestHeader h) { + } + + 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 abstract long getOutstandingRequests(); + + 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(); + + /** + * Print information about the connection. + * @param brief iff true prints brief details, otw full detail + * @return information about this connection + */ + protected 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(")"); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/ServerCnxnFactory.java b/src/java/main/org/apache/zookeeper/server/ServerCnxnFactory.java new file mode 100644 index 0000000..92d793f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ServerCnxnFactory.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.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginException; +import javax.security.auth.login.AppConfigurationEntry; + +import javax.management.JMException; + +import org.apache.zookeeper.Login; +import org.apache.zookeeper.Environment; +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"; + + public interface PacketProcessor { + public void processPacket(ByteBuffer packet, ServerCnxn src); + } + + private static final Logger LOG = LoggerFactory.getLogger(ServerCnxnFactory.class); + + // sessionMap is used to speed up closeSession() + protected final ConcurrentMap sessionMap = + new ConcurrentHashMap(); + + /** + * The buffer will cause the connection to be close when we do a send. + */ + static final ByteBuffer closeConn = ByteBuffer.allocate(0); + + public abstract int getLocalPort(); + + public abstract Iterable getConnections(); + + public int getNumAliveConnections() { + synchronized(cnxns) { + return cnxns.size(); + } + } + + ZooKeeperServer getZooKeeperServer() { + return zkServer; + } + + public abstract void closeSession(long sessionId); + + public abstract void configure(InetSocketAddress addr, + int maxClientCnxns) throws IOException; + + 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 abstract void startup(ZooKeeperServer zkServer) + throws IOException, InterruptedException; + + public abstract void join() throws InterruptedException; + + public abstract void shutdown(); + + public abstract void start(); + + protected ZooKeeperServer zkServer; + final public void setZooKeeperServer(ZooKeeperServer zk) { + this.zkServer = zk; + if (zk != null) { + zk.setServerCnxnFactory(this); + } + } + + public abstract void closeAll(); + + static public ServerCnxnFactory createFactory() throws IOException { + String serverCnxnFactoryName = + System.getProperty(ZOOKEEPER_SERVER_CNXN_FACTORY); + if (serverCnxnFactoryName == null) { + serverCnxnFactoryName = NIOServerCnxnFactory.class.getName(); + } + 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); + ioe.initCause(e); + throw ioe; + } + } + + static public ServerCnxnFactory createFactory(int clientPort, + int maxClientCnxns) throws IOException + { + return createFactory(new InetSocketAddress(clientPort), maxClientCnxns); + } + + static public ServerCnxnFactory createFactory(InetSocketAddress addr, + int maxClientCnxns) throws IOException + { + ServerCnxnFactory factory = createFactory(); + factory.configure(addr, maxClientCnxns); + return factory; + } + + public abstract InetSocketAddress getLocalAddress(); + + private final Map connectionBeans + = new ConcurrentHashMap(); + + protected final HashSet cnxns = new HashSet(); + 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); + } + } + + } + + public void addSession(long sessionId, ServerCnxn cnxn) { + sessionMap.put(sessionId, cnxn); + } + + /** + * 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); + 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); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ServerConfig.java b/src/java/main/org/apache/zookeeper/server/ServerConfig.java new file mode 100644 index 0000000..c0dee3c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ServerConfig.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; + +import java.net.InetSocketAddress; +import java.util.Arrays; + +import org.apache.yetus.audience.InterfaceAudience; +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 String dataDir; + protected String 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; + + /** + * Parse arguments for server configuration + * @param args clientPort dataDir and optional tickTime and maxClientCnxns + * @return ServerConfig configured wrt arguments + * @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 = 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 + * @return ServerConfig configured wrt arguments + * @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(); + dataDir = config.getDataDir(); + dataLogDir = config.getDataLogDir(); + tickTime = config.getTickTime(); + maxClientCnxns = config.getMaxClientCnxns(); + minSessionTimeout = config.getMinSessionTimeout(); + maxSessionTimeout = config.getMaxSessionTimeout(); + } + + public InetSocketAddress getClientPortAddress() { + return clientPortAddress; + } + public String getDataDir() { return dataDir; } + public String 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; } +} diff --git a/src/java/main/org/apache/zookeeper/server/ServerStats.java b/src/java/main/org/apache/zookeeper/server/ServerStats.java new file mode 100644 index 0000000..f41aaa5 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ServerStats.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; + + + +import org.apache.zookeeper.common.Time; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * Basic Server Statistics + */ +public class ServerStats { + private long packetsSent; + private long packetsReceived; + private long maxLatency; + private long minLatency = Long.MAX_VALUE; + private long totalLatency = 0; + private long count = 0; + private AtomicLong fsyncThresholdExceedCount = new AtomicLong(0); + + private final Provider provider; + + public interface Provider { + public long getOutstandingRequests(); + public long getLastProcessedZxid(); + public String getState(); + public int getNumAliveConnections(); + } + + public ServerStats(Provider provider) { + this.provider = provider; + } + + // getters + synchronized public long getMinLatency() { + return minLatency == Long.MAX_VALUE ? 0 : minLatency; + } + + synchronized public long getAvgLatency() { + if (count != 0) { + return totalLatency / count; + } + return 0; + } + + synchronized public long getMaxLatency() { + return maxLatency; + } + + public long getOutstandingRequests() { + return provider.getOutstandingRequests(); + } + + public long getLastProcessedZxid(){ + return provider.getLastProcessedZxid(); + } + + synchronized public long getPacketsReceived() { + return packetsReceived; + } + + synchronized public long getPacketsSent() { + return packetsSent; + } + + public String getServerState() { + return provider.getState(); + } + + /** The number of client connections alive to this server */ + public int getNumAliveClientConnections() { + return provider.getNumAliveConnections(); + } + + @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(); + } + + public long getFsyncThresholdExceedCount() { + return fsyncThresholdExceedCount.get(); + } + + public void incrementFsyncThresholdExceedCount() { + fsyncThresholdExceedCount.incrementAndGet(); + } + + public void resetFsyncThresholdExceedCount() { + fsyncThresholdExceedCount.set(0); + } + + // mutators + synchronized void updateLatency(long requestCreateTime) { + long latency = Time.currentElapsedTime() - requestCreateTime; + totalLatency += latency; + count++; + if (latency < minLatency) { + minLatency = latency; + } + if (latency > maxLatency) { + maxLatency = latency; + } + } + synchronized public void resetLatency(){ + totalLatency = 0; + count = 0; + maxLatency = 0; + minLatency = Long.MAX_VALUE; + } + synchronized public void resetMaxLatency(){ + maxLatency = getMinLatency(); + } + synchronized public void incrementPacketsReceived() { + packetsReceived++; + } + synchronized public void incrementPacketsSent() { + packetsSent++; + } + synchronized public void resetRequestCounters(){ + packetsReceived = 0; + packetsSent = 0; + } + synchronized public void reset() { + resetLatency(); + resetRequestCounters(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/SessionTracker.java b/src/java/main/org/apache/zookeeper/server/SessionTracker.java new file mode 100644 index 0000000..3535e1b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/SessionTracker.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.PrintWriter; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; + +/** + * 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 { + public static interface Session { + long getSessionId(); + int getTimeout(); + boolean isClosing(); + } + public static interface SessionExpirer { + void expire(Session session); + + long getServerId(); + } + + long createSession(int sessionTimeout); + + void addSession(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); + + void checkSession(long sessionId, Object owner) throws KeeperException.SessionExpiredException, 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); +} diff --git a/src/java/main/org/apache/zookeeper/server/SessionTrackerImpl.java b/src/java/main/org/apache/zookeeper/server/SessionTrackerImpl.java new file mode 100644 index 0000000..cdaaf2b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/SessionTrackerImpl.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; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.common.Time; + +/** + * 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); + + HashMap sessionsById = new HashMap(); + + HashMap sessionSets = new HashMap(); + + ConcurrentHashMap sessionsWithTimeout; + long nextSessionId = 0; + long nextExpirationTime; + + int expirationInterval; + + public static class SessionImpl implements Session { + SessionImpl(long sessionId, int timeout, long expireTime) { + this.sessionId = sessionId; + this.timeout = timeout; + this.tickTime = expireTime; + isClosing = false; + } + + final long sessionId; + final int timeout; + long tickTime; + boolean isClosing; + + Object owner; + + public long getSessionId() { return sessionId; } + public int getTimeout() { return timeout; } + public boolean isClosing() { return isClosing; } + } + + public static long initializeNextSession(long id) { + long nextSid = 0; + nextSid = (Time.currentElapsedTime() << 24) >>> 8; + nextSid = nextSid | (id <<56); + return nextSid; + } + + static class SessionSet { + HashSet sessions = new HashSet(); + } + + SessionExpirer expirer; + + private long roundToInterval(long time) { + // We give a one interval grace period + return (time / expirationInterval + 1) * expirationInterval; + } + + public SessionTrackerImpl(SessionExpirer expirer, + ConcurrentHashMap sessionsWithTimeout, int tickTime, + long sid, ZooKeeperServerListener listener) + { + super("SessionTracker", listener); + this.expirer = expirer; + this.expirationInterval = tickTime; + this.sessionsWithTimeout = sessionsWithTimeout; + nextExpirationTime = roundToInterval(Time.currentElapsedTime()); + this.nextSessionId = initializeNextSession(sid); + for (Entry e : sessionsWithTimeout.entrySet()) { + addSession(e.getKey(), e.getValue()); + } + } + + volatile boolean running = true; + + volatile long currentTime; + + synchronized public void dumpSessions(PrintWriter pwriter) { + pwriter.print("Session Sets ("); + pwriter.print(sessionSets.size()); + pwriter.println("):"); + ArrayList keys = new ArrayList(sessionSets.keySet()); + Collections.sort(keys); + for (long time : keys) { + pwriter.print(sessionSets.get(time).sessions.size()); + pwriter.print(" expire at "); + pwriter.print(new Date(time)); + pwriter.println(":"); + for (SessionImpl s : sessionSets.get(time).sessions) { + pwriter.print("\t0x"); + pwriter.println(Long.toHexString(s.sessionId)); + } + } + } + + @Override + synchronized public String toString() { + StringWriter sw = new StringWriter(); + PrintWriter pwriter = new PrintWriter(sw); + dumpSessions(pwriter); + pwriter.flush(); + pwriter.close(); + return sw.toString(); + } + + @Override + synchronized public void run() { + try { + while (running) { + currentTime = Time.currentElapsedTime(); + if (nextExpirationTime > currentTime) { + this.wait(nextExpirationTime - currentTime); + continue; + } + SessionSet set; + set = sessionSets.remove(nextExpirationTime); + if (set != null) { + for (SessionImpl s : set.sessions) { + setSessionClosing(s.sessionId); + expirer.expire(s); + } + } + nextExpirationTime += expirationInterval; + } + } catch (InterruptedException e) { + handleException(this.getName(), e); + } + LOG.info("SessionTrackerImpl exited loop!"); + } + + synchronized public boolean touchSession(long sessionId, int timeout) { + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, + ZooTrace.CLIENT_PING_TRACE_MASK, + "SessionTrackerImpl --- Touch session: 0x" + + Long.toHexString(sessionId) + " with timeout " + timeout); + } + SessionImpl s = sessionsById.get(sessionId); + // Return false, if the session doesn't exists or marked as closing + if (s == null || s.isClosing()) { + return false; + } + long expireTime = roundToInterval(Time.currentElapsedTime() + timeout); + if (s.tickTime >= expireTime) { + // Nothing needs to be done + return true; + } + SessionSet set = sessionSets.get(s.tickTime); + if (set != null) { + set.sessions.remove(s); + } + s.tickTime = expireTime; + set = sessionSets.get(s.tickTime); + if (set == null) { + set = new SessionSet(); + sessionSets.put(expireTime, set); + } + set.sessions.add(s); + return true; + } + + synchronized public void setSessionClosing(long sessionId) { + if (LOG.isTraceEnabled()) { + LOG.info("Session closing: 0x" + Long.toHexString(sessionId)); + } + SessionImpl s = sessionsById.get(sessionId); + if (s == null) { + return; + } + s.isClosing = true; + } + + synchronized public void removeSession(long 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) { + SessionSet set = sessionSets.get(s.tickTime); + // Session expiration has been removing the sessions + if(set != null){ + set.sessions.remove(s); + } + } + } + + public void shutdown() { + LOG.info("Shutting down"); + + running = false; + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.getTextTraceLevel(), + "Shutdown SessionTrackerImpl!"); + } + } + + + synchronized public long createSession(int sessionTimeout) { + addSession(nextSessionId, sessionTimeout); + return nextSessionId++; + } + + synchronized public void addSession(long id, int sessionTimeout) { + sessionsWithTimeout.put(id, sessionTimeout); + if (sessionsById.get(id) == null) { + SessionImpl s = new SessionImpl(id, sessionTimeout, 0); + sessionsById.put(id, s); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "SessionTrackerImpl --- Adding session 0x" + + Long.toHexString(id) + " " + sessionTimeout); + } + } else { + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "SessionTrackerImpl --- Existing session 0x" + + Long.toHexString(id) + " " + sessionTimeout); + } + } + touchSession(id, sessionTimeout); + } + + synchronized public void checkSession(long sessionId, Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException { + SessionImpl session = sessionsById.get(sessionId); + if (session == null || session.isClosing()) { + throw new KeeperException.SessionExpiredException(); + } + if (session.owner == null) { + session.owner = owner; + } else if (session.owner != owner) { + throw new KeeperException.SessionMovedException(); + } + } + + synchronized public 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; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java b/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.java new file mode 100644 index 0000000..1b131a3 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/SnapshotFormatter.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.server; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.zip.Adler32; +import java.util.zip.CheckedInputStream; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.server.persistence.FileSnap; + +/** + * Dump a snapshot file to stdout. + */ +@InterfaceAudience.Public +public class SnapshotFormatter { + + /** + * USAGE: SnapshotFormatter snapshot_file + */ + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println("USAGE: SnapshotFormatter snapshot_file"); + System.exit(2); + } + + new SnapshotFormatter().run(args[0]); + } + + public void run(String snapshotFileName) throws IOException { + InputStream is = new CheckedInputStream( + new BufferedInputStream(new FileInputStream(snapshotFileName)), + new Adler32()); + InputArchive ia = BinaryInputArchive.getArchive(is); + + FileSnap fileSnap = new FileSnap(null); + + DataTree dataTree = new DataTree(); + Map sessions = new HashMap(); + + fileSnap.deserialize(dataTree, sessions, ia); + + printDetails(dataTree, sessions); + } + + private void printDetails(DataTree dataTree, Map sessions) { + printZnodeDetails(dataTree); + printSessionDetails(dataTree, sessions); + } + + private void printZnodeDetails(DataTree dataTree) { + System.out.println(String.format("ZNode Details (count=%d):", + dataTree.getNodeCount())); + + printZnode(dataTree, "/"); + System.out.println("----"); + } + + private void printZnode(DataTree dataTree, String name) { + System.out.println("----"); + DataNode n = dataTree.getNode(name); + Set children; + synchronized(n) { // keep findbugs happy + System.out.println(name); + printStat(n.stat); + if (n.data != null) { + System.out.println(" dataLength = " + n.data.length); + } else { + System.out.println(" no data"); + } + children = n.getChildren(); + } + for (String child : children) { + printZnode(dataTree, name + (name.equals("/") ? "" : "/") + child); + } + } + + 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)); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/Stats.java b/src/java/main/org/apache/zookeeper/server/Stats.java new file mode 100644 index 0000000..e2584ee --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/Stats.java @@ -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. + */ + +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/src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.java new file mode 100644 index 0000000..317b4ac --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.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 java.io.Flushable; +import java.io.IOException; +import java.util.LinkedList; +import java.util.Random; +import java.util.concurrent.LinkedBlockingQueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This RequestProcessor logs requests to disk. It batches the requests to do + * the io efficiently. The request is not passed to the next RequestProcessor + * until its log has been synced to disk. + * + * SyncRequestProcessor is used in 3 different cases + * 1. Leader - Sync request to disk and forward it to AckRequestProcessor which + * send ack back to itself. + * 2. Follower - Sync request to disk and forward request to + * SendAckRequestProcessor which send the packets to leader. + * SendAckRequestProcessor is flushable which allow us to force + * push packets to leader. + * 3. Observer - Sync committed request to disk (received as INFORM packet). + * It never send ack back to the leader, so the nextProcessor will + * be null. 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 final ZooKeeperServer zks; + private final LinkedBlockingQueue queuedRequests = + new LinkedBlockingQueue(); + private final RequestProcessor nextProcessor; + + private Thread snapInProcess = null; + volatile private boolean running; + + /** + * 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 LinkedList toFlush = new LinkedList(); + private final Random r = new Random(System.nanoTime()); + /** + * The number of log entries to log before starting a snapshot + */ + private static int snapCount = ZooKeeperServer.getSnapCount(); + + /** + * The number of log entries before rolling the log, number + * is chosen randomly + */ + private static int randRoll; + + private final Request requestOfDeath = Request.requestOfDeath; + + public SyncRequestProcessor(ZooKeeperServer zks, + RequestProcessor nextProcessor) { + super("SyncThread:" + zks.getServerId(), zks + .getZooKeeperServerListener()); + this.zks = zks; + this.nextProcessor = nextProcessor; + running = true; + } + + /** + * used by tests to check for changing + * snapcounts + * @param count + */ + public static void setSnapCount(int count) { + snapCount = count; + randRoll = count; + } + + /** + * used by tests to get the snapcount + * @return the snapcount + */ + public static int getSnapCount() { + return snapCount; + } + + /** + * Sets the value of randRoll. This method + * is here to avoid a findbugs warning for + * setting a static variable in an instance + * method. + * + * @param roll + */ + private static void setRandRoll(int roll) { + randRoll = roll; + } + + @Override + public void run() { + try { + int logCount = 0; + + // we do this in an attempt to ensure that not all of the servers + // in the ensemble take a snapshot at the same time + setRandRoll(r.nextInt(snapCount/2)); + while (true) { + Request si = null; + if (toFlush.isEmpty()) { + si = queuedRequests.take(); + } else { + si = queuedRequests.poll(); + if (si == null) { + flush(toFlush); + continue; + } + } + if (si == requestOfDeath) { + break; + } + if (si != null) { + // track the number of records written to the log + if (zks.getZKDatabase().append(si)) { + logCount++; + if (logCount > (snapCount / 2 + randRoll)) { + setRandRoll(r.nextInt(snapCount/2)); + // roll the log + zks.getZKDatabase().rollLog(); + // take a snapshot + if (snapInProcess != null && snapInProcess.isAlive()) { + LOG.warn("Too busy to snap, skipping"); + } else { + snapInProcess = new ZooKeeperThread("Snapshot Thread") { + public void run() { + try { + zks.takeSnapshot(); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + } + } + }; + snapInProcess.start(); + } + logCount = 0; + } + } 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; + } + toFlush.add(si); + if (toFlush.size() > 1000) { + flush(toFlush); + } + } + } + } catch (Throwable t) { + handleException(this.getName(), t); + running = false; + } + LOG.info("SyncRequestProcessor exited!"); + } + + private void flush(LinkedList toFlush) + throws IOException, RequestProcessorException + { + if (toFlush.isEmpty()) + return; + + zks.getZKDatabase().commit(); + while (!toFlush.isEmpty()) { + Request i = toFlush.remove(); + if (nextProcessor != null) { + nextProcessor.processRequest(i); + } + } + if (nextProcessor != null && nextProcessor instanceof Flushable) { + ((Flushable)nextProcessor).flush(); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + queuedRequests.add(requestOfDeath); + try { + if(running){ + this.join(); + } + if (!toFlush.isEmpty()) { + flush(toFlush); + } + } catch(InterruptedException e) { + LOG.warn("Interrupted while wating for " + this + " to finish"); + } 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(Request request) { + // request.addRQRec(">sync"); + queuedRequests.add(request); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/TraceFormatter.java b/src/java/main/org/apache/zookeeper/server/TraceFormatter.java new file mode 100644 index 0000000..66cc31c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/TraceFormatter.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; + +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; + +public class TraceFormatter { + + 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 "getDate"; + case OpCode.setData: + return "setData"; + case OpCode.multi: + return "multi"; + case OpCode.getACL: + return "getACL"; + case OpCode.setACL: + return "setACL"; + case OpCode.getChildren: + return "getChildren"; + case OpCode.getChildren2: + return "getChildren2"; + case OpCode.ping: + return "ping"; + case OpCode.createSession: + return "createSession"; + case OpCode.closeSession: + return "closeSession"; + case OpCode.error: + return "error"; + default: + return "unknown " + op; + } + } + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("USAGE: TraceFormatter trace_file"); + System.exit(2); + } + 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=" + + op2String(type) + + " zxid=0x" + + Long.toHexString(zxid) + + " txnType=" + + txnType + + " len=" + + len + " path=" + path); + } + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/UnimplementedRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/UnimplementedRequestProcessor.java new file mode 100644 index 0000000..aa58e63 --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/zookeeper/server/WatchManager.java b/src/java/main/org/apache/zookeeper/server/WatchManager.java new file mode 100644 index 0000000..76e37f2 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/WatchManager.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 java.io.PrintWriter; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; + +/** + * 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 { + private static final Logger LOG = LoggerFactory.getLogger(WatchManager.class); + + private final HashMap> watchTable = + new HashMap>(); + + private final HashMap> watch2Paths = + new HashMap>(); + + public synchronized int size(){ + int result = 0; + for(Set watches : watchTable.values()) { + result += watches.size(); + } + return result; + } + + public synchronized void addWatch(String path, Watcher watcher) { + HashSet 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); + + HashSet paths = watch2Paths.get(watcher); + if (paths == null) { + // cnxns typically have many watches, so use default cap here + paths = new HashSet(); + watch2Paths.put(watcher, paths); + } + paths.add(path); + } + + public synchronized void removeWatcher(Watcher watcher) { + HashSet paths = watch2Paths.remove(watcher); + if (paths == null) { + return; + } + for (String p : paths) { + HashSet list = watchTable.get(p); + if (list != null) { + list.remove(watcher); + if (list.size() == 0) { + watchTable.remove(p); + } + } + } + } + + public Set triggerWatch(String path, EventType type) { + return triggerWatch(path, type, null); + } + + public Set triggerWatch(String path, EventType type, Set supress) { + WatchedEvent e = new WatchedEvent(type, + KeeperState.SyncConnected, path); + HashSet watchers; + synchronized (this) { + watchers = watchTable.remove(path); + if (watchers == null || watchers.isEmpty()) { + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, + ZooTrace.EVENT_DELIVERY_TRACE_MASK, + "No watchers for " + path); + } + return null; + } + for (Watcher w : watchers) { + HashSet paths = watch2Paths.get(w); + if (paths != null) { + paths.remove(path); + } + } + } + for (Watcher w : watchers) { + if (supress != null && supress.contains(w)) { + continue; + } + w.process(e); + } + return watchers; + } + + /** + * Brief description of this object. + */ + @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 (HashSet paths : watch2Paths.values()) { + total += paths.size(); + } + sb.append("Total watches:").append(total); + + return sb.toString(); + } + + /** + * String representation of watches. Warning, may be large! + * @param byPath iff true output watches by paths, otw output + * watches by connection + * @return string representation of watches + */ + 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); + } + } + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ZKDatabase.java b/src/java/main/org/apache/zookeeper/server/ZKDatabase.java new file mode 100644 index 0000000..75f4048 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZKDatabase.java @@ -0,0 +1,499 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; +import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.Watcher; +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.quorum.Leader; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPacket; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * 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 ConcurrentHashMap sessionsWithTimeouts; + protected FileTxnSnapLog snapLog; + protected long minCommittedLog, maxCommittedLog; + public static final int commitLogCount = 500; + protected static int commitLogBuffer = 700; + protected LinkedList committedLog = new LinkedList(); + protected ReentrantReadWriteLock logLock = new ReentrantReadWriteLock(); + volatile private boolean initialized = false; + + /** + * 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) { + dataTree = new DataTree(); + sessionsWithTimeouts = new ConcurrentHashMap(); + this.snapLog = snapLog; + } + + /** + * 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 = new DataTree(); + 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 LinkedList getCommittedLog() { + ReadLock rl = logLock.readLock(); + // only make a copy if this thread isn't already holding a lock + if(logLock.getReadHoldCount() <=0) { + try { + rl.lock(); + return new LinkedList(this.committedLog); + } finally { + rl.unlock(); + } + } + return this.committedLog; + } + + /** + * get the last processed zxid from a datatree + * @return the last processed zxid of a datatree + */ + public long getDataTreeLastProcessedZxid() { + return dataTree.lastProcessedZxid; + } + + /** + * set the datatree initialized or not + * @param b set the datatree initialized to b + */ + public void setDataTreeInit(boolean b) { + dataTree.initialized = b; + } + + /** + * return the sessions in the datatree + * @return the data tree sessions + */ + public Collection getSessions() { + return dataTree.getSessions(); + } + + /** + * 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){ + addCommittedProposal(hdr, txn); + } + }; + + /** + * load the database from the disk onto memory and also add + * the transactions to the committedlog in memory. + * @return the last valid zxid on disk + * @throws IOException + */ + public long loadDataBase() throws IOException { + long zxid = snapLog.restore(dataTree, sessionsWithTimeouts, commitProposalPlaybackListener); + initialized = true; + 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) { + Request r = new Request(null, 0, hdr.getCxid(), hdr.getType(), null, null); + r.txn = txn; + r.hdr = hdr; + r.zxid = hdr.getZxid(); + 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.removeFirst(); + minCommittedLog = committedLog.getFirst().packet.getZxid(); + } + if (committedLog.size() == 0) { + minCommittedLog = request.zxid; + maxCommittedLog = request.zxid; + } + + // + byte[] data = SerializeUtils.serializeRequest(request); + QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, data, null); + Proposal p = new Proposal(); + p.packet = pp; + p.request = request; + committedLog.add(p); + maxCommittedLog = p.packet.getZxid(); + } finally { + wl.unlock(); + } + } + + + 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); + } + + /** + * 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 HashSet 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 + * @param hdr the txnheader for the txn + * @param txn the transaction that needs to be processed + * @return the result of processing the transaction on this + * datatree/zkdatabase + */ + public ProcessTxnResult processTxn(TxnHeader hdr, Record txn) { + return dataTree.processTxn(hdr, txn); + } + + /** + * 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 { + 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 watcher the watcher function + */ + public void setWatches(long relativeZxid, List dataWatches, + List existWatches, List childWatches, Watcher watcher) { + dataTree.setWatches(relativeZxid, dataWatches, existWatches, childWatches, watcher); + } + + /** + * 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); + } + + /** + * 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(); + + // 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 { + return this.snapLog.append(si); + } + + /** + * roll the underlying log + */ + public void rollLog() throws IOException { + this.snapLog.rollLog(); + } + + /** + * 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(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperCriticalThread.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperCriticalThread.java new file mode 100644 index 0000000..c8de809 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperCriticalThread.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; + +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); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperSaslServer.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperSaslServer.java new file mode 100644 index 0000000..dd6ee8f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperSaslServer.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; + +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"; + + 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/src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java new file mode 100644 index 0000000..6e7dd2b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java @@ -0,0 +1,1104 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +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.ZooDefs.OpCode; +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.proto.AuthPacket; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.ConnectResponse; +import org.apache.zookeeper.proto.GetSASLRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +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.AuthenticationProvider; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.TxnHeader; +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; + + static { + LOG = LoggerFactory.getLogger(ZooKeeperServer.class); + + Environment.logEnv("Server environment:", LOG); + } + + protected ZooKeeperServerBean jmxServerBean; + protected DataTreeBean jmxDataTreeBean; + + + /** + * The server delegates loading of the tree to an instance of the interface + */ + public interface DataTreeBuilder { + public DataTree build(); + } + + static public class BasicDataTreeBuilder implements DataTreeBuilder { + public DataTree build() { + return new DataTree(); + } + } + + 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; + protected SessionTracker sessionTracker; + private FileTxnSnapLog txnLogFactory = null; + private ZKDatabase zkDb; + private final AtomicLong hzxid = new AtomicLong(0); + public final static Exception ok = new Exception("No prob"); + protected RequestProcessor firstProcessor; + protected volatile State state = State.INITIAL; + + protected enum State { + INITIAL, RUNNING, SHUTDOWN, ERROR; + } + + /** + * This is the secret that we use to generate passwords, for the moment it + * is more of a sanity check. + */ + static final private long superSecret = 0XB3415C00L; + + private final AtomicInteger requestsInProcess = new AtomicInteger(0); + final List outstandingChanges = new ArrayList(); + // this data structure must be accessed under the outstandingChanges lock + final HashMap outstandingChangesForPath = + new HashMap(); + + private ServerCnxnFactory serverCnxnFactory; + + private final ServerStats serverStats; + private final ZooKeeperServerListener listener; + private ZooKeeperServerShutdownHandler zkShutdownHandler; + + 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...) + * + * @throws IOException + */ + public ZooKeeperServer() { + serverStats = new ServerStats(this); + listener = new ZooKeeperServerListenerImpl(this); + } + + /** + * Creates a ZooKeeperServer instance. It sets everything up, but doesn't + * actually start listening for clients until run() is invoked. + * + * @param dataDir the directory to put the data + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, + int minSessionTimeout, int maxSessionTimeout, + DataTreeBuilder treeBuilder, ZKDatabase zkDb) { + serverStats = new ServerStats(this); + this.txnLogFactory = txnLogFactory; + this.txnLogFactory.setServerStats(this.serverStats); + this.zkDb = zkDb; + this.tickTime = tickTime; + this.minSessionTimeout = minSessionTimeout; + this.maxSessionTimeout = maxSessionTimeout; + + listener = new ZooKeeperServerListenerImpl(this); + + LOG.info("Created server with tickTime " + tickTime + + " minSessionTimeout " + getMinSessionTimeout() + + " maxSessionTimeout " + getMaxSessionTimeout() + + " datadir " + txnLogFactory.getDataDir() + + " snapdir " + txnLogFactory.getSnapDir()); + } + + /** + * creates a zookeeperserver instance. + * @param txnLogFactory the file transaction snapshot logging class + * @param tickTime the ticktime for the server + * @param treeBuilder the datatree builder + * @throws IOException + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, + DataTreeBuilder treeBuilder) throws IOException { + this(txnLogFactory, tickTime, -1, -1, treeBuilder, + new ZKDatabase(txnLogFactory)); + } + + public ServerStats serverStats() { + return serverStats; + } + + public void dumpConf(PrintWriter pwriter) { + pwriter.print("clientPort="); + pwriter.println(getClientPort()); + pwriter.print("dataDir="); + pwriter.println(zkDb.snapLog.getSnapDir().getAbsolutePath()); + pwriter.print("dataLogDir="); + pwriter.println(zkDb.snapLog.getDataDir().getAbsolutePath()); + pwriter.print("tickTime="); + pwriter.println(getTickTime()); + pwriter.print("maxClientCnxns="); + pwriter.println(serverCnxnFactory.getMaxClientCnxnsPerHost()); + pwriter.print("minSessionTimeout="); + pwriter.println(getMinSessionTimeout()); + pwriter.print("maxSessionTimeout="); + pwriter.println(getMaxSessionTimeout()); + + pwriter.print("serverId="); + pwriter.println(getServerId()); + } + + /** + * 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, new BasicDataTreeBuilder()); + } + + /** + * Default constructor, relies on the config for its agrument values + * + * @throws IOException + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, + DataTreeBuilder treeBuilder) + throws IOException + { + this(txnLogFactory, DEFAULT_TICK_TIME, -1, -1, treeBuilder, + new ZKDatabase(txnLogFactory)); + } + + /** + * 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()); + } + + // Clean up dead sessions + LinkedList deadSessions = new LinkedList(); + for (Long session : zkDb.getSessions()) { + if (zkDb.getSessionWithTimeOuts().get(session) == null) { + deadSessions.add(session); + } + } + zkDb.setDataTreeInit(true); + for (long session : deadSessions) { + // XXX: Is lastProcessedZxid really the best thing to use? + killSession(session, zkDb.getDataTreeLastProcessedZxid()); + } + } + + public void takeSnapshot(){ + + try { + txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts()); + } 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 + System.exit(10); + } + } + + + /** + * This should be called from a synchronized block on this! + */ + public long getZxid() { + return hzxid.get(); + } + + long getNextZxid() { + return hzxid.incrementAndGet(); + } + + public void setZxid(long zxid) { + hzxid.set(zxid); + } + + private void close(long sessionId) { + submitRequest(null, sessionId, OpCode.closeSession, 0, null, null); + } + + 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" + Long.toHexString(sessionId) + + ", timeout of " + session.getTimeout() + "ms exceeded"); + close(sessionId); + } + + public static class MissingSessionException extends IOException { + private static final long serialVersionUID = 7467414635467261007L; + + public MissingSessionException(String msg) { + super(msg); + } + } + + void touch(ServerCnxn cnxn) throws MissingSessionException { + if (cnxn == null) { + return; + } + long id = cnxn.getSessionId(); + int to = cnxn.getSessionTimeout(); + 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() { + if (sessionTracker == null) { + createSessionTracker(); + } + startSessionTracker(); + setupRequestProcessors(); + + registerJMX(); + + setState(State.RUNNING); + notifyAll(); + } + + 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; + } + + protected void createSessionTracker() { + sessionTracker = new SessionTrackerImpl(this, zkDb.getSessionWithTimeOuts(), + tickTime, 1, getZooKeeperServerListener()); + } + + protected void startSessionTracker() { + ((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; + } + + 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()) { + 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); + // 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 (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(); + } + } + } + + 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(); + } + + public int getInProcess() { + return requestsInProcess.get(); + } + + /** + * This structure is used to facilitate information sharing between PrepRP + * and FinalRP. + */ + static class ChangeRecord { + 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 */ + + @SuppressWarnings("unchecked") + ChangeRecord duplicate(long zxid) { + StatPersisted stat = new StatPersisted(); + if (this.stat != null) { + DataTree.copyStatPersisted(this.stat, stat); + } + return new ChangeRecord(zxid, path, stat, childCount, + acl == null ? new ArrayList() : new ArrayList(acl)); + } + } + + 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) { + 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); + submitRequest(cnxn, sessionId, OpCode.createSession, 0, to, null); + 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)) { + finishSessionInit(cnxn, false); + } else { + revalidateSession(cnxn, sessionId, sessionTimeout); + } + } + + public void finishSessionInit(ServerCnxn cnxn, boolean valid) { + // register with JMX + try { + if (valid) { + serverCnxnFactory.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.info("Invalid session 0x" + + Long.toHexString(cnxn.getSessionId()) + + " for client " + + cnxn.getRemoteSocketAddress() + + ", probably expired"); + cnxn.sendBuffer(ServerCnxnFactory.closeConn); + } else { + LOG.info("Established session 0x" + + Long.toHexString(cnxn.getSessionId()) + + " with negotiated timeout " + cnxn.getSessionTimeout() + + " for client " + + cnxn.getRemoteSocketAddress()); + cnxn.enableRecv(); + } + + } catch (Exception e) { + LOG.warn("Exception while establishing session, closing", e); + cnxn.close(); + } + } + + public void closeSession(ServerCnxn cnxn, RequestHeader requestHeader) { + closeSession(cnxn.getSessionId()); + } + + public long getServerId() { + return 0; + } + + /** + * @param cnxn + * @param sessionId + * @param xid + * @param bb + */ + private void submitRequest(ServerCnxn cnxn, long sessionId, int type, + int xid, ByteBuffer bb, List authInfo) { + Request si = new Request(cnxn, sessionId, xid, type, bb, authInfo); + submitRequest(si); + } + + public void submitRequest(Request si) { + 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 { + touch(si.cnxn); + boolean validpacket = Request.isValid(si.type); + if (validpacket) { + firstProcessor.processRequest(si); + if (si.cnxn != null) { + incInProcess(); + } + } else { + LOG.warn("Received packet at server of unknown type " + si.type); + new UnimplementedRequestProcessor().processRequest(si); + } + } catch (MissingSessionException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("Dropping request: " + e.getMessage()); + } + } catch (RequestProcessorException e) { + LOG.error("Unable to process request:" + e.getMessage(), e); + } + } + + public static int getSnapCount() { + String sc = System.getProperty("zookeeper.snapCount"); + 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("zookeeper.globalOutstandingLimit"); + int limit; + try { + limit = Integer.parseInt(sc); + } catch (Exception e) { + limit = 1000; + } + return limit; + } + + public void setServerCnxnFactory(ServerCnxnFactory factory) { + serverCnxnFactory = factory; + } + + public ServerCnxnFactory getServerCnxnFactory() { + return serverCnxnFactory; + } + + /** + * 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(); + } + + /** + * 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 == -1 ? tickTime * 2 : minSessionTimeout; + } + + public void setMinSessionTimeout(int min) { + LOG.info("minSessionTimeout set to " + min); + this.minSessionTimeout = min; + } + + public int getMaxSessionTimeout() { + return maxSessionTimeout == -1 ? tickTime * 20 : maxSessionTimeout; + } + + public void setMaxSessionTimeout(int max) { + LOG.info("maxSessionTimeout set to " + max); + this.maxSessionTimeout = max; + } + + public int getClientPort() { + return serverCnxnFactory != null ? serverCnxnFactory.getLocalPort() : -1; + } + + public void setTxnLogFactory(FileTxnSnapLog txnLog) { + this.txnLogFactory = txnLog; + } + + public FileTxnSnapLog getTxnLogFactory() { + return this.txnLogFactory; + } + + public String getState() { + return "standalone"; + } + + public void dumpEphemerals(PrintWriter pwriter) { + zkDb.dumpEphemerals(pwriter); + } + + /** + * return the total number of client connections that are alive + * to this server + */ + public int getNumAliveConnections() { + return serverCnxnFactory.getNumAliveConnections(); + } + + public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException { + BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer)); + ConnectRequest connReq = new ConnectRequest(); + connReq.deserialize(bia, "connect"); + if (LOG.isDebugEnabled()) { + LOG.debug("Session establishment request from client " + + cnxn.getRemoteSocketAddress() + + " client's lastZxid is 0x" + + Long.toHexString(connReq.getLastZxidSeen())); + } + 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 " + + cnxn.getRemoteSocketAddress() + + "; will be dropped if server is in r-o mode"); + } + if (readOnly == false && this instanceof ReadOnlyZooKeeperServer) { + String msg = "Refusing session request for not-read-only client " + + cnxn.getRemoteSocketAddress(); + LOG.info(msg); + throw new CloseRequestException(msg); + } + 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); + } + 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(); + long sessionId = connReq.getSessionId(); + if (sessionId != 0) { + long clientSessionId = connReq.getSessionId(); + LOG.info("Client attempting to renew session 0x" + + Long.toHexString(clientSessionId) + + " at " + cnxn.getRemoteSocketAddress()); + serverCnxnFactory.closeSession(sessionId); + cnxn.setSessionId(sessionId); + reopenSession(cnxn, sessionId, passwd, sessionTimeout); + } else { + LOG.info("Client attempting to establish new session at " + + cnxn.getRemoteSocketAddress()); + createSession(cnxn, passwd, sessionTimeout); + } + } + + public boolean shouldThrottle(long outStandingCount) { + if (getGlobalOutstandingLimit() < getInProcess()) { + return outStandingCount > 0; + } + return false; + } + + public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException { + // 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"); + // Through the magic of byte buffers, txn will not be + // pointing + // to the start of the txn + incomingBuffer = incomingBuffer.slice(); + if (h.getType() == OpCode.auth) { + LOG.info("got auth packet " + cnxn.getRemoteSocketAddress()); + AuthPacket authPacket = new AuthPacket(); + ByteBufferInputStream.byteBuffer2Record(incomingBuffer, authPacket); + String scheme = authPacket.getScheme(); + AuthenticationProvider ap = ProviderRegistry.getProvider(scheme); + Code authReturn = KeeperException.Code.AUTHFAILED; + if(ap != null) { + try { + authReturn = ap.handleAuthentication(cnxn, authPacket.getAuth()); + } catch(RuntimeException e) { + LOG.warn("Caught runtime exception from AuthenticationProvider: " + scheme + " due to " + e); + authReturn = KeeperException.Code.AUTHFAILED; + } + } + if (authReturn!= KeeperException.Code.OK) { + if (ap == null) { + LOG.warn("No authentication provider for scheme: " + + scheme + " has " + + 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(); + } else { + if (LOG.isDebugEnabled()) { + 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); + } + return; + } else { + if (h.getType() == OpCode.sasl) { + Record rsp = processSasl(incomingBuffer,cnxn); + ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue()); + cnxn.sendResponse(rh,rsp, "response"); // not sure about 3rd arg..what is it? + return; + } + else { + Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), + h.getType(), incomingBuffer, cnxn.getAuthInfo()); + si.setOwner(ServerCnxn.me); + submitRequest(si); + } + } + cnxn.incrOutstandingRequests(h); + } + + private Record processSasl(ByteBuffer incomingBuffer, ServerCnxn cnxn) 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() == true) { + String authorizationID = saslServer.getAuthorizationID(); + LOG.info("adding SASL authorization for authorizationID: " + authorizationID); + cnxn.addAuthInfo(new Id("sasl",authorizationID)); + } + } + catch (SaslException e) { + LOG.warn("Client failed to SASL authenticate: " + e, e); + if ((System.getProperty("zookeeper.allowSaslFailedClients") != null) + && + (System.getProperty("zookeeper.allowSaslFailedClients").equals("true"))) { + LOG.warn("Maintaining client connection despite SASL authentication failure."); + } else { + LOG.warn("Closing client connection due to SASL authentication failure."); + cnxn.close(); + } + } + } + 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); + } + // wrap SASL response token to client inside a Response object. + return new SetSASLResponse(responseToken); + } + + public ProcessTxnResult processTxn(TxnHeader hdr, Record txn) { + ProcessTxnResult rc; + int opCode = hdr.getType(); + long sessionId = hdr.getClientId(); + rc = getZKDatabase().processTxn(hdr, txn); + if (opCode == OpCode.createSession) { + if (txn instanceof CreateSessionTxn) { + CreateSessionTxn cst = (CreateSessionTxn) txn; + sessionTracker.addSession(sessionId, cst + .getTimeOut()); + } else { + LOG.warn("*****>>>>> Got " + + txn.getClass() + " " + + txn.toString()); + } + } else if (opCode == OpCode.closeSession) { + sessionTracker.removeSession(sessionId); + } + return rc; + } + + /** + * 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; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperServerBean.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerBean.java new file mode 100644 index 0000000..cfd4711 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerBean.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; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Date; + +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * 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() { + try { + return InetAddress.getLocalHost().getHostAddress() + ":" + + zks.getClientPort(); + } catch (UnknownHostException e) { + return "localhost:" + 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 long 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() { + ServerCnxnFactory fac = zks.getServerCnxnFactory(); + if (fac == null) { + return -1; + } + return fac.getMaxClientCnxnsPerHost(); + } + + public void setMaxClientCnxnsPerHost(int max) { + // if fac is null the exception will be propagated to the client + zks.getServerCnxnFactory().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 getPacketsReceived() { + return zks.serverStats().getPacketsReceived(); + } + + public long getPacketsSent() { + return zks.serverStats().getPacketsSent(); + } + + public long getFsyncThresholdExceedCount() { + return zks.serverStats().getFsyncThresholdExceedCount(); + } + + public void resetLatency() { + zks.serverStats().resetLatency(); + } + + public void resetMaxLatency() { + zks.serverStats().resetMaxLatency(); + } + + public void resetFsyncThresholdExceedCount() { + zks.serverStats().resetFsyncThresholdExceedCount(); + } + + public void resetStatistics() { + ServerStats serverStats = zks.serverStats(); + serverStats.resetRequestCounters(); + serverStats.resetLatency(); + serverStats.resetFsyncThresholdExceedCount(); + } + + public long getNumAliveConnections() { + return zks.getNumAliveConnections(); + } + + @Override + public int getJuteMaxBufferSize() { + return BinaryInputArchive.maxBuffer; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListener.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListener.java new file mode 100644 index 0000000..b1c4c8c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListener.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; + +/** + * 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/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.java new file mode 100644 index 0000000..08f4934 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.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 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/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMXBean.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMXBean.java new file mode 100644 index 0000000..ee46d18 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMXBean.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; + +/** + * ZooKeeper server MBean. + */ +public interface ZooKeeperServerMXBean { + /** + * @return the server socket port number + */ + public String getClientPort(); + /** + * @return the zookeeper server version + */ + public String getVersion(); + /** + * @return time the server was started + */ + public String getStartTime(); + /** + * @return min request latency in ms + */ + public long getMinRequestLatency(); + /** + * @return average request latency in ms + */ + public long getAvgRequestLatency(); + /** + * @return max request latency in ms + */ + public long getMaxRequestLatency(); + /** + * @return number of packets received so far + */ + public long getPacketsReceived(); + /** + * @return number of packets sent so far + */ + public long getPacketsSent(); + + /** + * @return number of fsync threshold exceeds so far + */ + public long getFsyncThresholdExceedCount(); + /** + * @return number of outstanding requests. + */ + public long getOutstandingRequests(); + /** + * Current TickTime of server in milliseconds + */ + public int getTickTime(); + /** + * Set TickTime of server in milliseconds + */ + public void setTickTime(int tickTime); + + /** Current maxClientCnxns allowed from a particular host */ + public int getMaxClientCnxnsPerHost(); + + /** Set maxClientCnxns allowed from a particular host */ + public void setMaxClientCnxnsPerHost(int max); + + /** + * Current minSessionTimeout of the server in milliseconds + */ + public int getMinSessionTimeout(); + /** + * Set minSessionTimeout of server in milliseconds + */ + public void setMinSessionTimeout(int min); + + /** + * Current maxSessionTimeout of the server in milliseconds + */ + public int getMaxSessionTimeout(); + /** + * Set maxSessionTimeout of server in milliseconds + */ + public void setMaxSessionTimeout(int max); + + /** + * Reset packet and latency statistics + */ + public void resetStatistics(); + /** + * Reset min/avg/max latency statistics + */ + public void resetLatency(); + /** + * Reset max latency statistics only. + */ + public void resetMaxLatency(); + + /** + * Reset Fsync Threshold Exceed Count statistics only. + */ + public void resetFsyncThresholdExceedCount(); + /** + * @return number of alive client connections + */ + public long getNumAliveConnections(); + + /** + * + * @return Returns the value of the following config setting: jute.maxbuffer + */ + public int getJuteMaxBufferSize(); +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMain.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMain.java new file mode 100644 index 0000000..ef7621c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerMain.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 java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import javax.management.JMException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.jmx.ManagedUtil; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * 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]"; + + private ServerCnxnFactory cnxnFactory; + + /* + * 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); + System.exit(2); + } catch (ConfigException e) { + LOG.error("Invalid config, exiting abnormally", e); + System.err.println("Invalid config, exiting abnormally"); + System.exit(2); + } catch (Exception e) { + LOG.error("Unexpected exception, exiting abnormally", e); + System.exit(1); + } + LOG.info("Exiting normally"); + System.exit(0); + } + + protected void initializeAndRun(String[] args) + throws ConfigException, IOException + { + 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 + */ + public void runFromConfig(ServerConfig config) throws IOException { + LOG.info("Starting server"); + FileTxnSnapLog txnLog = null; + try { + // 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 + final ZooKeeperServer zkServer = new ZooKeeperServer(); + // 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)); + + txnLog = new FileTxnSnapLog(new File(config.dataLogDir), new File( + config.dataDir)); + txnLog.setServerStats(zkServer.serverStats()); + zkServer.setTxnLogFactory(txnLog); + zkServer.setTickTime(config.tickTime); + zkServer.setMinSessionTimeout(config.minSessionTimeout); + zkServer.setMaxSessionTimeout(config.maxSessionTimeout); + cnxnFactory = ServerCnxnFactory.createFactory(); + cnxnFactory.configure(config.getClientPortAddress(), + config.getMaxClientCnxns()); + cnxnFactory.startup(zkServer); + // 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(); + + cnxnFactory.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(); + } + } + } + + /** + * Shutdown the serving instance + */ + protected void shutdown() { + if (cnxnFactory != null) { + cnxnFactory.shutdown(); + } + } + + // VisibleForTesting + ServerCnxnFactory getCnxnFactory() { + return cnxnFactory; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.java new file mode 100644 index 0000000..499cacb --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.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; + +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/src/java/main/org/apache/zookeeper/server/ZooKeeperThread.java b/src/java/main/org/apache/zookeeper/server/ZooKeeperThread.java new file mode 100644 index 0000000..cf6cecf --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooKeeperThread.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.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(Runnable thread, String threadName) { + super(thread, threadName); + setUncaughtExceptionHandler(uncaughtExceptionalHandler); + } + + 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/src/java/main/org/apache/zookeeper/server/ZooTrace.java b/src/java/main/org/apache/zookeeper/server/ZooTrace.java new file mode 100644 index 0000000..946a4bf --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/ZooTrace.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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.quorum.LearnerHandler; +import org.apache.zookeeper.server.quorum.QuorumPacket; + +/** + * 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 { + final static public long CLIENT_REQUEST_TRACE_MASK = 1 << 1; + + final static public long CLIENT_DATA_PACKET_TRACE_MASK = 1 << 2; + + final static public long CLIENT_PING_TRACE_MASK = 1 << 3; + + final static public long SERVER_PACKET_TRACE_MASK = 1 << 4; + + final static public long SESSION_TRACE_MASK = 1 << 5; + + final static public long EVENT_DELIVERY_TRACE_MASK = 1 << 6; + + final static public long SERVER_PING_TRACE_MASK = 1 << 7; + + final static public long WARNING_TRACE_MASK = 1 << 8; + + final static public 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 long getTextTraceLevel() { + return traceMask; + } + + public static void setTextTraceLevel(long mask) { + traceMask = mask; + Logger LOG = LoggerFactory.getLogger(ZooTrace.class); + LOG.info("Set text trace mask to 0x" + Long.toHexString(mask)); + } + + public static 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); + } + } + + static public void logQuorumPacket(Logger log, long mask, + char direction, QuorumPacket qp) + { + if (isTraceEnabled(log, mask)) { + logTraceMessage(log, mask, direction + + " " + LearnerHandler.packetToString(qp)); + } + } + + static public 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/src/java/main/org/apache/zookeeper/server/auth/AuthenticationProvider.java b/src/java/main/org/apache/zookeeper/server/auth/AuthenticationProvider.java new file mode 100644 index 0000000..dd6e32f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/AuthenticationProvider.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.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); +} diff --git a/src/java/main/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java b/src/java/main/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java new file mode 100644 index 0000000..2427e7a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.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.auth; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; + +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:" to enable + * super user access (i.e. acls disabled) + */ + private final static String superDigest = System.getProperty( + "zookeeper.DigestAuthenticationProvider.superDigest"); + + public String getScheme() { + return "digest"; + } + + static final private 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(); + } + + static final private 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 ? '+' : '/'; + } + + static public 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); + } + + /** 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/src/java/main/org/apache/zookeeper/server/auth/DigestLoginModule.java b/src/java/main/org/apache/zookeeper/server/auth/DigestLoginModule.java new file mode 100644 index 0000000..955c728 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/DigestLoginModule.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.auth; + +import javax.security.auth.Subject; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.spi.LoginModule; +import java.util.Map; + +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((Object)username); + String password = (String)options.get("password"); + this.subject.getPrivateCredentials().add((Object)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/src/java/main/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java b/src/java/main/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java new file mode 100644 index 0000000..192dc06 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/IPAuthenticationProvider.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.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) { + return addr2Bytes(id) != null; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/auth/KerberosName.java b/src/java/main/org/apache/zookeeper/server/auth/KerberosName.java new file mode 100644 index 0000000..01d1f66 --- /dev/null +++ b/src/java/main/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("index " + num + " from " + format + + " is outside of the valid range 0 to " + + (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. + * @param conf the new configuration + * @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/src/java/main/org/apache/zookeeper/server/auth/ProviderRegistry.java b/src/java/main/org/apache/zookeeper/server/auth/ProviderRegistry.java new file mode 100644 index 0000000..855971b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/ProviderRegistry.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.auth; + +import java.util.Enumeration; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.ZooKeeperServer; + +public class ProviderRegistry { + private static final Logger LOG = LoggerFactory.getLogger(ProviderRegistry.class); + + private static boolean initialized = false; + private static HashMap authenticationProviders = + new HashMap(); + + public static void initialize() { + synchronized (ProviderRegistry.class) { + if (initialized) + return; + 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("zookeeper.authProvider.")) { + 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 AuthenticationProvider getProvider(String scheme) { + if(!initialized) + initialize(); + return authenticationProviders.get(scheme); + } + + public static String listProviders() { + StringBuilder sb = new StringBuilder(); + for(String s: authenticationProviders.keySet()) { + sb.append(s + " "); +} + return sb.toString(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.java b/src/java/main/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.java new file mode 100644 index 0000000..063304f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.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.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 (System.getProperty("zookeeper.superUser") != null) { + return (id.equals(System.getProperty("zookeeper.superUser")) || id.equals(aclExpr)); + } + return (id.equals("super") || id.equals(aclExpr)); + } + + 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/src/java/main/org/apache/zookeeper/server/auth/SaslServerCallbackHandler.java b/src/java/main/org/apache/zookeeper/server/auth/SaslServerCallbackHandler.java new file mode 100644 index 0000000..9f53a4d --- /dev/null +++ b/src/java/main/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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +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 '" + nc.getDefaultName() + "' not found in list of DIGEST-MD5 authenticateable users."); + 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=" + authenticationID + + "; authorizationID=" + 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/src/java/main/org/apache/zookeeper/server/package.html b/src/java/main/org/apache/zookeeper/server/package.html new file mode 100644 index 0000000..89a5d7e --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/zookeeper/server/persistence/FilePadding.java b/src/java/main/org/apache/zookeeper/server/persistence/FilePadding.java new file mode 100644 index 0000000..49fde24 --- /dev/null +++ b/src/java/main/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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +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(size + " is not a valid value for preAllocSize"); + } + } + } + + 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. + * @throws IOException + */ + // 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/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java new file mode 100644 index 0000000..1687277 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java @@ -0,0 +1,255 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.zip.Adler32; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.util.SerializeUtils; + +/** + * 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; + 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 final static int SNAP_MAGIC + = ByteBuffer.wrap("ZKSN".getBytes()).getInt(); + + public static final String SNAPSHOT_FILE_PREFIX = "snapshot"; + + public FileSnap(File snapDir) { + this.snapDir = snapDir; + } + + /** + * 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; + boolean foundValid = false; + for (int i = 0; i < snapList.size(); i++) { + snap = snapList.get(i); + InputStream snapIS = null; + CheckedInputStream crcIn = null; + try { + LOG.info("Reading snapshot " + snap); + snapIS = new BufferedInputStream(new FileInputStream(snap)); + crcIn = new CheckedInputStream(snapIS, new Adler32()); + InputArchive ia = BinaryInputArchive.getArchive(crcIn); + deserialize(dt,sessions, ia); + long checkSum = crcIn.getChecksum().getValue(); + long val = ia.readLong("val"); + if (val != checkSum) { + throw new IOException("CRC corruption in snapshot : " + snap); + } + foundValid = true; + break; + } catch(IOException e) { + LOG.warn("problem reading snap file " + snap, e); + } finally { + if (snapIS != null) + snapIS.close(); + if (crcIn != null) + crcIn.close(); + } + } + if (!foundValid) { + throw new IOException("Not able to find valid snapshots in " + snapDir); + } + dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), SNAPSHOT_FILE_PREFIX); + 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 { + FileHeader header = new FileHeader(); + header.deserialize(ia, "fileheader"); + if (header.getMagic() != SNAP_MAGIC) { + throw new IOException("mismatching magic headers " + + header.getMagic() + + " != " + FileSnap.SNAP_MAGIC); + } + 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 + */ + private List findNValidSnapshots(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) { + // we should catch the exceptions + // from the valid snapshot and continue + // until we find a valid one + try { + if (Util.isValidSnapshot(f)) { + list.add(f); + count++; + if (count == n) { + break; + } + } + } catch (IOException e) { + LOG.info("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 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 + */ + public synchronized void serialize(DataTree dt, Map sessions, File snapShot) + throws IOException { + if (!close) { + OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot)); + CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32()); + //CheckedOutputStream cout = new CheckedOutputStream() + OutputArchive oa = BinaryOutputArchive.getArchive(crcOut); + FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId); + serialize(dt,sessions,oa, header); + long val = crcOut.getChecksum().getValue(); + oa.writeLong(val, "val"); + oa.writeString("/", "path"); + sessOS.flush(); + crcOut.close(); + sessOS.close(); + } + } + + /** + * 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/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java new file mode 100644 index 0000000..b1cd006 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java @@ -0,0 +1,691 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.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.ArrayList; +import java.util.LinkedList; +import java.util.List; +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.ServerStats; +import org.apache.zookeeper.server.util.SerializeUtils; +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 { + private static final Logger LOG; + + public final static int TXNLOG_MAGIC = + ByteBuffer.wrap("ZKLG".getBytes()).getInt(); + + public final static int VERSION = 2; + + public static final String LOG_FILE_PREFIX = "log"; + + /** Maximum time we allow for elapsed fsync before WARNing */ + private final static long fsyncWarningThresholdMS; + + static { + LOG = LoggerFactory.getLogger(FileTxnLog.class); + + /** Local variable to read fsync.warningthresholdms into */ + Long fsyncWarningThreshold; + if ((fsyncWarningThreshold = Long.getLong("zookeeper.fsync.warningthresholdms")) == null) + fsyncWarningThreshold = Long.getLong("fsync.warningthresholdms", 1000); + fsyncWarningThresholdMS = fsyncWarningThreshold; + } + + 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 LinkedList streamsToFlush = + new LinkedList(); + File logFileWrite = null; + private FilePadding filePadding = new FilePadding(); + + private ServerStats serverStats; + + /** + * 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 void setServerStats(ServerStats serverStats) { + this.serverStats = serverStats; + } + + /** + * 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(); + this.logStream = null; + oa = null; + } + } + + /** + * 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 + { + if (hdr == null) { + return false; + } + + if (hdr.getZxid() <= lastZxidSeen) { + LOG.warn("Current zxid " + hdr.getZxid() + + " is <= " + lastZxidSeen + " for " + + hdr.getType()); + } else { + lastZxidSeen = hdr.getZxid(); + } + + if (logStream==null) { + if(LOG.isInfoEnabled()){ + 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); + 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) { + continue; + } + // 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 evertyhing 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(); + + log.getChannel().force(false); + + long syncElapsedMS = + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startSyncNS); + if (syncElapsedMS > fsyncWarningThresholdMS) { + if(serverStats != null) { + serverStats.incrementFsyncThresholdExceedCount(); + } + LOG.warn("fsync-ing the write ahead log in " + + Thread.currentThread().getName() + + " took " + syncElapsedMS + + "ms which will adversely effect operation latency. " + + "See the ZooKeeper troubleshooting guide"); + } + } + } + while (streamsToFlush.size() > 1) { + streamsToFlush.removeFirst().close(); + } + } + + /** + * 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 new FileTxnIterator(logDir, zxid); + } + + /** + * 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 fomr 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; + 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 + * @throws IOException + */ + public FileTxnIterator(File logDir, long zxid) throws IOException { + this.logDir = logDir; + this.zxid = zxid; + init(); + } + + /** + * 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); + } + // add the last logfile that is less than the zxid + else if (Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) < zxid) { + storedFiles.add(f); + break; + } + } + goToNextLog(); + if (!next()) + return; + while (hdr.getZxid() < zxid) { + if (!next()) + return; + } + } + + /** + * 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 ia input archive + * @param is file input stream associated with the input archive. + * @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); + hdr = new TxnHeader(); + record = SerializeUtils.deserializeTxn(bytes, hdr); + } catch (EOFException e) { + LOG.debug("EOF excepton " + 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; + } + + /** + * reutrn 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; + } + + /** + * close the iterator + * and release the resources. + */ + public void close() throws IOException { + if (inputStream != null) { + inputStream.close(); + } + } + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java new file mode 100644 index 0000000..77b946d --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java @@ -0,0 +1,428 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.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.server.DataTree; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.Request; +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.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a helper class + * above the implementations + * of txnlog and snapshot + * classes + */ +public class FileTxnSnapLog { + //the direcotry containing the + //the transaction logs + private final File dataDir; + //the directory containing the + //the snapshot directory + private final File snapDir; + private TxnLog txnLog; + private SnapShot snapLog; + public final static int VERSION = 2; + public final static String version = "version-"; + + private static final Logger LOG = LoggerFactory.getLogger(FileTxnSnapLog.class); + + /** + * 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); + } + + /** + * the constructor which takes the datadir and + * snapdir. + * @param dataDir the trasaction directory + * @param snapDir the snapshot directory + */ + public FileTxnSnapLog(File dataDir, File snapDir) throws IOException { + LOG.debug("Opening datadir:{} snapDir:{}", dataDir, snapDir); + + this.dataDir = new File(dataDir, version + VERSION); + this.snapDir = new File(snapDir, version + VERSION); + if (!this.dataDir.exists()) { + if (!this.dataDir.mkdirs()) { + throw new IOException("Unable to create data directory " + + this.dataDir); + } + } + if (!this.dataDir.canWrite()) { + throw new IOException("Cannot write to data directory " + this.dataDir); + } + + if (!this.snapDir.exists()) { + if (!this.snapDir.mkdirs()) { + throw new IOException("Unable to create snap directory " + + this.snapDir); + } + } + if (!this.snapDir.canWrite()) { + throw new IOException("Cannot write to snap directory " + this.snapDir); + } + + // 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(); + } + + txnLog = new FileTxnLog(this.dataDir); + snapLog = new FileSnap(this.snapDir); + } + + 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; + } + + /** + * this function restores the server + * database after reading from the + * snapshots and transaction logs + * @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 { + snapLog.deserialize(dt, sessions); + return fastForwardFromEdits(dt, sessions, listener); + } + + /** + * 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 + */ + public long fastForwardFromEdits(DataTree dt, Map sessions, + PlayBackListener listener) throws IOException { + FileTxnLog txnLog = new FileTxnLog(dataDir); + TxnIterator itr = txnLog.read(dt.lastProcessedZxid+1); + long highestZxid = dt.lastProcessedZxid; + TxnHeader hdr; + try { + while (true) { + // iterator points to + // the first valid txn when initialized + hdr = itr.getHeader(); + if (hdr == null) { + //empty logs + return dt.lastProcessedZxid; + } + if (hdr.getZxid() < highestZxid && highestZxid != 0) { + LOG.error("{}(higestZxid) > {}(next log) for type {}", + new Object[] { highestZxid, hdr.getZxid(), + hdr.getType() }); + } else { + highestZxid = hdr.getZxid(); + } + try { + processTransaction(hdr,dt,sessions, itr.getTxn()); + } catch(KeeperException.NoNodeException e) { + throw new IOException("Failed to process transaction type: " + + hdr.getType() + " error: " + e.getMessage(), e); + } + listener.onTxnLoaded(hdr, itr.getTxn()); + if (!itr.next()) + break; + } + } finally { + if (itr != null) { + itr.close(); + } + } + return highestZxid; + } + + /** + * 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; + 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; + 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:" + hdr.getType() + + ", error: " + rc.err + ", path: " + 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 sesssion timeouts to be + * serialized onto disk + * @throws IOException + */ + public void save(DataTree dataTree, + ConcurrentHashMap sessionsWithTimeouts) + throws IOException { + long lastZxid = dataTree.lastProcessedZxid; + File snapshotFile = new File(snapDir, Util.makeSnapshotName(lastZxid)); + LOG.info("Snapshotting: 0x{} to {}", Long.toHexString(lastZxid), + snapshotFile); + snapLog.serialize(dataTree, sessionsWithTimeouts, snapshotFile); + + } + + /** + * 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) throws IOException { + // close the existing txnLog and snapLog + close(); + + // truncate it + FileTxnLog truncLog = new FileTxnLog(dataDir); + boolean truncated = truncLog.truncate(zxid); + truncLog.close(); + + // 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; + } + + /** + * 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); + } + + /** + * 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 + * returns true iff something appended, otw false + * @throws IOException + */ + public boolean append(Request si) throws IOException { + return txnLog.append(si.hdr, si.txn); + } + + /** + * commit the transaction of logs + * @throws IOException + */ + public void commit() throws IOException { + txnLog.commit(); + } + + /** + * 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 { + txnLog.close(); + snapLog.close(); + } + + @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); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java b/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java new file mode 100644 index 0000000..c964afc --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.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.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. + */ +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 + * @throws IOException + */ + void serialize(DataTree dt, Map sessions, + File name) + throws IOException; + + /** + * find the most recent snapshot file + * @return the most recent snapshot file + * @throws IOException + */ + File findMostRecentSnapshot() throws IOException; + + /** + * free resources from this snapshot immediately + * @throws IOException + */ + void close() throws IOException; +} diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java new file mode 100644 index 0000000..141283f --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.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.server.persistence; + +import java.io.IOException; + +import org.apache.jute.Record; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * Interface for reading transaction logs. + * + */ +public interface TxnLog { + + /** + + * 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 + * returns true iff something appended, otw false + * @throws IOException + */ + boolean append(TxnHeader hdr, Record r) 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; + + /** + * commmit the trasaction and make sure + * they are persisted + * @throws IOException + */ + void commit() throws IOException; + + /** + * close the transactions logs + */ + void close() throws IOException; + /** + * an iterating interface for reading + * transaction logs. + */ + public interface TxnIterator { + /** + * return the transaction header. + * @return return the transaction header. + */ + TxnHeader getHeader(); + + /** + * return the transaction record. + * @return return the transaction record. + */ + Record getTxn(); + + /** + * go to the next transaction record. + * @throws IOException + */ + boolean next() throws IOException; + + /** + * close files and release the + * resources + * @throws IOException + */ + void close() throws IOException; + } +} + diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java new file mode 100644 index 0000000..8e3ddf8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java @@ -0,0 +1,273 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.TraceFormatter; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; + +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.text.DateFormat; +import java.util.Date; +import java.util.Scanner; +import java.util.zip.Adler32; +import java.util.zip.Checksum; + +import static org.apache.zookeeper.server.persistence.FileTxnLog.TXNLOG_MAGIC; +import static org.apache.zookeeper.server.persistence.TxnLogToolkitCliParser.printHelpAndExit; + +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; + + TxnLogToolkitParseException(int exitCode, String message, Object... params) { + super(exitCode, message, params); + } + } + + 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; + + /** + * @param args Command line arguments + */ + public static void main(String[] args) throws Exception { + final TxnLogToolkit lt = parseCommandLine(args); + try { + lt.dump(new Scanner(System.in)); + lt.printStat(); + } catch (TxnLogToolkitParseException e) { + System.err.println(e.getMessage() + "\n"); + printHelpAndExit(e.getExitCode()); + } catch (TxnLogToolkitException e) { + System.err.println(e.getMessage()); + System.exit(e.getExitCode()); + } finally { + lt.close(); + } + } + + public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileName, boolean force) + throws FileNotFoundException, TxnLogToolkitException { + this.recoveryMode = recoveryMode; + this.verbose = verbose; + this.force = force; + txnLogFile = new File(txnLogFileName); + if (!txnLogFile.exists() || !txnLogFile.canRead()) { + throw new TxnLogToolkitException(1, "File doesn't exist or not readable: %s", txnLogFile); + } + if (recoveryMode) { + recoveryLogFile = new File(txnLogFile.toString() + ".fixed"); + if (recoveryLogFile.exists()) { + throw new TxnLogToolkitException(1, "Recovery file %s already exists or not writable", recoveryLogFile); + } + } + + openTxnLogFile(); + if (recoveryMode) { + openRecoveryFile(); + } + } + + 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(2, "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(1, "Last transaction was partial."); + } + if (recoveryMode) { + filePadding.padFile(recoveryFos.getChannel()); + recoveryOa.writeLong(crcValue, "crcvalue"); + recoveryOa.writeBuffer(bytes, "txnEntry"); + recoveryOa.writeByte((byte)'B', "EOR"); + } + count++; + } + } + + 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(0, "Recovery aborted."); + } + } + } + + private void printTxn(byte[] bytes) throws IOException { + printTxn(bytes, ""); + } + + private void printTxn(byte[] bytes, String prefix) throws IOException { + TxnHeader hdr = new TxnHeader(); + Record txn = SerializeUtils.deserializeTxn(bytes, hdr); + 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()), + TraceFormatter.op2String(hdr.getType()), + txn); + if (prefix != null && !"".equals(prefix.trim())) { + System.out.print(prefix + " - "); + } + if (txns.endsWith("\n")) { + System.out.print(txns); + } else { + System.out.println(txns); + } + } + + 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 { + TxnLogToolkitCliParser parser = new TxnLogToolkitCliParser(); + parser.parse(args); + return new TxnLogToolkit(parser.isRecoveryMode(), parser.isVerbose(), parser.getTxnLogFileName(), parser.isForce()); + } + + 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/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParser.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParser.java new file mode 100644 index 0000000..094500a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParser.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.persistence; + +class TxnLogToolkitCliParser { + private String txnLogFileName; + private boolean recoveryMode; + private boolean verbose; + private boolean force; + + String getTxnLogFileName() { + return txnLogFileName; + } + + boolean isRecoveryMode() { + return recoveryMode; + } + + boolean isVerbose() { + return verbose; + } + + boolean isForce() { + return force; + } + + void parse(String[] args) throws TxnLogToolkit.TxnLogToolkitParseException { + if (args == null) { + throw new TxnLogToolkit.TxnLogToolkitParseException(1, "No arguments given"); + } + txnLogFileName = null; + for (String arg : args) { + if (arg.startsWith("--")) { + String par = arg.substring(2); + if ("help".equalsIgnoreCase(par)) { + printHelpAndExit(0); + } else if ("recover".equalsIgnoreCase(par)) { + recoveryMode = true; + } else if ("verbose".equalsIgnoreCase(par)) { + verbose = true; + } else if ("dump".equalsIgnoreCase(par)) { + recoveryMode = false; + } else if ("yes".equalsIgnoreCase(par)) { + force = true; + } else { + throw new TxnLogToolkit.TxnLogToolkitParseException(1, "Invalid argument: %s", par); + } + } else if (arg.startsWith("-")) { + String par = arg.substring(1); + if ("h".equalsIgnoreCase(par)) { + printHelpAndExit(0); + } else if ("r".equalsIgnoreCase(par)) { + recoveryMode = true; + } else if ("v".equalsIgnoreCase(par)) { + verbose = true; + } else if ("d".equalsIgnoreCase(par)) { + recoveryMode = false; + } else if ("y".equalsIgnoreCase(par)) { + force = true; + } else { + throw new TxnLogToolkit.TxnLogToolkitParseException(1, "Invalid argument: %s", par); + } + } else { + if (txnLogFileName != null) { + throw new TxnLogToolkit.TxnLogToolkitParseException(1, "Invalid arguments: more than one TXN log file given"); + } + txnLogFileName = arg; + } + } + + if (txnLogFileName == null) { + throw new TxnLogToolkit.TxnLogToolkitParseException(1, "Invalid arguments: TXN log file name missing"); + } + } + + static void printHelpAndExit(int exitCode) { + System.out.println("usage: TxnLogToolkit [-dhrvy] txn_log_file_name\n"); + System.out.println(" -d,--dump Dump mode. Dump all entries of the log file. (this is the default)"); + System.out.println(" -h,--help Print help message"); + System.out.println(" -r,--recover Recovery mode. Re-calculate CRC for broken entries."); + System.out.println(" -v,--verbose Be verbose in recovery mode: print all entries, not just fixed ones."); + System.out.println(" -y,--yes Non-interactive mode: repair all CRC errors without asking"); + System.exit(exitCode); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/persistence/Util.java b/src/java/main/org/apache/zookeeper/server/persistence/Util.java new file mode 100644 index 0000000..a774a24 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/persistence/Util.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.server.persistence; + +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.io.Serializable; +import java.net.URI; +import java.nio.ByteBuffer; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * 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 (extention) + * @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); + } + + /** + * 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} or {@link makeSnapshotName}. + * + * @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; + } + + /** + * 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 file that is not a snapshot is also + * an invalid snapshot. + * + * @param f file to verify + * @return true if the snapshot is valid + * @throws IOException + */ + public static boolean isValidSnapshot(File f) throws IOException { + if (f==null || Util.getZxidFromName(f.getName(), FileSnap.SNAPSHOT_FILE_PREFIX) == -1) + return false; + + // Check for a valid snapshot + RandomAccessFile raf = new RandomAccessFile(f, "r"); + try { + // including the header and the last / bytes + // the snapshot should be atleast 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 " + f + + " too short, len = " + readlen); + return false; + } + ByteBuffer bb = ByteBuffer.wrap(bytes); + int len = bb.getInt(); + byte b = bb.get(); + if (len != 1 || b != '/') { + LOG.info("Invalid snapshot " + f + " len = " + len + + " byte = " + (b & 0xff)); + return false; + } + } finally { + raf.close(); + } + + return true; + } + + /** + * 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 { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + + hdr.serialize(boa, "hdr"); + if (txn != null) { + txn.serialize(boa, "txn"); + } + return baos.toByteArray(); + } + + /** + * Write the serialized transaction record to the output archive. + * + * @param oa output archive + * @param bytes serialized trasnaction 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/src/java/main/org/apache/zookeeper/server/quorum/AckRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/AckRequestProcessor.java new file mode 100644 index 0000000..8e7e472 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/AckRequestProcessor.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.quorum; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; + + +/** + * 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) + leader.processAck(self.getId(), request.zxid, null); + else + LOG.error("Null QuorumPeer"); + } + + public void shutdown() { + // XXX No need to do anything + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/AuthFastLeaderElection.java b/src/java/main/org/apache/zookeeper/server/quorum/AuthFastLeaderElection.java new file mode 100644 index 0000000..d32a725 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/AuthFastLeaderElection.java @@ -0,0 +1,980 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Semaphore; + +import java.util.concurrent.TimeUnit; +import java.util.Random; + +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.quorum.Election; +import org.apache.zookeeper.server.quorum.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; + +/** + * @deprecated This class has been deprecated as of release 3.4.0. + */ +@Deprecated +public class AuthFastLeaderElection implements Election { + private static final Logger LOG = LoggerFactory.getLogger(AuthFastLeaderElection.class); + + /* Sequence numbers for messages */ + static int sequencer = 0; + static int maxTag = 0; + + /* + * Determine how much time a process has to wait once it believes that it + * has reached the end of leader election. + */ + static int finalizeWait = 100; + + /* + * Challenge counter to avoid replay attacks + */ + + static int challengeCounter = 0; + + /* + * Flag to determine whether to authenticate or not + */ + + private boolean authEnabled = false; + + static public class Notification { + /* + * Proposed leader + */ + long leader; + + /* + * zxid of the proposed leader + */ + long zxid; + + /* + * Epoch + */ + long epoch; + + /* + * current state of sender + */ + QuorumPeer.ServerState state; + + /* + * Address of the sender + */ + InetSocketAddress addr; + } + + /* + * Messages to send, both Notifications and Acks + */ + static public class ToSend { + static enum mType { + crequest, challenge, notification, ack + } + + ToSend(mType type, long tag, long leader, long zxid, long epoch, + ServerState state, InetSocketAddress addr) { + + switch (type) { + case crequest: + this.type = 0; + this.tag = tag; + this.leader = leader; + this.zxid = zxid; + this.epoch = epoch; + this.state = state; + this.addr = addr; + + break; + case challenge: + this.type = 1; + this.tag = tag; + this.leader = leader; + this.zxid = zxid; + this.epoch = epoch; + this.state = state; + this.addr = addr; + + break; + case notification: + this.type = 2; + this.leader = leader; + this.zxid = zxid; + this.epoch = epoch; + this.state = QuorumPeer.ServerState.LOOKING; + this.tag = tag; + this.addr = addr; + + break; + case ack: + this.type = 3; + this.tag = tag; + this.leader = leader; + this.zxid = zxid; + this.epoch = epoch; + this.state = state; + this.addr = addr; + + break; + default: + break; + } + } + + /* + * Message type: 0 notification, 1 acknowledgement + */ + int type; + + /* + * Proposed leader in the case of notification + */ + long leader; + + /* + * id contains the tag for acks, and zxid for notifications + */ + long zxid; + + /* + * Epoch + */ + long epoch; + + /* + * Current state; + */ + QuorumPeer.ServerState state; + + /* + * Message tag + */ + long tag; + + InetSocketAddress addr; + } + + LinkedBlockingQueue sendqueue; + + LinkedBlockingQueue recvqueue; + + private class Messenger { + + final DatagramSocket mySocket; + long lastProposedLeader; + long lastProposedZxid; + long lastEpoch; + final Set ackset; + final ConcurrentHashMap challengeMap; + final ConcurrentHashMap challengeMutex; + final ConcurrentHashMap ackMutex; + final ConcurrentHashMap> addrChallengeMap; + + class WorkerReceiver implements Runnable { + + DatagramSocket mySocket; + Messenger myMsg; + + WorkerReceiver(DatagramSocket s, Messenger msg) { + mySocket = s; + myMsg = msg; + } + + boolean saveChallenge(long tag, long challenge) { + Semaphore s = challengeMutex.get(tag); + if (s != null) { + synchronized (Messenger.this) { + challengeMap.put(tag, challenge); + challengeMutex.remove(tag); + } + + + s.release(); + } else { + LOG.error("No challenge mutex object"); + } + + + return true; + } + + public void run() { + byte responseBytes[] = new byte[48]; + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + DatagramPacket responsePacket = new DatagramPacket( + responseBytes, responseBytes.length); + while (true) { + // Sleeps on receive + try { + responseBuffer.clear(); + mySocket.receive(responsePacket); + } catch (IOException e) { + LOG.warn("Ignoring exception receiving", e); + } + // Receive new message + if (responsePacket.getLength() != responseBytes.length) { + LOG.warn("Got a short response: " + + responsePacket.getLength() + " " + + responsePacket.toString()); + continue; + } + responseBuffer.clear(); + int type = responseBuffer.getInt(); + if ((type > 3) || (type < 0)) { + LOG.warn("Got bad Msg type: " + type); + continue; + } + long tag = responseBuffer.getLong(); + + QuorumPeer.ServerState ackstate = QuorumPeer.ServerState.LOOKING; + switch (responseBuffer.getInt()) { + case 0: + ackstate = QuorumPeer.ServerState.LOOKING; + break; + case 1: + ackstate = QuorumPeer.ServerState.LEADING; + break; + case 2: + ackstate = QuorumPeer.ServerState.FOLLOWING; + break; + } + + Vote current = self.getCurrentVote(); + + switch (type) { + case 0: + // Receive challenge request + ToSend c = new ToSend(ToSend.mType.challenge, tag, + current.getId(), current.getZxid(), + logicalclock, self.getPeerState(), + (InetSocketAddress) responsePacket + .getSocketAddress()); + sendqueue.offer(c); + break; + case 1: + // Receive challenge and store somewhere else + long challenge = responseBuffer.getLong(); + saveChallenge(tag, challenge); + + break; + case 2: + Notification n = new Notification(); + n.leader = responseBuffer.getLong(); + n.zxid = responseBuffer.getLong(); + n.epoch = responseBuffer.getLong(); + n.state = ackstate; + n.addr = (InetSocketAddress) responsePacket + .getSocketAddress(); + + if ((myMsg.lastEpoch <= n.epoch) + && ((n.zxid > myMsg.lastProposedZxid) + || ((n.zxid == myMsg.lastProposedZxid) + && (n.leader > myMsg.lastProposedLeader)))) { + myMsg.lastProposedZxid = n.zxid; + myMsg.lastProposedLeader = n.leader; + myMsg.lastEpoch = n.epoch; + } + + long recChallenge; + InetSocketAddress addr = (InetSocketAddress) responsePacket + .getSocketAddress(); + if (authEnabled) { + ConcurrentHashMap tmpMap = addrChallengeMap.get(addr); + if(tmpMap != null){ + if (tmpMap.get(tag) != null) { + recChallenge = responseBuffer.getLong(); + + if (tmpMap.get(tag) == recChallenge) { + recvqueue.offer(n); + + ToSend a = new ToSend(ToSend.mType.ack, + tag, current.getId(), + current.getZxid(), + logicalclock, self.getPeerState(), + addr); + + sendqueue.offer(a); + } else { + LOG.warn("Incorrect challenge: " + + recChallenge + ", " + + addrChallengeMap.toString()); + } + } else { + LOG.warn("No challenge for host: " + addr + + " " + tag); + } + } + } else { + recvqueue.offer(n); + + ToSend a = new ToSend(ToSend.mType.ack, tag, + current.getId(), current.getZxid(), + logicalclock, self.getPeerState(), + (InetSocketAddress) responsePacket + .getSocketAddress()); + + sendqueue.offer(a); + } + break; + + // Upon reception of an ack message, remove it from the + // queue + case 3: + Semaphore s = ackMutex.get(tag); + + if(s != null) + s.release(); + else LOG.error("Empty ack semaphore"); + + ackset.add(tag); + + if (authEnabled) { + ConcurrentHashMap tmpMap = addrChallengeMap.get(responsePacket + .getSocketAddress()); + if(tmpMap != null) { + tmpMap.remove(tag); + } else { + LOG.warn("No such address in the ensemble configuration " + responsePacket + .getSocketAddress()); + } + } + + if (ackstate != QuorumPeer.ServerState.LOOKING) { + Notification outofsync = new Notification(); + outofsync.leader = responseBuffer.getLong(); + outofsync.zxid = responseBuffer.getLong(); + outofsync.epoch = responseBuffer.getLong(); + outofsync.state = ackstate; + outofsync.addr = (InetSocketAddress) responsePacket + .getSocketAddress(); + + recvqueue.offer(outofsync); + } + + break; + // Default case + default: + LOG.warn("Received message of incorrect type " + type); + break; + } + } + } + } + + class WorkerSender implements Runnable { + + Random rand; + int maxAttempts; + int ackWait = finalizeWait; + + /* + * Receives a socket and max number of attempts as input + */ + + WorkerSender(int attempts) { + maxAttempts = attempts; + rand = new Random(java.lang.Thread.currentThread().getId() + + Time.currentElapsedTime()); + } + + long genChallenge() { + byte buf[] = new byte[8]; + + buf[0] = (byte) ((challengeCounter & 0xff000000) >>> 24); + buf[1] = (byte) ((challengeCounter & 0x00ff0000) >>> 16); + buf[2] = (byte) ((challengeCounter & 0x0000ff00) >>> 8); + buf[3] = (byte) ((challengeCounter & 0x000000ff)); + + challengeCounter++; + int secret = rand.nextInt(java.lang.Integer.MAX_VALUE); + + buf[4] = (byte) ((secret & 0xff000000) >>> 24); + buf[5] = (byte) ((secret & 0x00ff0000) >>> 16); + buf[6] = (byte) ((secret & 0x0000ff00) >>> 8); + buf[7] = (byte) ((secret & 0x000000ff)); + + return (((long)(buf[0] & 0xFF)) << 56) + + (((long)(buf[1] & 0xFF)) << 48) + + (((long)(buf[2] & 0xFF)) << 40) + + (((long)(buf[3] & 0xFF)) << 32) + + (((long)(buf[4] & 0xFF)) << 24) + + (((long)(buf[5] & 0xFF)) << 16) + + (((long)(buf[6] & 0xFF)) << 8) + + ((long)(buf[7] & 0xFF)); + } + + public void run() { + while (true) { + try { + ToSend m = sendqueue.take(); + process(m); + } catch (InterruptedException e) { + break; + } + + } + } + + private void process(ToSend m) { + int attempts = 0; + byte zeroes[]; + byte requestBytes[] = new byte[48]; + DatagramPacket requestPacket = new DatagramPacket(requestBytes, + requestBytes.length); + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + + switch (m.type) { + case 0: + /* + * Building challenge request packet to send + */ + requestBuffer.clear(); + requestBuffer.putInt(ToSend.mType.crequest.ordinal()); + requestBuffer.putLong(m.tag); + requestBuffer.putInt(m.state.ordinal()); + zeroes = new byte[32]; + requestBuffer.put(zeroes); + + requestPacket.setLength(48); + try { + requestPacket.setSocketAddress(m.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + m.addr, + e); + } + + try { + if (challengeMap.get(m.tag) == null) { + mySocket.send(requestPacket); + } + } catch (IOException e) { + LOG.warn("Exception while sending challenge: ", e); + } + + break; + case 1: + /* + * Building challenge packet to send + */ + + long newChallenge; + ConcurrentHashMap tmpMap = addrChallengeMap.get(m.addr); + if(tmpMap != null){ + Long tmpLong = tmpMap.get(m.tag); + if (tmpLong != null) { + newChallenge = tmpLong; + } else { + newChallenge = genChallenge(); + } + + tmpMap.put(m.tag, newChallenge); + + requestBuffer.clear(); + requestBuffer.putInt(ToSend.mType.challenge.ordinal()); + requestBuffer.putLong(m.tag); + requestBuffer.putInt(m.state.ordinal()); + requestBuffer.putLong(newChallenge); + zeroes = new byte[24]; + requestBuffer.put(zeroes); + + requestPacket.setLength(48); + try { + requestPacket.setSocketAddress(m.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + m.addr, + e); + } + + + try { + mySocket.send(requestPacket); + } catch (IOException e) { + LOG.warn("Exception while sending challenge: ", e); + } + } else { + LOG.error("Address is not in the configuration: " + m.addr); + } + + break; + case 2: + + /* + * Building notification packet to send + */ + + requestBuffer.clear(); + requestBuffer.putInt(m.type); + requestBuffer.putLong(m.tag); + requestBuffer.putInt(m.state.ordinal()); + requestBuffer.putLong(m.leader); + requestBuffer.putLong(m.zxid); + requestBuffer.putLong(m.epoch); + zeroes = new byte[8]; + requestBuffer.put(zeroes); + + requestPacket.setLength(48); + try { + requestPacket.setSocketAddress(m.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + m.addr, + e); + } + + + boolean myChallenge = false; + boolean myAck = false; + + while (attempts < maxAttempts) { + try { + /* + * Try to obtain a challenge only if does not have + * one yet + */ + + if (!myChallenge && authEnabled) { + ToSend crequest = new ToSend( + ToSend.mType.crequest, m.tag, m.leader, + m.zxid, m.epoch, + QuorumPeer.ServerState.LOOKING, m.addr); + sendqueue.offer(crequest); + + try { + double timeout = ackWait + * java.lang.Math.pow(2, attempts); + + Semaphore s = new Semaphore(0); + synchronized(Messenger.this) { + challengeMutex.put(m.tag, s); + s.tryAcquire((long) timeout, TimeUnit.MILLISECONDS); + myChallenge = challengeMap + .containsKey(m.tag); + } + } catch (InterruptedException e) { + LOG.warn("Challenge request exception: ", e); + } + } + + /* + * If don't have challenge yet, skip sending + * notification + */ + + if (authEnabled && !myChallenge) { + attempts++; + continue; + } + + if (authEnabled) { + requestBuffer.position(40); + Long tmpLong = challengeMap.get(m.tag); + if(tmpLong != null){ + requestBuffer.putLong(tmpLong); + } else { + LOG.warn("No challenge with tag: " + m.tag); + } + } + mySocket.send(requestPacket); + try { + Semaphore s = new Semaphore(0); + double timeout = ackWait + * java.lang.Math.pow(10, attempts); + ackMutex.put(m.tag, s); + s.tryAcquire((int) timeout, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + LOG.warn("Ack exception: ", e); + } + + if(ackset.remove(m.tag)){ + myAck = true; + } + + } catch (IOException e) { + LOG.warn("Sending exception: ", e); + /* + * Do nothing, just try again + */ + } + if (myAck) { + /* + * Received ack successfully, so return + */ + challengeMap.remove(m.tag); + + return; + } else + attempts++; + } + /* + * Return message to queue for another attempt later if + * epoch hasn't changed. + */ + if (m.epoch == logicalclock) { + challengeMap.remove(m.tag); + sendqueue.offer(m); + } + break; + case 3: + + requestBuffer.clear(); + requestBuffer.putInt(m.type); + requestBuffer.putLong(m.tag); + requestBuffer.putInt(m.state.ordinal()); + requestBuffer.putLong(m.leader); + requestBuffer.putLong(m.zxid); + requestBuffer.putLong(m.epoch); + + requestPacket.setLength(48); + try { + requestPacket.setSocketAddress(m.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + m.addr, + e); + } + + + try { + mySocket.send(requestPacket); + } catch (IOException e) { + LOG.warn("Exception while sending ack: ", e); + } + break; + } + } + } + + public boolean queueEmpty() { + return (sendqueue.isEmpty() || ackset.isEmpty() || recvqueue + .isEmpty()); + } + + Messenger(int threads, DatagramSocket s) { + mySocket = s; + ackset = Collections.newSetFromMap(new ConcurrentHashMap()); + challengeMap = new ConcurrentHashMap(); + challengeMutex = new ConcurrentHashMap(); + ackMutex = new ConcurrentHashMap(); + addrChallengeMap = new ConcurrentHashMap>(); + lastProposedLeader = 0; + lastProposedZxid = 0; + lastEpoch = 0; + + for (int i = 0; i < threads; ++i) { + Thread t = new ZooKeeperThread(new WorkerSender(3), + "WorkerSender Thread: " + (i + 1)); + t.setDaemon(true); + t.start(); + } + + for (QuorumServer server : self.getVotingView().values()) { + InetSocketAddress saddr = new InetSocketAddress(server.addr + .getAddress(), port); + addrChallengeMap.put(saddr, new ConcurrentHashMap()); + } + + Thread t = new ZooKeeperThread(new WorkerReceiver(s, this), + "WorkerReceiver-" + s.getRemoteSocketAddress()); + t.start(); + } + + } + + QuorumPeer self; + int port; + volatile long logicalclock; /* Election instance */ + DatagramSocket mySocket; + long proposedLeader; + long proposedZxid; + + public AuthFastLeaderElection(QuorumPeer self, + boolean auth) { + this.authEnabled = auth; + starter(self); + } + + public AuthFastLeaderElection(QuorumPeer self) { + starter(self); + } + + private void starter(QuorumPeer self) { + this.self = self; + port = self.getVotingView().get(self.getId()).electionAddr.getPort(); + proposedLeader = -1; + proposedZxid = -1; + + try { + mySocket = new DatagramSocket(port); + // mySocket.setSoTimeout(20000); + } catch (SocketException e1) { + e1.printStackTrace(); + throw new RuntimeException(); + } + sendqueue = new LinkedBlockingQueue(2 * self.getVotingView().size()); + recvqueue = new LinkedBlockingQueue(2 * self.getVotingView() + .size()); + new Messenger(self.getVotingView().size() * 2, mySocket); + } + + private void leaveInstance() { + logicalclock++; + } + + private void sendNotifications() { + for (QuorumServer server : self.getView().values()) { + + ToSend notmsg = new ToSend(ToSend.mType.notification, + AuthFastLeaderElection.sequencer++, proposedLeader, + proposedZxid, logicalclock, QuorumPeer.ServerState.LOOKING, + self.getView().get(server.id).electionAddr); + + sendqueue.offer(notmsg); + } + } + + private boolean totalOrderPredicate(long id, long zxid) { + if ((zxid > proposedZxid) + || ((zxid == proposedZxid) && (id > proposedLeader))) + return true; + else + return false; + + } + + private boolean termPredicate(HashMap votes, + long l, long zxid) { + + + Collection votesCast = votes.values(); + int count = 0; + /* + * First make the views consistent. Sometimes peers will have different + * zxids for a server depending on timing. + */ + for (Vote v : votesCast) { + if ((v.getId() == l) && (v.getZxid() == zxid)) + count++; + } + + if (count > (self.getVotingView().size() / 2)) + return true; + else + return false; + + } + + /** + * There is nothing to shutdown in this implementation of + * leader election, so we simply have an empty method. + */ + public void shutdown(){} + + /** + * Invoked in QuorumPeer to find or elect a new leader. + * + * @throws InterruptedException + */ + 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; + } + + try { + HashMap recvset = + new HashMap(); + + HashMap outofelection = + new HashMap(); + + logicalclock++; + + proposedLeader = self.getId(); + proposedZxid = self.getLastLoggedZxid(); + + LOG.info("Election tally"); + sendNotifications(); + + /* + * Loop in which we exchange notifications until we find a leader + */ + + while (self.getPeerState() == ServerState.LOOKING) { + /* + * Remove next notification from queue, times out after 2 times + * the termination time + */ + Notification n = recvqueue.poll(2 * finalizeWait, + TimeUnit.MILLISECONDS); + + /* + * Sends more notifications if haven't received enough. + * Otherwise processes new notification. + */ + if (n == null) { + if (((!outofelection.isEmpty()) || (recvset.size() > 1))) + sendNotifications(); + } else + switch (n.state) { + case LOOKING: + if (n.epoch > logicalclock) { + logicalclock = n.epoch; + recvset.clear(); + if (totalOrderPredicate(n.leader, n.zxid)) { + proposedLeader = n.leader; + proposedZxid = n.zxid; + } + sendNotifications(); + } else if (n.epoch < logicalclock) { + break; + } else if (totalOrderPredicate(n.leader, n.zxid)) { + proposedLeader = n.leader; + proposedZxid = n.zxid; + + sendNotifications(); + } + + recvset.put(n.addr, new Vote(n.leader, n.zxid)); + + // If have received from all nodes, then terminate + if (self.getVotingView().size() == recvset.size()) { + self.setPeerState((proposedLeader == self.getId()) ? + ServerState.LEADING: ServerState.FOLLOWING); + // if (self.state == ServerState.FOLLOWING) { + // Thread.sleep(100); + // } + leaveInstance(); + return new Vote(proposedLeader, proposedZxid); + + } else if (termPredicate(recvset, proposedLeader, + proposedZxid)) { + // Otherwise, wait for a fixed amount of time + LOG.info("Passed predicate"); + Thread.sleep(finalizeWait); + + // Notification probe = recvqueue.peek(); + + // Verify if there is any change in the proposed leader + while ((!recvqueue.isEmpty()) + && !totalOrderPredicate( + recvqueue.peek().leader, recvqueue + .peek().zxid)) { + recvqueue.poll(); + } + if (recvqueue.isEmpty()) { + // LOG.warn("Proposed leader: " + + // proposedLeader); + self.setPeerState( + (proposedLeader == self.getId()) ? + ServerState.LEADING : + ServerState.FOLLOWING); + + leaveInstance(); + return new Vote(proposedLeader, proposedZxid); + } + } + break; + case LEADING: + outofelection.put(n.addr, new Vote(n.leader, n.zxid)); + + if (termPredicate(outofelection, n.leader, n.zxid)) { + + self.setPeerState((n.leader == self.getId()) ? + ServerState.LEADING: ServerState.FOLLOWING); + + leaveInstance(); + return new Vote(n.leader, n.zxid); + } + break; + case FOLLOWING: + outofelection.put(n.addr, new Vote(n.leader, n.zxid)); + + if (termPredicate(outofelection, n.leader, n.zxid)) { + + self.setPeerState((n.leader == self.getId()) ? + ServerState.LEADING: ServerState.FOLLOWING); + + leaveInstance(); + return new Vote(n.leader, n.zxid); + } + break; + default: + break; + } + } + + 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; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/CommitProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/CommitProcessor.java new file mode 100644 index 0000000..3419f08 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/CommitProcessor.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.server.quorum; + +import java.util.ArrayList; +import java.util.LinkedList; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeperServerListener; + +/** + * 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. + */ +public class CommitProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + private static final Logger LOG = LoggerFactory.getLogger(CommitProcessor.class); + + /** + * Requests that we are holding until the commit comes in. + */ + LinkedList queuedRequests = new LinkedList(); + + /** + * Requests that have been committed. + */ + LinkedList committedRequests = new LinkedList(); + + RequestProcessor nextProcessor; + ArrayList toProcess = new ArrayList(); + + /** + * 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; + } + + volatile boolean finished = false; + + @Override + public void run() { + try { + Request nextPending = null; + while (!finished) { + int len = toProcess.size(); + for (int i = 0; i < len; i++) { + nextProcessor.processRequest(toProcess.get(i)); + } + toProcess.clear(); + synchronized (this) { + if ((queuedRequests.size() == 0 || nextPending != null) + && committedRequests.size() == 0) { + wait(); + continue; + } + // First check and see if the commit came in for the pending + // request + if ((queuedRequests.size() == 0 || nextPending != null) + && committedRequests.size() > 0) { + Request r = committedRequests.remove(); + /* + * We match with nextPending so that we can move to the + * next request when it is committed. We also want to + * use nextPending because it has the cnxn member set + * properly. + */ + if (nextPending != null + && nextPending.sessionId == r.sessionId + && nextPending.cxid == r.cxid) { + // we want to send our version of the request. + // the pointer to the connection in the request + nextPending.hdr = r.hdr; + nextPending.txn = r.txn; + nextPending.zxid = r.zxid; + toProcess.add(nextPending); + nextPending = null; + } else { + // this request came from someone else so just + // send the commit packet + toProcess.add(r); + } + } + } + + // We haven't matched the pending requests, so go back to + // waiting + if (nextPending != null) { + continue; + } + + synchronized (this) { + // Process the next requests in the queuedRequests + while (nextPending == null && queuedRequests.size() > 0) { + Request request = queuedRequests.remove(); + switch (request.type) { + case OpCode.create: + case OpCode.delete: + case OpCode.setData: + case OpCode.multi: + case OpCode.setACL: + case OpCode.createSession: + case OpCode.closeSession: + nextPending = request; + break; + case OpCode.sync: + if (matchSyncs) { + nextPending = request; + } else { + toProcess.add(request); + } + break; + default: + toProcess.add(request); + } + } + } + } + } catch (InterruptedException e) { + LOG.warn("Interrupted exception while waiting", e); + } catch (Throwable e) { + LOG.error("Unexpected exception causing CommitProcessor to exit", e); + } + LOG.info("CommitProcessor exited loop!"); + } + + synchronized public void commit(Request request) { + if (!finished) { + if (request == null) { + LOG.warn("Committed a null!", + new Exception("committing a null! ")); + return; + } + if (LOG.isDebugEnabled()) { + LOG.debug("Committing request:: " + request); + } + committedRequests.add(request); + notifyAll(); + } + } + + synchronized public void processRequest(Request request) { + // request.addRQRec(">commit"); + if (LOG.isDebugEnabled()) { + LOG.debug("Processing request:: " + request); + } + + if (!finished) { + queuedRequests.add(request); + notifyAll(); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + synchronized (this) { + finished = true; + queuedRequests.clear(); + notifyAll(); + } + if (nextProcessor != null) { + nextProcessor.shutdown(); + } + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/Election.java b/src/java/main/org/apache/zookeeper/server/quorum/Election.java new file mode 100644 index 0000000..8990638 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Election.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.server.quorum; + + +import org.apache.zookeeper.server.quorum.Vote; + +public interface Election { + public Vote lookForLeader() throws InterruptedException; + public void shutdown(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/FastLeaderElection.java b/src/java/main/org/apache/zookeeper/server/quorum/FastLeaderElection.java new file mode 100644 index 0000000..91b6af8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/FastLeaderElection.java @@ -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. + */ + + +package org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.HashSet; +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.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +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. + */ + final static 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. + */ + + final static int maxNotificationInterval = 60000; + + /** + * Connection manager. Fast leader election uses TCP for + * communication between peers, and QuorumCnxManager manages + * such connections. + */ + + QuorumCnxManager manager; + + + /** + * 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 + */ + + static public class Notification { + /* + * Format version, introduced in 3.4.6 + */ + + public final static int CURRENTVERSION = 0x1; + 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; + + /* + * epoch of the proposed leader + */ + long peerEpoch; + + @Override + public String toString() { + return Long.toHexString(version) + " (message format version), " + + leader + " (n.leader), 0x" + + Long.toHexString(zxid) + " (n.zxid), 0x" + + Long.toHexString(electionEpoch) + " (n.round), " + state + + " (n.state), " + sid + " (n.sid), 0x" + + Long.toHexString(peerEpoch) + " (n.peerEpoch) "; + } + } + + 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 + */ + + requestBuffer.clear(); + requestBuffer.putInt(state); + requestBuffer.putLong(leader); + requestBuffer.putLong(zxid); + requestBuffer.putLong(electionEpoch); + requestBuffer.putLong(epoch); + requestBuffer.putInt(Notification.CURRENTVERSION); + + return requestBuffer; + } + + /** + * Messages that a peer wants to send to other peers. + * These messages can be both Notifications and Acks + * of reception of notification. + */ + static public class ToSend { + static enum mType {crequest, challenge, notification, ack} + + ToSend(mType type, + long leader, + long zxid, + long electionEpoch, + ServerState state, + long sid, + long peerEpoch) { + + this.leader = leader; + this.zxid = zxid; + this.electionEpoch = electionEpoch; + this.state = state; + this.sid = sid; + this.peerEpoch = peerEpoch; + } + + /* + * 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; + + /* + * 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{ + response = manager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if(response == null) continue; + + /* + * If it is from an observer, respond right away. + * Note that the following predicate assumes that + * if a server is not a follower, then it must be + * an observer. If we ever have any other type of + * learner in the future, we'll have to change the + * way we check for observers. + */ + if(!validVoter(response.sid)){ + Vote current = self.getCurrentVote(); + ToSend notmsg = new ToSend(ToSend.mType.notification, + current.getId(), + current.getZxid(), + logicalclock.get(), + self.getPeerState(), + response.sid, + current.getPeerEpoch()); + + sendqueue.offer(notmsg); + } else { + // Receive new message + if (LOG.isDebugEnabled()) { + LOG.debug("Receive new notification message. My id = " + + self.getId()); + } + + /* + * We check for 28 bytes for backward compatibility + */ + if (response.buffer.capacity() < 28) { + LOG.error("Got a short response: " + + response.buffer.capacity()); + continue; + } + boolean backCompatibility = (response.buffer.capacity() == 28); + response.buffer.clear(); + + // Instantiate Notification and set its attributes + Notification n = new Notification(); + + // State of peer that sent this message + QuorumPeer.ServerState ackstate = QuorumPeer.ServerState.LOOKING; + switch (response.buffer.getInt()) { + 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; + } + + n.leader = response.buffer.getLong(); + n.zxid = response.buffer.getLong(); + n.electionEpoch = response.buffer.getLong(); + n.state = ackstate; + n.sid = response.sid; + if(!backCompatibility){ + n.peerEpoch = response.buffer.getLong(); + } else { + if(LOG.isInfoEnabled()){ + LOG.info("Backward compatibility mode, server id=" + n.sid); + } + n.peerEpoch = ZxidUtils.getEpochFromZxid(n.zxid); + } + + /* + * Version added in 3.4.6 + */ + + n.version = (response.buffer.remaining() >= 4) ? + response.buffer.getInt() : 0x0; + + /* + * Print notification info + */ + if(LOG.isInfoEnabled()){ + printNotification(n); + } + + /* + * If this server is looking, then send proposed leader + */ + + if(self.getPeerState() == QuorumPeer.ServerState.LOOKING){ + 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(); + ToSend notmsg = new ToSend(ToSend.mType.notification, + v.getId(), + v.getZxid(), + logicalclock.get(), + self.getPeerState(), + response.sid, + v.getPeerEpoch()); + 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(LOG.isDebugEnabled()){ + LOG.debug("Sending new notification. My id = " + + self.getId() + " recipient=" + + response.sid + " zxid=0x" + + Long.toHexString(current.getZxid()) + + " leader=" + current.getId()); + } + + ToSend notmsg; + if(n.version > 0x0) { + notmsg = new ToSend( + ToSend.mType.notification, + current.getId(), + current.getZxid(), + current.getElectionEpoch(), + self.getPeerState(), + response.sid, + current.getPeerEpoch()); + + } else { + Vote bcVote = self.getBCVote(); + notmsg = new ToSend( + ToSend.mType.notification, + bcVote.getId(), + bcVote.getZxid(), + bcVote.getElectionEpoch(), + self.getPeerState(), + response.sid, + bcVote.getPeerEpoch()); + } + sendqueue.offer(notmsg); + } + } + } + } catch (InterruptedException e) { + System.out.println("Interrupted Exception while waiting for new message" + + e.toString()); + } + } + 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 { + ToSend m = sendqueue.poll(3000, TimeUnit.MILLISECONDS); + if(m == null) continue; + + 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) { + ByteBuffer requestBuffer = buildMsg(m.state.ordinal(), + m.leader, + m.zxid, + m.electionEpoch, + m.peerEpoch); + manager.toSend(m.sid, requestBuffer); + } + } + + + WorkerSender ws; + WorkerReceiver wr; + + /** + * Constructor of class Messenger. + * + * @param manager Connection manager + */ + Messenger(QuorumCnxManager manager) { + + this.ws = new WorkerSender(manager); + + Thread t = new Thread(this.ws, + "WorkerSender[myid=" + self.getId() + "]"); + t.setDaemon(true); + t.start(); + + this.wr = new WorkerReceiver(manager); + + t = new Thread(this.wr, + "WorkerReceiver[myid=" + self.getId() + "]"); + t.setDaemon(true); + t.start(); + } + + /** + * Stops instances of WorkerSender and WorkerReceiver + */ + void halt(){ + this.ws.stop = true; + this.wr.stop = true; + } + + } + + QuorumPeer self; + Messenger messenger; + AtomicLong logicalclock = new AtomicLong(); /* Election instance */ + long proposedLeader; + long proposedZxid; + long proposedEpoch; + + + /** + * Returns the current vlue of the logical clock counter + */ + public long getLogicalClock(){ + return logicalclock.get(); + } + + /** + * 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; + 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; + + sendqueue = new LinkedBlockingQueue(); + recvqueue = new LinkedBlockingQueue(); + this.messenger = new Messenger(manager); + } + + private void leaveInstance(Vote v) { + if(LOG.isDebugEnabled()){ + LOG.debug("About to leave FLE instance: leader=" + + v.getId() + ", zxid=0x" + + Long.toHexString(v.getZxid()) + ", my id=" + self.getId() + + ", my state=" + self.getPeerState()); + } + recvqueue.clear(); + } + + public QuorumCnxManager getCnxManager(){ + return manager; + } + + volatile boolean stop; + public void shutdown(){ + stop = true; + 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() { + for (QuorumServer server : self.getVotingView().values()) { + long sid = server.id; + + ToSend notmsg = new ToSend(ToSend.mType.notification, + proposedLeader, + proposedZxid, + logicalclock.get(), + QuorumPeer.ServerState.LOOKING, + sid, + proposedEpoch); + if(LOG.isDebugEnabled()){ + LOG.debug("Sending Notification: " + proposedLeader + " (n.leader), 0x" + + Long.toHexString(proposedZxid) + " (n.zxid), 0x" + Long.toHexString(logicalclock.get()) + + " (n.round), " + sid + " (recipient), " + self.getId() + + " (myid), 0x" + Long.toHexString(proposedEpoch) + " (n.peerEpoch)"); + } + sendqueue.offer(notmsg); + } + } + + + private void printNotification(Notification n){ + LOG.info("Notification: " + n.toString() + + self.getPeerState() + " (my state)"); + } + + /** + * Check if a pair (server id, zxid) succeeds our + * current vote. + * + * @param id Server identifier + * @param zxid Last zxid observed by the issuer of this vote + */ + protected boolean totalOrderPredicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch) { + LOG.debug("id: " + newId + ", proposed id: " + curId + ", zxid: 0x" + + Long.toHexString(newZxid) + ", proposed zxid: 0x" + 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. + */ + + return ((newEpoch > curEpoch) || + ((newEpoch == curEpoch) && + ((newZxid > curZxid) || ((newZxid == curZxid) && (newId > curId))))); + } + + /** + * Termination predicate. Given a set of votes, determines if + * have sufficient to declare the end of the election round. + * + * @param votes Set of votes + * @param l Identifier of the vote received last + * @param zxid zxid of the the vote received last + */ + protected boolean termPredicate( + HashMap votes, + Vote vote) { + + HashSet set = new HashSet(); + + /* + * 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())){ + set.add(entry.getKey()); + } + } + + return self.getQuorumVerifier().containsQuorum(set); + } + + /** + * 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( + HashMap 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; + } + + /** + * This predicate checks that a leader has been elected. It doesn't + * make a lot of sense without context (check lookForLeader) and it + * has been separated for testing purposes. + * + * @param recv map of received votes + * @param ooe map containing out of election votes (LEADING or FOLLOWING) + * @param n Notification + * @return + */ + protected boolean ooePredicate(HashMap recv, + HashMap ooe, + Notification n) { + + return (termPredicate(recv, new Vote(n.version, + n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch, + n.state)) + && checkLeader(ooe, n.leader, n.electionEpoch)); + + } + + synchronized void updateProposal(long leader, long zxid, long epoch){ + if(LOG.isDebugEnabled()){ + LOG.debug("Updating proposal: " + leader + " (newleader), 0x" + + Long.toHexString(zxid) + " (newzxid), " + proposedLeader + + " (oldleader), 0x" + Long.toHexString(proposedZxid) + " (oldzxid)"); + } + proposedLeader = leader; + proposedZxid = zxid; + proposedEpoch = epoch; + } + + 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'm a participant: " + self.getId()); + return ServerState.FOLLOWING; + } + else{ + LOG.debug("I'm an observer: " + self.getId()); + return ServerState.OBSERVING; + } + } + + /** + * Returns the initial vote value of server identifier. + * + * @return long + */ + private long getInitId(){ + if(self.getLearnerType() == LearnerType.PARTICIPANT) + 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; + } + + /** + * 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; + } + if (self.start_fle == 0) { + self.start_fle = Time.currentElapsedTime(); + } + try { + HashMap recvset = new HashMap(); + + HashMap outofelection = new HashMap(); + + int notTimeout = finalizeWait; + + synchronized(this){ + logicalclock.incrementAndGet(); + updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); + } + + LOG.info("New election. My id = " + self.getId() + + ", proposed zxid=0x" + Long.toHexString(proposedZxid)); + sendNotifications(); + + /* + * Loop in which we exchange notifications until we find a leader + */ + + while ((self.getPeerState() == ServerState.LOOKING) && + (!stop)){ + /* + * Remove next notification from queue, times out after 2 times + * the termination time + */ + Notification n = recvqueue.poll(notTimeout, + TimeUnit.MILLISECONDS); + + /* + * Sends more notifications if haven't received enough. + * Otherwise processes new notification. + */ + if(n == null){ + if(manager.haveDelivered()){ + sendNotifications(); + } else { + manager.connectAll(); + } + + /* + * Exponential backoff + */ + int tmpTimeOut = notTimeout*2; + notTimeout = (tmpTimeOut < maxNotificationInterval? + tmpTimeOut : maxNotificationInterval); + LOG.info("Notification time out: " + notTimeout); + } + else if(validVoter(n.sid) && validVoter(n.leader)) { + /* + * Only proceed if the vote comes from a replica in the + * voting view for a replica in the voting view. + */ + switch (n.state) { + case LOOKING: + // If notification > current, replace and send messages out + if (n.electionEpoch > logicalclock.get()) { + logicalclock.set(n.electionEpoch); + recvset.clear(); + if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, + getInitId(), getInitLastLoggedZxid(), getPeerEpoch())) { + updateProposal(n.leader, n.zxid, n.peerEpoch); + } else { + updateProposal(getInitId(), + getInitLastLoggedZxid(), + getPeerEpoch()); + } + sendNotifications(); + } else if (n.electionEpoch < logicalclock.get()) { + if(LOG.isDebugEnabled()){ + LOG.debug("Notification election epoch is smaller than logicalclock. n.electionEpoch = 0x" + + Long.toHexString(n.electionEpoch) + + ", logicalclock=0x" + Long.toHexString(logicalclock.get())); + } + break; + } else if (totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, + proposedLeader, proposedZxid, proposedEpoch)) { + updateProposal(n.leader, n.zxid, n.peerEpoch); + sendNotifications(); + } + + if(LOG.isDebugEnabled()){ + LOG.debug("Adding vote: from=" + n.sid + + ", proposed leader=" + n.leader + + ", proposed zxid=0x" + Long.toHexString(n.zxid) + + ", proposed election epoch=0x" + Long.toHexString(n.electionEpoch)); + } + + recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch)); + + if (termPredicate(recvset, + new Vote(proposedLeader, proposedZxid, + logicalclock.get(), proposedEpoch))) { + + // Verify if there is any change in the proposed leader + while((n = recvqueue.poll(finalizeWait, + TimeUnit.MILLISECONDS)) != null){ + if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, + proposedLeader, proposedZxid, proposedEpoch)){ + recvqueue.put(n); + break; + } + } + + /* + * This predicate is true once we don't read any new + * relevant message from the reception queue + */ + if (n == null) { + self.setPeerState((proposedLeader == self.getId()) ? + ServerState.LEADING: learningState()); + + Vote endVote = new Vote(proposedLeader, + proposedZxid, + logicalclock.get(), + proposedEpoch); + leaveInstance(endVote); + return endVote; + } + } + break; + case OBSERVING: + LOG.debug("Notification from observer: " + n.sid); + break; + case FOLLOWING: + case LEADING: + /* + * Consider all notifications from the same epoch + * together. + */ + if(n.electionEpoch == logicalclock.get()){ + recvset.put(n.sid, new Vote(n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch)); + + if(ooePredicate(recvset, outofelection, n)) { + self.setPeerState((n.leader == self.getId()) ? + ServerState.LEADING: learningState()); + + Vote endVote = new Vote(n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch); + leaveInstance(endVote); + return endVote; + } + } + + /* + * Before joining an established ensemble, verify + * a majority is following the same leader. + */ + outofelection.put(n.sid, new Vote(n.version, + n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch, + n.state)); + + if(ooePredicate(outofelection, outofelection, n)) { + synchronized(this){ + logicalclock.set(n.electionEpoch); + self.setPeerState((n.leader == self.getId()) ? + ServerState.LEADING: learningState()); + } + Vote endVote = new Vote(n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch); + leaveInstance(endVote); + return endVote; + } + break; + default: + LOG.warn("Notification state unrecognized: {} (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.getVotingView().containsKey(sid); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/Follower.java b/src/java/main/org/apache/zookeeper/server/quorum/Follower.java new file mode 100644 index 0000000..27c3375 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Follower.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.server.quorum; + +import java.io.IOException; +import java.net.InetSocketAddress; + +import org.apache.jute.Record; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +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; + + 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); + LOG.info("FOLLOWING - LEADER ELECTION TOOK - {}", electionTimeTaken); + self.start_fle = 0; + self.end_fle = 0; + fzk.registerJMX(new FollowerBean(this, zk), self.jmxLocalPeerBean); + try { + QuorumServer leaderServer = findLeader(); + try { + connectToLeader(leaderServer.addr, leaderServer.hostname); + long newEpochZxid = registerWithLeader(Leader.FOLLOWERINFO); + + //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"); + } + syncWithLeader(newEpochZxid); + QuorumPacket qp = new QuorumPacket(); + while (this.isRunning()) { + readPacket(qp); + processPacket(qp); + } + } catch (Exception e) { + LOG.warn("Exception when following the leader", e); + try { + sock.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + // clear pending revalidations + pendingRevalidations.clear(); + } + } finally { + zk.unregisterJMX((Learner)this); + } + } + + /** + * Examine the packet received in qp and dispatch based on its contents. + * @param qp + * @throws IOException + */ + protected void processPacket(QuorumPacket qp) throws IOException{ + switch (qp.getType()) { + case Leader.PING: + ping(qp); + break; + case Leader.PROPOSAL: + TxnHeader hdr = new TxnHeader(); + Record txn = SerializeUtils.deserializeTxn(qp.getData(), hdr); + if (hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x" + + Long.toHexString(hdr.getZxid()) + + " expected 0x" + + Long.toHexString(lastQueued + 1)); + } + lastQueued = hdr.getZxid(); + fzk.logRequest(hdr, txn); + break; + case Leader.COMMIT: + fzk.commit(qp.getZxid()); + break; + case Leader.UPTODATE: + LOG.error("Received an UPTODATE message after Follower started"); + break; + case Leader.REVALIDATE: + revalidate(qp); + break; + case Leader.SYNC: + fzk.sync(); + break; + default: + LOG.error("Invalid packet type: {} received by Observer", qp.getType()); + } + } + + /** + * 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; + } + + @Override + public void shutdown() { + LOG.info("shutdown called", new Exception("shutdown Follower")); + super.shutdown(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/FollowerBean.java b/src/java/main/org/apache/zookeeper/server/quorum/FollowerBean.java new file mode 100644 index 0000000..43d7cb7 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/FollowerBean.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; + +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(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/FollowerMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/FollowerMXBean.java new file mode 100644 index 0000000..45c7fd8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/FollowerMXBean.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.quorum; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Follower MBean + */ +public interface FollowerMXBean extends ZooKeeperServerMXBean { + /** + * @return socket address + */ + public String getQuorumAddress(); + + /** + * @return last queued zxid + */ + public String getLastQueuedZxid(); + + /** + * @return count of pending revalidations + */ + public int getPendingRevalidationCount(); + + /** + * @return time taken for leader election in milliseconds. + */ + public long getElectionTimeTaken(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.java new file mode 100644 index 0000000..8013b65 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.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.quorum; + +import java.util.concurrent.LinkedBlockingQueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooTrace; + +/** + * 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; + + RequestProcessor nextProcessor; + + 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) { + Request request = queuedRequests.take(); + if (LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, ZooTrace.CLIENT_REQUEST_TRACE_MASK, + 'F', request, ""); + } + if (request == Request.requestOfDeath) { + break; + } + // 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 follower has pending, so we + // add it to pendingSyncs. + switch (request.type) { + case OpCode.sync: + zks.pendingSyncs.add(request); + zks.getFollower().request(request); + break; + case OpCode.create: + case OpCode.delete: + case OpCode.setData: + case OpCode.setACL: + case OpCode.createSession: + case OpCode.closeSession: + case OpCode.multi: + zks.getFollower().request(request); + break; + } + } + } catch (Exception e) { + handleException(this.getName(), e); + } + LOG.info("FollowerRequestProcessor exited loop!"); + } + + public void processRequest(Request request) { + if (!finished) { + queuedRequests.add(request); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + finished = true; + queuedRequests.clear(); + queuedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.java new file mode 100644 index 0000000..1fe687b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.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 java.io.IOException; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.jute.Record; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.apache.zookeeper.txn.TxnHeader; + +/** + * 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); + + CommitProcessor commitProcessor; + + SyncRequestProcessor syncProcessor; + + /* + * Pending sync requests + */ + ConcurrentLinkedQueue pendingSyncs; + + /** + * @param port + * @param dataDir + * @throws IOException + */ + FollowerZooKeeperServer(FileTxnSnapLog logFactory,QuorumPeer self, + DataTreeBuilder treeBuilder, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, + self.maxSessionTimeout, treeBuilder, zkDb, self); + this.pendingSyncs = new ConcurrentLinkedQueue(); + } + + public Follower getFollower(){ + return self.follower; + } + + @Override + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + commitProcessor = new CommitProcessor(finalProcessor, + Long.toString(getServerId()), true, + getZooKeeperServerListener()); + commitProcessor.start(); + firstProcessor = new FollowerRequestProcessor(this, commitProcessor); + ((FollowerRequestProcessor) firstProcessor).start(); + syncProcessor = new SyncRequestProcessor(this, + new SendAckRequestProcessor((Learner)getFollower())); + syncProcessor.start(); + } + + LinkedBlockingQueue pendingTxns = new LinkedBlockingQueue(); + + public void logRequest(TxnHeader hdr, Record txn) { + Request request = new Request(null, hdr.getClientId(), hdr.getCxid(), + hdr.getType(), null, null); + request.hdr = hdr; + request.txn = txn; + request.zxid = hdr.getZxid(); + 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)); + System.exit(12); + } + Request request = pendingTxns.remove(); + commitProcessor.commit(request); + } + + synchronized public void sync(){ + if(pendingSyncs.size() ==0){ + LOG.warn("Not expecting a sync."); + return; + } + + Request r = pendingSyncs.remove(); + commitProcessor.commit(r); + } + + @Override + public int getGlobalOutstandingLimit() { + return super.getGlobalOutstandingLimit() / (self.getQuorumSize() - 1); + } + + @Override + public void shutdown() { + 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); + } + } + + @Override + public String getState() { + return "follower"; + } + + @Override + public Learner getLearner() { + return getFollower(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/Leader.java b/src/java/main/org/apache/zookeeper/server/quorum/Leader.java new file mode 100644 index 0000000..ed715fd --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Leader.java @@ -0,0 +1,1083 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.ByteArrayOutputStream; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.BindException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; + +import javax.security.sasl.SaslException; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +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 { + private static final Logger LOG = LoggerFactory.getLogger(Leader.class); + + static final private boolean nodelay = System.getProperty("leader.nodelay", "true").equals("true"); + static { + LOG.info("TCP NoDelay set to: " + nodelay); + } + + static public class Proposal { + public QuorumPacket packet; + + public HashSet ackSet = new HashSet(); + + public Request request; + + @Override + public String toString() { + return packet.getType() + ", " + packet.getZxid() + ", " + request; + } + } + + final LeaderZooKeeperServer zk; + + final QuorumPeer self; + // VisibleForTesting + protected boolean quorumFormed = false; + + // the follower acceptor thread + LearnerCnxAcceptor cnxAcceptor; + + // list of all the followers + private final HashSet learners = + new HashSet(); + + /** + * 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(); + + private final ProposalStats proposalStats; + + public ProposalStats getProposalStats() { + return proposalStats; + } + + /** + * Returns a copy of the current forwarding follower snapshot + */ + public List getForwardingFollowers() { + synchronized (forwardingFollowers) { + return new ArrayList(forwardingFollowers); + } + } + + private 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); + } + } + + // Pending sync requests. Must access under 'this' lock. + private final HashMap> pendingSyncs = + new HashMap>(); + + synchronized public int getNumPendingSyncs() { + return pendingSyncs.size(); + } + + //Follower counter + final AtomicLong followerCounter = new AtomicLong(-1); + + /** + * Adds peer to the leader. + * + * @param learner + * instance of learner handle + */ + void addLearnerHandler(LearnerHandler learner) { + synchronized (learners) { + learners.add(learner); + } + } + + /** + * Remove the learner from the learner list + * + * @param peer + */ + 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); + } + } + + ServerSocket ss; + + Leader(QuorumPeer self,LeaderZooKeeperServer zk) throws IOException { + this.self = self; + this.proposalStats = new ProposalStats(); + try { + if (self.getQuorumListenOnAllIPs()) { + ss = new ServerSocket(self.getQuorumAddress().getPort()); + } else { + ss = new ServerSocket(); + } + ss.setReuseAddress(true); + if (!self.getQuorumListenOnAllIPs()) { + ss.bind(self.getQuorumAddress()); + } + } catch (BindException e) { + if (self.getQuorumListenOnAllIPs()) { + LOG.error("Couldn't bind to port " + self.getQuorumAddress().getPort(), e); + } else { + LOG.error("Couldn't bind to " + self.getQuorumAddress(), e); + } + throw e; + } + this.zk=zk; + } + + /** + * This message is for follower to expect diff + */ + final static int DIFF = 13; + + /** + * This is for follower to truncate its logs + */ + final static int TRUNC = 14; + + /** + * This is for follower to download the snapshots + */ + final static int SNAP = 15; + + /** + * This tells the leader that the connecting peer is actually an observer + */ + final static int OBSERVERINFO = 16; + + /** + * This message type is sent by the leader to indicate it's zxid and if + * needed, its database. + */ + final static int NEWLEADER = 10; + + /** + * This message type is sent by a follower to pass the last zxid. This is here + * for backward compatibility purposes. + */ + final static 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. + */ + final static 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. + */ + final static int REQUEST = 1; + + /** + * This message type is sent by a leader to propose a mutation. + */ + public final static int PROPOSAL = 2; + + /** + * This message type is sent by a follower after it has synced a proposal. + */ + final static int ACK = 3; + + /** + * This message type is sent by a leader to commit a proposal and cause + * followers to start serving the corresponding data. + */ + final static int COMMIT = 4; + + /** + * This message type is enchanged between follower and leader (initiated by + * follower) to determine liveliness. + */ + final static int PING = 5; + + /** + * This message type is to validate a session that should be active. + */ + final static int REVALIDATE = 6; + + /** + * This message is a reply to a synchronize command flushing the pipe + * between the leader and the follower. + */ + final static int SYNC = 7; + + /** + * This message type informs observers of a committed proposal. + */ + final static int INFORM = 8; + + ConcurrentMap outstandingProposals = new ConcurrentHashMap(); + + ConcurrentLinkedQueue toBeApplied = new ConcurrentLinkedQueue(); + + Proposal newLeaderProposal = new Proposal(); + + class LearnerCnxAcceptor extends ZooKeeperThread{ + private volatile boolean stop = false; + + public LearnerCnxAcceptor() { + super("LearnerCnxAcceptor-" + ss.getLocalSocketAddress()); + } + + @Override + public void run() { + try { + while (!stop) { + 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); + s.setTcpNoDelay(nodelay); + + BufferedInputStream is = new BufferedInputStream( + s.getInputStream()); + LearnerHandler fh = new LearnerHandler(s, is, Leader.this); + fh.start(); + } catch (SocketException e) { + if (stop) { + LOG.info("exception while shutting down acceptor: " + + e); + + // When Leader.shutdown() calls ss.close(), + // the call to accept throws an exception. + // We catch and set stop to true. + stop = true; + } else { + throw e; + } + } catch (SaslException e){ + LOG.error("Exception while connecting to quorum learner", e); + } + } + } catch (Exception e) { + LOG.warn("Exception while accepting follower", e); + } + } + + public void halt() { + stop = true; + } + } + + StateSummary leaderStateSummary; + + long epoch = -1; + boolean waitingForNewEpoch = true; + volatile boolean readyToStart = false; + + /** + * 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); + LOG.info("LEADING - LEADER ELECTION TOOK - {}", electionTimeTaken); + self.start_fle = 0; + self.end_fle = 0; + + zk.registerJMX(new LeaderBean(this, zk), self.jmxLocalPeerBean); + + try { + self.tick.set(0); + zk.loadData(); + + leaderStateSummary = new StateSummary(self.getCurrentEpoch(), zk.getLastProcessedZxid()); + + // Start thread that waits for connection requests from + // new followers. + cnxAcceptor = new LearnerCnxAcceptor(); + cnxAcceptor.start(); + + readyToStart = true; + long epoch = getEpochToPropose(self.getId(), self.getAcceptedEpoch()); + + zk.setZxid(ZxidUtils.makeZxid(epoch, 0)); + + synchronized(this){ + lastProposed = zk.getZxid(); + } + + 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())); + } + + waitForEpochAck(self.getId(), leaderStateSummary); + self.setCurrentEpoch(epoch); + + // 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 + try { + waitForNewLeaderAck(self.getId(), zk.getZxid()); + } catch (InterruptedException e) { + shutdown("Waiting for a quorum of followers, only synced with sids: [ " + + getSidSetString(newLeaderProposal.ackSet) + " ]"); + HashSet followerSet = new HashSet(); + for (LearnerHandler f : learners) + followerSet.add(f.getSid()); + + if (self.getQuorumVerifier().containsQuorum(followerSet)) { + LOG.warn("Enough followers present. " + + "Perhaps the initTicks need to be increased."); + } + Thread.sleep(self.tickTime); + self.tick.incrementAndGet(); + return; + } + + 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.cnxnFactory.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; + + while (true) { + Thread.sleep(self.tickTime / 2); + if (!tickSkip) { + self.tick.incrementAndGet(); + } + HashSet syncedSet = new HashSet(); + + // lock on the followers when we use it. + syncedSet.add(self.getId()); + + for (LearnerHandler f : getLearners()) { + // Synced set is used to check we have a supporting quorum, so only + // PARTICIPANT, not OBSERVER, learners should be used + if (f.synced() && f.getLearnerType() == LearnerType.PARTICIPANT) { + syncedSet.add(f.getSid()); + } + f.ping(); + } + + // check leader running status + if (!this.isRunning()) { + shutdown("Unexpected internal error"); + return; + } + + if (!tickSkip && !self.getQuorumVerifier().containsQuorum(syncedSet)) { + //if (!tickSkip && syncedCount < self.quorumPeers.size() / 2) { + // Lost quorum, shutdown + shutdown("Not sufficient followers synced, only synced with sids: [ " + + getSidSetString(syncedSet) + " ]"); + // make sure the order is the same! + // the leader goes to looking + return; + } + tickSkip = !tickSkip; + } + } 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", + new Exception("shutdown Leader! reason: " + reason)); + + if (cnxAcceptor != null) { + cnxAcceptor.halt(); + } + + // NIO should not accept conenctions + self.cnxnFactory.setZooKeeperServer(null); + try { + ss.close(); + } catch (IOException e) { + LOG.warn("Ignoring unexpected exception during close",e); + } + // clear all the connections + self.cnxnFactory.closeAll(); + // 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; + } + + /** + * 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 followerAddr + */ + synchronized public void processAck(long sid, long zxid, SocketAddress followerAddr) { + 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 by this method. However, + * the learner sends ack back to the leader after it gets UPTODATE + * so we just ignore the message. + */ + return; + } + + if (outstandingProposals.size() == 0) { + if (LOG.isDebugEnabled()) { + LOG.debug("outstanding is 0"); + } + return; + } + if (lastCommitted >= zxid) { + if (LOG.isDebugEnabled()) { + LOG.debug("proposal has already been committed, pzxid: 0x{} zxid: 0x{}", + Long.toHexString(lastCommitted), Long.toHexString(zxid)); + } + // The proposal has already been committed + return; + } + Proposal p = outstandingProposals.get(zxid); + if (p == null) { + LOG.warn("Trying to commit future proposal: zxid 0x{} from {}", + Long.toHexString(zxid), followerAddr); + return; + } + + p.ackSet.add(sid); + if (LOG.isDebugEnabled()) { + LOG.debug("Count for zxid: 0x{} is {}", + Long.toHexString(zxid), p.ackSet.size()); + } + if (self.getQuorumVerifier().containsQuorum(p.ackSet)){ + if (zxid != lastCommitted+1) { + LOG.warn("Commiting zxid 0x{} from {} not first!", + Long.toHexString(zxid), followerAddr); + LOG.warn("First is 0x{}", Long.toHexString(lastCommitted + 1)); + } + outstandingProposals.remove(zxid); + if (p.request != null) { + toBeApplied.add(p); + } + + if (p.request == null) { + LOG.warn("Going to commmit null request for proposal: {}", p); + } + commit(zxid); + inform(p); + zk.commitProcessor.commit(p.request); + if(pendingSyncs.containsKey(zxid)){ + for(LearnerSyncRequest r: pendingSyncs.remove(zxid)) { + sendSync(r); + } + } + } + } + + static class ToBeAppliedRequestProcessor implements RequestProcessor { + private RequestProcessor next; + + private ConcurrentLinkedQueue toBeApplied; + + /** + * 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, + ConcurrentLinkedQueue toBeApplied) { + if (!(next instanceof FinalRequestProcessor)) { + throw new RuntimeException(ToBeAppliedRequestProcessor.class + .getName() + + " must be connected to " + + FinalRequestProcessor.class.getName() + + " not " + + next.getClass().getName()); + } + this.toBeApplied = toBeApplied; + this.next = next; + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.RequestProcessor#processRequest(org.apache.zookeeper.server.Request) + */ + public void processRequest(Request request) throws RequestProcessorException { + // request.addRQRec(">tobe"); + next.processRequest(request); + Proposal p = toBeApplied.peek(); + if (p != null && p.request != null + && p.request.zxid == request.zxid) { + toBeApplied.remove(); + } + } + + /* + * (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) { + 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); + } + + /** + * Create an inform packet and send it to all observers. + * @param zxid + * @param proposal + */ + public void inform(Proposal proposal) { + QuorumPacket qp = new QuorumPacket(Leader.INFORM, proposal.request.zxid, + proposal.packet.getData(), null); + sendObserverPacket(qp); + } + + long 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.setLastProposalSize(data.length); + QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, data, null); + + Proposal p = new Proposal(); + p.packet = pp; + p.request = request; + synchronized (this) { + if (LOG.isDebugEnabled()) { + LOG.debug("Proposing:: " + request); + } + + lastProposed = p.packet.getZxid(); + outstandingProposals.put(lastProposed, p); + sendPacket(pp); + } + return p; + } + + /** + * Process sync requests + * + * @param r the request + */ + + synchronized public void processSync(LearnerSyncRequest r){ + 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 + * + * @param f + * @param r + */ + + 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 + */ + synchronized public long startForwarding(LearnerHandler handler, + long lastSeenZxid) { + // Queue up any outstanding requests enabling the receipt of + // new requests + if (lastProposed > lastSeenZxid) { + for (Proposal p : toBeApplied) { + if (p.packet.getZxid() <= lastSeenZxid) { + continue; + } + handler.queuePacket(p.packet); + // Since the proposal has been committed we need to send the + // commit message also + 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) { + Listzxids = 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; + } + // VisibleForTesting + protected Set connectingFollowers = new HashSet(); + 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(); + long cur = start; + long end = start + self.getInitLimit()*self.getTickTime(); + while(waitingForNewEpoch && cur < end) { + connectingFollowers.wait(end - cur); + cur = Time.currentElapsedTime(); + } + if (waitingForNewEpoch) { + throw new InterruptedException("Timeout while waiting for epoch from quorum"); + } + } + return epoch; + } + } + // VisibleForTesting + protected Set electingFollowers = new HashSet(); + // VisibleForTesting + protected boolean electionFinished = false; + 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 (isParticipant(id)) { + electingFollowers.add(id); + } + } + QuorumVerifier verifier = self.getQuorumVerifier(); + if (electingFollowers.contains(self.getId()) && verifier.containsQuorum(electingFollowers)) { + electionFinished = true; + electingFollowers.notifyAll(); + } else { + long start = Time.currentElapsedTime(); + long cur = start; + long end = start + self.getInitLimit()*self.getTickTime(); + 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: [ " + + getSidSetString(newLeaderProposal.ackSet) + + " ]; starting up and setting last processed zxid: 0x{}", + Long.toHexString(zk.getZxid())); + 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 + */ + public void waitForNewLeaderAck(long sid, long zxid) + throws InterruptedException { + + synchronized (newLeaderProposal.ackSet) { + + if (quorumFormed) { + return; + } + + long currentZxid = newLeaderProposal.packet.getZxid(); + if (zxid != currentZxid) { + LOG.error("NEWLEADER ACK from sid: " + sid + + " is from a different epoch - current 0x" + + Long.toHexString(currentZxid) + " receieved 0x" + + Long.toHexString(zxid)); + return; + } + + if (isParticipant(sid)) { + newLeaderProposal.ackSet.add(sid); + } + + if (self.getQuorumVerifier().containsQuorum( + newLeaderProposal.ackSet)) { + quorumFormed = true; + newLeaderProposal.ackSet.notifyAll(); + } else { + long start = Time.currentElapsedTime(); + long cur = start; + long end = start + self.getInitLimit() * self.getTickTime(); + while (!quorumFormed && cur < end) { + newLeaderProposal.ackSet.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 PING: + return "PING"; + case REVALIDATE: + return "REVALIDATE"; + case SYNC: + return "SYNC"; + case INFORM: + return "INFORM"; + default: + return "UNKNOWN"; + } + } + + private boolean isRunning() { + return self.isRunning() && zk.isRunning(); + } + + private boolean isParticipant(long sid) { + return self.getVotingView().containsKey(sid); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LeaderBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderBean.java new file mode 100644 index 0000000..4862567 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderBean.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 org.apache.zookeeper.server.ZooKeeperServerBean; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.LearnerHandler; +import org.apache.zookeeper.server.quorum.Leader; + +/** + * 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()) { + sb.append(handler.toString()).append("\n"); + } + return sb.toString(); + } + + @Override + public long getElectionTimeTaken() { + return leader.self.getElectionTimeTaken(); + } + + @Override + public int getLastProposalSize() { + return leader.getProposalStats().getLastProposalSize(); + } + + @Override + public int getMinProposalSize() { + return leader.getProposalStats().getMinProposalSize(); + } + + @Override + public int getMaxProposalSize() { + return leader.getProposalStats().getMaxProposalSize(); + } + + @Override + public void resetProposalStatistics() { + leader.getProposalStats().reset(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LeaderElection.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElection.java new file mode 100644 index 0000000..a66ee78 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElection.java @@ -0,0 +1,295 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.quorum.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; + +/** + * @deprecated This class has been deprecated as of release 3.4.0. + */ +@Deprecated +public class LeaderElection implements Election { + private static final Logger LOG = LoggerFactory.getLogger(LeaderElection.class); + protected static final Random epochGen = new Random(); + + protected QuorumPeer self; + + public LeaderElection(QuorumPeer self) { + this.self = self; + } + + protected static class ElectionResult { + public Vote vote; + + public int count; + + public Vote winner; + + public int winningCount; + + public int numValidVotes; + } + + protected ElectionResult countVotes(HashMap votes, HashSet heardFrom) { + final ElectionResult result = new ElectionResult(); + // Initialize with null vote + result.vote = new Vote(Long.MIN_VALUE, Long.MIN_VALUE); + result.winner = new Vote(Long.MIN_VALUE, Long.MIN_VALUE); + + // First, filter out votes from unheard-from machines. Then + // make the views consistent. Sometimes peers will have + // different zxids for a server depending on timing. + final HashMap validVotes = new HashMap(); + final Map maxZxids = new HashMap(); + for (Map.Entry e : votes.entrySet()) { + // Only include votes from machines that we heard from + final Vote v = e.getValue(); + if (heardFrom.contains(v.getId())) { + validVotes.put(e.getKey(), v); + Long val = maxZxids.get(v.getId()); + if (val == null || val < v.getZxid()) { + maxZxids.put(v.getId(), v.getZxid()); + } + } + } + + // Make all zxids for a given vote id equal to the largest zxid seen for + // that id + for (Map.Entry e : validVotes.entrySet()) { + final Vote v = e.getValue(); + Long zxid = maxZxids.get(v.getId()); + if (v.getZxid() < zxid) { + // This is safe inside an iterator as per + // http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html + e.setValue(new Vote(v.getId(), zxid, v.getElectionEpoch(), v.getPeerEpoch(), v.getState())); + } + } + + result.numValidVotes = validVotes.size(); + + final HashMap countTable = new HashMap(); + // Now do the tally + for (Vote v : validVotes.values()) { + Integer count = countTable.get(v); + if (count == null) { + count = 0; + } + countTable.put(v, count + 1); + if (v.getId() == result.vote.getId()) { + result.count++; + } else if (v.getZxid() > result.vote.getZxid() + || (v.getZxid() == result.vote.getZxid() && v.getId() > result.vote.getId())) { + result.vote = v; + result.count = 1; + } + } + result.winningCount = 0; + LOG.info("Election tally: "); + for (Entry entry : countTable.entrySet()) { + if (entry.getValue() > result.winningCount) { + result.winningCount = entry.getValue(); + result.winner = entry.getKey(); + } + LOG.info(entry.getKey().getId() + "\t-> " + entry.getValue()); + } + return result; + } + + /** + * There is nothing to shutdown in this implementation of + * leader election, so we simply have an empty method. + */ + public void shutdown(){} + + /** + * Invoked in QuorumPeer to find or elect a new leader. + * + * @throws InterruptedException + */ + 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; + } + + try { + self.setCurrentVote(new Vote(self.getId(), + self.getLastLoggedZxid())); + // We are going to look for a leader by casting a vote for ourself + byte requestBytes[] = new byte[4]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + byte responseBytes[] = new byte[28]; + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + /* The current vote for the leader. Initially me! */ + DatagramSocket s = null; + try { + s = new DatagramSocket(); + s.setSoTimeout(200); + } catch (SocketException e1) { + LOG.error("Socket exception when creating socket for leader election", e1); + System.exit(4); + } + DatagramPacket requestPacket = new DatagramPacket(requestBytes, + requestBytes.length); + DatagramPacket responsePacket = new DatagramPacket(responseBytes, + responseBytes.length); + int xid = epochGen.nextInt(); + while (self.isRunning()) { + HashMap votes = + new HashMap(self.getVotingView().size()); + + requestBuffer.clear(); + requestBuffer.putInt(xid); + requestPacket.setLength(4); + HashSet heardFrom = new HashSet(); + for (QuorumServer server : self.getVotingView().values()) { + LOG.info("Server address: " + server.addr); + try { + requestPacket.setSocketAddress(server.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + server.addr, + e); + } + + try { + s.send(requestPacket); + responsePacket.setLength(responseBytes.length); + s.receive(responsePacket); + if (responsePacket.getLength() != responseBytes.length) { + LOG.error("Got a short response: " + + responsePacket.getLength()); + continue; + } + responseBuffer.clear(); + int recvedXid = responseBuffer.getInt(); + if (recvedXid != xid) { + LOG.error("Got bad xid: expected " + xid + + " got " + recvedXid); + continue; + } + long peerId = responseBuffer.getLong(); + heardFrom.add(peerId); + //if(server.id != peerId){ + Vote vote = new Vote(responseBuffer.getLong(), + responseBuffer.getLong()); + InetSocketAddress addr = + (InetSocketAddress) responsePacket + .getSocketAddress(); + votes.put(addr, vote); + //} + } catch (IOException e) { + LOG.warn("Ignoring exception while looking for leader", + e); + // Errors are okay, since hosts may be + // down + } + } + + ElectionResult result = countVotes(votes, heardFrom); + // ZOOKEEPER-569: + // If no votes are received for live peers, reset to voting + // for ourselves as otherwise we may hang on to a vote + // for a dead peer + if (result.numValidVotes == 0) { + self.setCurrentVote(new Vote(self.getId(), + self.getLastLoggedZxid())); + } else { + if (result.winner.getId() >= 0) { + self.setCurrentVote(result.vote); + // To do: this doesn't use a quorum verifier + if (result.winningCount > (self.getVotingView().size() / 2)) { + self.setCurrentVote(result.winner); + s.close(); + Vote current = self.getCurrentVote(); + LOG.info("Found leader: my type is: " + self.getLearnerType()); + /* + * We want to make sure we implement the state machine + * correctly. If we are a PARTICIPANT, once a leader + * is elected we can move either to LEADING or + * FOLLOWING. However if we are an OBSERVER, it is an + * error to be elected as a Leader. + */ + if (self.getLearnerType() == LearnerType.OBSERVER) { + if (current.getId() == self.getId()) { + // This should never happen! + LOG.error("OBSERVER elected as leader!"); + Thread.sleep(100); + } + else { + self.setPeerState(ServerState.OBSERVING); + Thread.sleep(100); + return current; + } + } else { + self.setPeerState((current.getId() == self.getId()) + ? ServerState.LEADING: ServerState.FOLLOWING); + if (self.getPeerState() == ServerState.FOLLOWING) { + Thread.sleep(100); + } + return current; + } + } + } + } + Thread.sleep(1000); + } + 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; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionBean.java new file mode 100644 index 0000000..7c20a58 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionBean.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.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/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.java new file mode 100644 index 0000000..406d153 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.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.server.quorum; + + +/** + * Leader election protocol MBean. + */ +public interface LeaderElectionMXBean { + /** + * + * @return the time when the leader election started + */ + public String getStartTime(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LeaderMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderMXBean.java new file mode 100644 index 0000000..7a1a439 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderMXBean.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; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Leader MBean. + */ +public interface LeaderMXBean extends ZooKeeperServerMXBean { + /** + * Current zxid of cluster. + */ + public String getCurrentZxid(); + + /** + * @return information on current followers + */ + public String followerInfo(); + + /** + * @return time taken for leader election in milliseconds. + */ + public long getElectionTimeTaken(); + + /** + * @return size of latest generated proposal + */ + public int getLastProposalSize(); + + /** + * @return size of smallest generated proposal + */ + public int getMinProposalSize(); + + /** + * @return size of largest generated proposal + */ + public int getMaxProposalSize(); + + /** + * Resets statistics of proposal size (min/max/last) + */ + public void resetProposalStatistics(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java new file mode 100644 index 0000000..e10cc49 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java @@ -0,0 +1,182 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.SessionExpiredException; +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.ServerCnxn; +import org.apache.zookeeper.server.SessionTrackerImpl; +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 { + CommitProcessor commitProcessor; + + /** + * @param port + * @param dataDir + * @throws IOException + */ + LeaderZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, + DataTreeBuilder treeBuilder, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, + self.maxSessionTimeout, treeBuilder, zkDb, self); + } + + public Leader getLeader(){ + return self.leader; + } + + @Override + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + RequestProcessor toBeAppliedProcessor = new Leader.ToBeAppliedRequestProcessor( + finalProcessor, getLeader().toBeApplied); + commitProcessor = new CommitProcessor(toBeAppliedProcessor, + Long.toString(getServerId()), false, + getZooKeeperServerListener()); + commitProcessor.start(); + ProposalRequestProcessor proposalProcessor = new ProposalRequestProcessor(this, + commitProcessor); + proposalProcessor.initialize(); + firstProcessor = new PrepRequestProcessor(this, proposalProcessor); + ((PrepRequestProcessor)firstProcessor).start(); + } + + @Override + public int getGlobalOutstandingLimit() { + return super.getGlobalOutstandingLimit() / (self.getQuorumSize() - 1); + } + + @Override + public void createSessionTracker() { + sessionTracker = new SessionTrackerImpl(this, getZKDatabase() + .getSessionWithTimeOuts(), tickTime, self.getId(), + getZooKeeperServerListener()); + } + + @Override + protected void startSessionTracker() { + ((SessionTrackerImpl)sessionTracker).start(); + } + + + public boolean touch(long sess, int to) { + return sessionTracker.touchSession(sess, to); + } + + @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; + } + } + + @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/src/java/main/org/apache/zookeeper/server/quorum/Learner.java b/src/java/main/org/apache/zookeeper/server/quorum/Learner.java new file mode 100644 index 0000000..1645cda --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Learner.java @@ -0,0 +1,570 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.File; +import java.io.IOException; +import java.net.ConnectException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map.Entry; +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.server.Request; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.txn.TxnHeader; +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; + } + QuorumPeer self; + LearnerZooKeeperServer zk; + + protected BufferedOutputStream bufferedOutput; + + protected Socket sock; + + /** + * Socket getter + * @return + */ + public Socket getSocket() { + return sock; + } + + protected InputArchive leaderIs; + protected OutputArchive leaderOs; + /** the protocol version of the leader */ + protected int leaderProtocolVersion = 0x01; + + protected static final Logger LOG = LoggerFactory.getLogger(Learner.class); + + static final private boolean nodelay = System.getProperty("follower.nodelay", "true").equals("true"); + static { + LOG.info("TCP NoDelay set to: " + nodelay); + } + + final ConcurrentHashMap pendingRevalidations = + new ConcurrentHashMap(); + + public int getPendingRevalidationsCount() { + return pendingRevalidations.size(); + } + + /** + * validate a session for a client + * + * @param clientId + * the client to be revalidated + * @param timeout + * the timeout for which the session is valid + * @return + * @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 + * + * @param pp + * the proposal packet to be sent to the leader + * @throws IOException + */ + void writePacket(QuorumPacket pp, boolean flush) throws IOException { + synchronized (leaderOs) { + if (pp != null) { + leaderOs.writeRecord(pp, "packet"); + } + if (flush) { + bufferedOutput.flush(); + } + } + } + + /** + * 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"); + } + long traceMask = ZooTrace.SERVER_PACKET_TRACE_MASK; + if (pp.getType() == Leader.PING) { + traceMask = ZooTrace.SERVER_PING_TRACE_MASK; + } + if (LOG.isTraceEnabled()) { + 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(); + 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; + // Find the leader by id + Vote current = self.getCurrentVote(); + 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(); + leaderServer = s; + break; + } + } + if (leaderServer == null) { + LOG.warn("Couldn't find the leader with id = " + + current.getId()); + } + return leaderServer; + } + + /** + * Establish a connection with the Leader found by findLeader. Retries + * 5 times before giving up. + * @param addr - the address of the Leader to connect to. + * @throws IOException
  • if the socket connection fails on the 5th attempt
  • + *
  • if there is an authentication failure while connecting to leader
  • + * @throws ConnectException + * @throws InterruptedException + */ + protected void connectToLeader(InetSocketAddress addr, String hostname) + throws IOException, ConnectException, InterruptedException { + sock = new Socket(); + sock.setSoTimeout(self.tickTime * self.initLimit); + for (int tries = 0; tries < 5; tries++) { + try { + sock.connect(addr, self.tickTime * self.syncLimit); + sock.setTcpNoDelay(nodelay); + break; + } catch (IOException e) { + if (tries == 4) { + LOG.error("Unexpected exception",e); + throw e; + } else { + LOG.warn("Unexpected exception, tries="+tries+ + ", connecting to " + addr,e); + sock = new Socket(); + sock.setSoTimeout(self.tickTime * self.initLimit); + } + } + Thread.sleep(1000); + } + + self.authLearner.authenticate(sock, hostname); + + leaderIs = BinaryInputArchive.getArchive(new BufferedInputStream( + sock.getInputStream())); + bufferedOutput = new BufferedOutputStream(sock.getOutputStream()); + leaderOs = BinaryOutputArchive.getArchive(bufferedOutput); + } + + /** + * Once connected to the leader, perform the handshake protocol to + * establish a following / observing connection. + * @param pktType + * @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(); + 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); + ByteArrayOutputStream bsid = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bsid); + boa.writeRecord(li, "LearnerInfo"); + qp.setData(bsid.toByteArray()); + + writePacket(qp, true); + 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()); + } + 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. + * @param newLeaderZxid + * @throws IOException + * @throws InterruptedException + */ + protected void syncWithLeader(long newLeaderZxid) throws IOException, InterruptedException{ + QuorumPacket ack = new QuorumPacket(Leader.ACK, 0, null, null); + QuorumPacket qp = new QuorumPacket(); + long newEpoch = ZxidUtils.getEpochFromZxid(newLeaderZxid); + // 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; + readPacket(qp); + LinkedList packetsCommitted = new LinkedList(); + LinkedList packetsNotCommitted = new LinkedList(); + synchronized (zk) { + if (qp.getType() == Leader.DIFF) { + LOG.info("Getting a diff from the leader 0x{}", Long.toHexString(qp.getZxid())); + snapshotNeeded = false; + } + else if (qp.getType() == Leader.SNAP) { + LOG.info("Getting a snapshot from leader 0x" + Long.toHexString(qp.getZxid())); + // The leader is going to dump the database + // clear our own database and read + zk.getZKDatabase().clear(); + zk.getZKDatabase().deserializeSnapshot(leaderIs); + 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()); + } else if (qp.getType() == Leader.TRUNC) { + //we need to truncate the log to the lastzxid of the leader + 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 " + + Long.toHexString(qp.getZxid())); + System.exit(13); + } + zk.getZKDatabase().setlastProcessedZxid(qp.getZxid()); + } + else { + LOG.error("Got unexpected packet from leader " + + qp.getType() + " exiting ... " ); + System.exit(13); + + } + 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; + // we are now going to start getting transactions to apply followed by an UPTODATE + outerLoop: + while (self.isRunning()) { + readPacket(qp); + switch(qp.getType()) { + case Leader.PROPOSAL: + PacketInFlight pif = new PacketInFlight(); + pif.hdr = new TxnHeader(); + pif.rec = SerializeUtils.deserializeTxn(qp.getData(), pif.hdr); + if (pif.hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x" + + Long.toHexString(pif.hdr.getZxid()) + + " expected 0x" + + Long.toHexString(lastQueued + 1)); + } + lastQueued = pif.hdr.getZxid(); + packetsNotCommitted.add(pif); + break; + case Leader.COMMIT: + if (!writeToTxnLog) { + pif = packetsNotCommitted.peekFirst(); + if (pif.hdr.getZxid() != qp.getZxid()) { + LOG.warn("Committing " + qp.getZxid() + ", but next proposal is " + pif.hdr.getZxid()); + } else { + zk.processTxn(pif.hdr, pif.rec); + packetsNotCommitted.remove(); + } + } else { + packetsCommitted.add(qp.getZxid()); + } + break; + case Leader.INFORM: + /* + * Only observer get this type of packet. We treat this + * as receiving PROPOSAL and COMMMIT. + */ + PacketInFlight packet = new PacketInFlight(); + packet.hdr = new TxnHeader(); + packet.rec = SerializeUtils.deserializeTxn(qp.getData(), packet.hdr); + // Log warning message if txn comes out-of-order + if (packet.hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x" + + Long.toHexString(packet.hdr.getZxid()) + + " expected 0x" + + 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; + case Leader.UPTODATE: + if (isPreZAB1_0) { + zk.takeSnapshot(); + self.setCurrentEpoch(newEpoch); + } + self.cnxnFactory.setZooKeeperServer(zk); + break outerLoop; + case Leader.NEWLEADER: // Getting NEWLEADER here instead of in discovery + // means this is Zab 1.0 + // Create updatingEpoch file and remove it after current + // epoch is set. QuorumPeer.loadDataBase() uses this file to + // detect the case where the server was terminated after + // taking a snapshot but before setting the current epoch. + File updating = new File(self.getTxnFactory().getSnapDir(), + QuorumPeer.UPDATING_EPOCH_FILENAME); + if (!updating.exists() && !updating.createNewFile()) { + throw new IOException("Failed to create " + + updating.toString()); + } + if (snapshotNeeded) { + zk.takeSnapshot(); + } + self.setCurrentEpoch(newEpoch); + if (!updating.delete()) { + throw new IOException("Failed to delete " + + updating.toString()); + } + writeToTxnLog = true; //Anything after this needs to go to the transaction log, not applied directly in memory + isPreZAB1_0 = false; + writePacket(new QuorumPacket(Leader.ACK, newLeaderZxid, null, null), true); + break; + } + } + } + ack.setZxid(ZxidUtils.makeZxid(newEpoch, 0)); + writePacket(ack, true); + sock.setSoTimeout(self.tickTime * self.syncLimit); + 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(newEpoch); + + // 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); + } + 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 " + Long.toHexString(zxid) + + ", but next proposal is " + + 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.txn = p.rec; + request.hdr = p.hdr; + 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" + + Long.toHexString(sessionId) + + " for validation"); + } 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); + HashMap touchTable = zk + .getTouchSnapshot(); + for (Entry entry : touchTable.entrySet()) { + dos.writeLong(entry.getKey()); + dos.writeInt(entry.getValue()); + } + qp.setData(bos.toByteArray()); + writePacket(qp, true); + } + + + /** + * Shutdown the Peer + */ + public void shutdown() { + // set the zookeeper server to null + self.cnxnFactory.setZooKeeperServer(null); + // clear all the connections + self.cnxnFactory.closeAll(); + // shutdown previous zookeeper + if (zk != null) { + zk.shutdown(); + } + } + + boolean isRunning() { + return self.isRunning() && zk.isRunning(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LearnerHandler.java b/src/java/main/org/apache/zookeeper/server/quorum/LearnerHandler.java new file mode 100644 index 0000000..973950d --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LearnerHandler.java @@ -0,0 +1,715 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.Socket; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.concurrent.LinkedBlockingQueue; +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.jute.Record; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.Request; +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.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.txn.TxnHeader; +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 Leader leader; + + /** 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; + } + + protected int version = 0x1; + + int getVersion() { + return version; + } + + /** + * The packets to be sent to the learner + */ + final LinkedBlockingQueue queuedPackets = + new LinkedBlockingQueue(); + + /** + * 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 " + zxid + " received before ACK for " + currentZxid + "!!!!"); + nextTime = 0; + nextZxid = 0; + } + } + + public synchronized boolean check(long time) { + if (currentTime == 0) { + return true; + } else { + long msDelay = (time - currentTime) / 1000000; + return (msDelay < (leader.self.tickTime * leader.self.syncLimit)); + } + } + }; + + private SyncLimitCheck syncLimitCheck = new SyncLimitCheck(); + + private BinaryInputArchive ia; + + private BinaryOutputArchive oa; + + private final BufferedInputStream bufferedInput; + private BufferedOutputStream bufferedOutput; + + LearnerHandler(Socket sock, BufferedInputStream bufferedInput, + Leader leader) throws IOException { + super("LearnerHandler-" + sock.getRemoteSocketAddress()); + this.sock = sock; + this.leader = leader; + this.bufferedInput = bufferedInput; + try { + leader.self.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()); + } + } + + @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 { + QuorumPacket p; + p = queuedPackets.poll(); + if (p == null) { + bufferedOutput.flush(); + p = queuedPackets.take(); + } + + 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); + } + oa.writeRecord(p, "packet"); + } 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; + } + } + } + + static public String packetToString(QuorumPacket p) { + String type = null; + String mess = null; + Record txn = 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"; + TxnHeader hdr = new TxnHeader(); + try { + SerializeUtils.deserializeTxn(p.getData(), hdr); + // mess = "transaction: " + txn.toString(); + } catch (IOException e) { + LOG.warn("Unexpected exception",e); + } + 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; + 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 { + leader.addLearnerHandler(this); + tickOfNextAckDeadline = leader.self.tick.get() + + leader.self.initLimit + leader.self.syncLimit; + + ia = BinaryInputArchive.getArchive(bufferedInput); + bufferedOutput = new BufferedOutputStream(sock.getOutputStream()); + oa = BinaryOutputArchive.getArchive(bufferedOutput); + + QuorumPacket qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + if(qp.getType() != Leader.FOLLOWERINFO && qp.getType() != Leader.OBSERVERINFO){ + LOG.error("First packet " + qp.toString() + + " is not FOLLOWERINFO or OBSERVERINFO!"); + return; + } + byte learnerInfoData[] = qp.getData(); + if (learnerInfoData != null) { + if (learnerInfoData.length == 8) { + ByteBuffer bbsid = ByteBuffer.wrap(learnerInfoData); + this.sid = bbsid.getLong(); + } else { + LearnerInfo li = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(learnerInfoData), li); + this.sid = li.getServerid(); + this.version = li.getProtocolVersion(); + } + } else { + this.sid = leader.followerCounter.getAndDecrement(); + } + + LOG.info("Follower sid: " + sid + " : info : " + + leader.self.quorumPeers.get(sid)); + + if (qp.getType() == Leader.OBSERVERINFO) { + learnerType = LearnerType.OBSERVER; + } + + long lastAcceptedEpoch = ZxidUtils.getEpochFromZxid(qp.getZxid()); + + long peerLastZxid; + StateSummary ss = null; + long zxid = qp.getZxid(); + long newEpoch = leader.getEpochToPropose(this.getSid(), lastAcceptedEpoch); + + 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 + leader.waitForEpochAck(this.getSid(), ss); + } else { + byte ver[] = new byte[4]; + ByteBuffer.wrap(ver).putInt(0x10000); + QuorumPacket newEpochPacket = new QuorumPacket(Leader.LEADERINFO, ZxidUtils.makeZxid(newEpoch, 0), ver, null); + oa.writeRecord(newEpochPacket, "packet"); + bufferedOutput.flush(); + QuorumPacket ackEpochPacket = new QuorumPacket(); + ia.readRecord(ackEpochPacket, "packet"); + if (ackEpochPacket.getType() != Leader.ACKEPOCH) { + LOG.error(ackEpochPacket.toString() + + " is not ACKEPOCH"); + return; + } + ByteBuffer bbepoch = ByteBuffer.wrap(ackEpochPacket.getData()); + ss = new StateSummary(bbepoch.getInt(), ackEpochPacket.getZxid()); + leader.waitForEpochAck(this.getSid(), ss); + } + peerLastZxid = ss.getLastZxid(); + + /* the default to send to the follower */ + int packetToSend = Leader.SNAP; + long zxidToSend = 0; + long leaderLastZxid = 0; + /** the packets that the follower needs to get updates from **/ + long updates = peerLastZxid; + + /* we are sending the diff check if we have proposals in memory to be able to + * send a diff to the + */ + ReentrantReadWriteLock lock = leader.zk.getZKDatabase().getLogLock(); + ReadLock rl = lock.readLock(); + try { + rl.lock(); + final long maxCommittedLog = leader.zk.getZKDatabase().getmaxCommittedLog(); + final long minCommittedLog = leader.zk.getZKDatabase().getminCommittedLog(); + LOG.info("Synchronizing with Follower sid: " + sid + +" maxCommittedLog=0x"+Long.toHexString(maxCommittedLog) + +" minCommittedLog=0x"+Long.toHexString(minCommittedLog) + +" peerLastZxid=0x"+Long.toHexString(peerLastZxid)); + + LinkedList proposals = leader.zk.getZKDatabase().getCommittedLog(); + + if (peerLastZxid == leader.zk.getZKDatabase().getDataTreeLastProcessedZxid()) { + // Follower is already sync with us, send empty diff + LOG.info("leader and follower are in sync, zxid=0x{}", + Long.toHexString(peerLastZxid)); + packetToSend = Leader.DIFF; + zxidToSend = peerLastZxid; + } else if (proposals.size() != 0) { + LOG.debug("proposal size is {}", proposals.size()); + if ((maxCommittedLog >= peerLastZxid) + && (minCommittedLog <= peerLastZxid)) { + LOG.debug("Sending proposals to follower"); + + // as we look through proposals, this variable keeps track of previous + // proposal Id. + long prevProposalZxid = minCommittedLog; + + // Keep track of whether we are about to send the first packet. + // Before sending the first packet, we have to tell the learner + // whether to expect a trunc or a diff + boolean firstPacket=true; + + // If we are here, we can use committedLog to sync with + // follower. Then we only need to decide whether to + // send trunc or not + packetToSend = Leader.DIFF; + zxidToSend = maxCommittedLog; + + for (Proposal propose: proposals) { + // skip the proposals the peer already has + if (propose.packet.getZxid() <= peerLastZxid) { + prevProposalZxid = propose.packet.getZxid(); + continue; + } else { + // If we are sending the first packet, figure out whether to trunc + // in case the follower has some proposals that the leader doesn't + if (firstPacket) { + firstPacket = false; + // Does the peer have some proposals that the leader hasn't seen yet + if (prevProposalZxid < peerLastZxid) { + // send a trunc message before sending the diff + packetToSend = Leader.TRUNC; + zxidToSend = prevProposalZxid; + updates = zxidToSend; + } + } + queuePacket(propose.packet); + QuorumPacket qcommit = new QuorumPacket(Leader.COMMIT, propose.packet.getZxid(), + null, null); + queuePacket(qcommit); + } + } + } else if (peerLastZxid > maxCommittedLog) { + LOG.debug("Sending TRUNC to follower zxidToSend=0x{} updates=0x{}", + Long.toHexString(maxCommittedLog), + Long.toHexString(updates)); + + packetToSend = Leader.TRUNC; + zxidToSend = maxCommittedLog; + updates = zxidToSend; + } else { + LOG.warn("Unhandled proposal scenario"); + } + } else { + // just let the state transfer happen + LOG.debug("proposals is empty"); + } + + LOG.info("Sending " + Leader.getPacketType(packetToSend)); + leaderLastZxid = leader.startForwarding(this, updates); + + } finally { + rl.unlock(); + } + + QuorumPacket newLeaderQP = new QuorumPacket(Leader.NEWLEADER, + ZxidUtils.makeZxid(newEpoch, 0), null, null); + if (getVersion() < 0x10000) { + oa.writeRecord(newLeaderQP, "packet"); + } else { + queuedPackets.add(newLeaderQP); + } + bufferedOutput.flush(); + //Need to set the zxidToSend to the latest zxid + if (packetToSend == Leader.SNAP) { + zxidToSend = leader.zk.getZKDatabase().getDataTreeLastProcessedZxid(); + } + oa.writeRecord(new QuorumPacket(packetToSend, zxidToSend, null, null), "packet"); + bufferedOutput.flush(); + + /* if we are not truncating or sending a diff just send a snapshot */ + if (packetToSend == Leader.SNAP) { + LOG.info("Sending snapshot last zxid of peer is 0x" + + Long.toHexString(peerLastZxid) + " " + + " zxid of leader is 0x" + + Long.toHexString(leaderLastZxid) + + "sent zxid of db as 0x" + + Long.toHexString(zxidToSend)); + // Dump data to peer + leader.zk.getZKDatabase().serializeSnapshot(oa); + oa.writeString("BenWasHere", "signature"); + } + bufferedOutput.flush(); + + // Start sending packets + new Thread() { + public void run() { + Thread.currentThread().setName( + "Sender-" + sock.getRemoteSocketAddress()); + try { + sendPackets(); + } catch (InterruptedException e) { + LOG.warn("Unexpected interruption",e); + } + } + }.start(); + + /* + * Have to wait for the first ACK, wait until + * the leader is ready, and only then we can + * start processing messages. + */ + qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + if(qp.getType() != Leader.ACK){ + LOG.error("Next packet was supposed to be an ACK"); + return; + } + LOG.info("Received NEWLEADER-ACK message from " + getSid()); + leader.waitForNewLeaderAck(getSid(), qp.getZxid()); + + syncLimitCheck.start(); + + // now that the ack has been processed expect the syncLimit + sock.setSoTimeout(leader.self.tickTime * leader.self.syncLimit); + + /* + * Wait until leader starts up + */ + synchronized(leader.zk){ + while(!leader.zk.isRunning() && !this.isInterrupted()){ + leader.zk.wait(20); + } + } + // Mutation packets will be queued during the serialize, + // so we need to mark when the peer can actually start + // using the data + // + queuedPackets.add(new QuorumPacket(Leader.UPTODATE, -1, null, null)); + + while (true) { + qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + + 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 = leader.self.tick.get() + leader.self.syncLimit; + + + ByteBuffer bb; + long sessionId; + int cxid; + int type; + + switch (qp.getType()) { + case Leader.ACK: + if (this.learnerType == LearnerType.OBSERVER) { + if (LOG.isDebugEnabled()) { + LOG.debug("Received ACK from Observer " + this.sid); + } + } + syncLimitCheck.updateAck(qp.getZxid()); + leader.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(); + leader.zk.touch(sess, to); + } + break; + case Leader.REVALIDATE: + bis = new ByteArrayInputStream(qp.getData()); + 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 = leader.zk.touch(id, to); + if (valid) { + try { + //set the session owner + // as the follower that + // owns the session + leader.zk.setOwner(id, this); + } catch (SessionExpiredException e) { + LOG.error("Somehow session " + Long.toHexString(id) + " expired right after being renewed! (impossible)", 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()); + queuedPackets.add(qp); + break; + case Leader.REQUEST: + bb = ByteBuffer.wrap(qp.getData()); + sessionId = bb.getLong(); + cxid = bb.getInt(); + type = bb.getInt(); + bb = bb.slice(); + Request si; + if(type == OpCode.sync){ + si = new LearnerSyncRequest(this, sessionId, cxid, type, bb, qp.getAuthinfo()); + } else { + si = new Request(null, sessionId, cxid, type, bb, qp.getAuthinfo()); + } + si.setOwner(this); + leader.zk.submitRequest(si); + 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 causing shutdown", e); + } finally { + LOG.warn("******* GOODBYE " + + (sock != null ? sock.getRemoteSocketAddress() : "") + + " ********"); + shutdown(); + } + } + + public void shutdown() { + // Send the packet of death + try { + 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(); + leader.removeLearnerHandler(this); + } + + public long tickOfNextAckDeadline() { + return tickOfNextAckDeadline; + } + + /** + * ping calls from the leader to the peers + */ + public void ping() { + long id; + if (syncLimitCheck.check(System.nanoTime())) { + synchronized(leader) { + id = leader.lastProposed; + } + QuorumPacket ping = new QuorumPacket(Leader.PING, id, null, null); + queuePacket(ping); + } else { + LOG.warn("Closing connection to peer due to transaction timeout."); + shutdown(); + } + } + + void queuePacket(QuorumPacket p) { + queuedPackets.add(p); + } + + public boolean synced() { + return isAlive() + && leader.self.tick.get() <= tickOfNextAckDeadline; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LearnerSessionTracker.java b/src/java/main/org/apache/zookeeper/server/quorum/LearnerSessionTracker.java new file mode 100644 index 0000000..72f007e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LearnerSessionTracker.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.quorum; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.zookeeper.server.SessionTracker; +import org.apache.zookeeper.server.SessionTrackerImpl; +import org.apache.zookeeper.server.ZooKeeperServerListener; + +/** + * This is really just a shell of a SessionTracker that tracks session activity + * to be forwarded to the Leader using a PING. + */ +public class LearnerSessionTracker implements SessionTracker { + SessionExpirer expirer; + + HashMap touchTable = new HashMap(); + long serverId = 1; + long nextSessionId=0; + + private ConcurrentHashMap sessionsWithTimeouts; + + public LearnerSessionTracker(SessionExpirer expirer, + ConcurrentHashMap sessionsWithTimeouts, long id, + ZooKeeperServerListener listener) { + this.expirer = expirer; + this.sessionsWithTimeouts = sessionsWithTimeouts; + this.serverId = id; + nextSessionId = SessionTrackerImpl.initializeNextSession(this.serverId); + + } + + synchronized public void removeSession(long sessionId) { + sessionsWithTimeouts.remove(sessionId); + touchTable.remove(sessionId); + } + + public void shutdown() { + } + + synchronized public void addSession(long sessionId, int sessionTimeout) { + sessionsWithTimeouts.put(sessionId, sessionTimeout); + touchTable.put(sessionId, sessionTimeout); + } + + synchronized public boolean touchSession(long sessionId, int sessionTimeout) { + touchTable.put(sessionId, sessionTimeout); + return true; + } + + synchronized HashMap snapshot() { + HashMap oldTouchTable = touchTable; + touchTable = new HashMap(); + return oldTouchTable; + } + + + synchronized public long createSession(int sessionTimeout) { + return (nextSessionId++); + } + + public void checkSession(long sessionId, Object owner) { + // Nothing to do here. Sessions are checked at the Leader + } + + public void setOwner(long sessionId, Object owner) { + // Nothing to do here. Sessions are checked at the Leader + } + + public void dumpSessions(PrintWriter pwriter) { + // the original class didn't have tostring impl, so just + // dup what we had before + pwriter.println(toString()); + } + + public void setSessionClosing(long sessionId) { + // Nothing to do here. + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LearnerSyncRequest.java b/src/java/main/org/apache/zookeeper/server/quorum/LearnerSyncRequest.java new file mode 100644 index 0000000..bfbc9a8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LearnerSyncRequest.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; + +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/src/java/main/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java new file mode 100644 index 0000000..a1744ea --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java @@ -0,0 +1,146 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.HashMap; + +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.DataTreeBean; +import org.apache.zookeeper.server.ServerCnxn; +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 { + public LearnerZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, + int minSessionTimeout, int maxSessionTimeout, + DataTreeBuilder treeBuilder, ZKDatabase zkDb, QuorumPeer self) + throws IOException + { + super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, + treeBuilder, 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. + */ + abstract public 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 HashMap getTouchSnapshot() { + if (sessionTracker != null) { + return ((LearnerSessionTracker) sessionTracker).snapshot(); + } + return new HashMap(); + } + + /** + * 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() { + sessionTracker = new LearnerSessionTracker(this, getZKDatabase() + .getSessionWithTimeOuts(), self.getId(), + getZooKeeperServerListener()); + } + + @Override + protected void startSessionTracker() {} + + @Override + protected void revalidateSession(ServerCnxn cnxn, long sessionId, + int sessionTimeout) throws IOException { + 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; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java new file mode 100644 index 0000000..0d9fa70 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.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; + + +/** + * 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 int getTick() { + return peer.getTick(); + } + + public String getState() { + return peer.getServerState(); + } + + public String getQuorumAddress() { + return peer.getQuorumAddress().toString(); + } + + public int getElectionType() { + return peer.getElectionType(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerMXBean.java new file mode 100644 index 0000000..eedf308 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/LocalPeerMXBean.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; + + +/** + * 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 + */ + public int getTickTime(); + + /** Current maxClientCnxns allowed from a particular host */ + public int getMaxClientCnxnsPerHost(); + + /** + * @return the minimum number of milliseconds allowed for a session timeout + */ + public int getMinSessionTimeout(); + + /** + * @return the maximum number of milliseconds allowed for a session timeout + */ + public int getMaxSessionTimeout(); + + /** + * @return the number of ticks that the initial sync phase can take + */ + public int getInitLimit(); + + /** + * @return the number of ticks that can pass between sending a request + * and getting a acknowledgment + */ + public int getSyncLimit(); + + /** + * @return the current tick + */ + public int getTick(); + + /** + * @return the current server state + */ + public String getState(); + + /** + * @return the quorum address + */ + public String getQuorumAddress(); + + /** + * @return the election type + */ + public int getElectionType(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/Observer.java b/src/java/main/org/apache/zookeeper/server/quorum/Observer.java new file mode 100644 index 0000000..ded4a24 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Observer.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 java.io.IOException; + +import org.apache.jute.Record; +import org.apache.zookeeper.server.ObserverBean; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * 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{ + + 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 InterruptedException + */ + void observeLeader() throws InterruptedException { + zk.registerJMX(new ObserverBean(this, zk), self.jmxLocalPeerBean); + + try { + QuorumServer leaderServer = findLeader(); + LOG.info("Observing " + leaderServer.addr); + try { + connectToLeader(leaderServer.addr, leaderServer.hostname); + long newLeaderZxid = registerWithLeader(Leader.OBSERVERINFO); + + syncWithLeader(newLeaderZxid); + QuorumPacket qp = new QuorumPacket(); + while (this.isRunning()) { + readPacket(qp); + processPacket(qp); + } + } catch (Exception e) { + LOG.warn("Exception when observing the leader", e); + try { + sock.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + // clear pending revalidations + pendingRevalidations.clear(); + } + } finally { + zk.unregisterJMX(this); + } + } + + /** + * Controls the response of an observer to the receipt of a quorumpacket + * @param qp + * @throws IOException + */ + protected void processPacket(QuorumPacket qp) throws IOException{ + 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: + TxnHeader hdr = new TxnHeader(); + Record txn = SerializeUtils.deserializeTxn(qp.getData(), hdr); + Request request = new Request (null, hdr.getClientId(), + hdr.getCxid(), + hdr.getType(), null, null); + request.txn = txn; + request.hdr = hdr; + ObserverZooKeeperServer obs = (ObserverZooKeeperServer)zk; + obs.commitRequest(request); + break; + default: + LOG.error("Invalid packet type: {} received by Observer", qp.getType()); + } + } + + /** + * Shutdown the Observer. + */ + public void shutdown() { + LOG.info("shutdown called", new Exception("shutdown Observer")); + super.shutdown(); + } +} + diff --git a/src/java/main/org/apache/zookeeper/server/quorum/ObserverMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/ObserverMXBean.java new file mode 100644 index 0000000..2c1799a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ObserverMXBean.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.quorum; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Observer MX Bean interface, implemented by ObserverBean + * + */ +public interface ObserverMXBean extends ZooKeeperServerMXBean { + /** + * @return count of pending revalidations + */ + public int getPendingRevalidationCount(); + + /** + * @return socket address + */ + public String getQuorumAddress(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.java new file mode 100644 index 0000000..c9c5370 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.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.server.quorum; + +import java.util.concurrent.LinkedBlockingQueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooTrace; + +/** + * 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; + } + // 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.delete: + case OpCode.setData: + case OpCode.setACL: + case OpCode.createSession: + case OpCode.closeSession: + case OpCode.multi: + 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) { + 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/src/java/main/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.java new file mode 100644 index 0000000..f024948 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.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 java.io.IOException; +import java.util.concurrent.ConcurrentLinkedQueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +/** + * 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(); + + /* + * Request processors + */ + private CommitProcessor commitProcessor; + private SyncRequestProcessor syncProcessor; + + /* + * Pending sync requests + */ + ConcurrentLinkedQueue pendingSyncs = + new ConcurrentLinkedQueue(); + + ObserverZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, + DataTreeBuilder treeBuilder, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, + self.maxSessionTimeout, treeBuilder, 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 + */ + synchronized public 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(); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java new file mode 100644 index 0000000..641262e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.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.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; + AckRequestProcessor ackProcessor = new AckRequestProcessor(zks.getLeader()); + 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"); + + + /* 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. 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. + */ + + if(request instanceof LearnerSyncRequest){ + zks.getLeader().processSync((LearnerSyncRequest)request); + } else { + nextProcessor.processRequest(request); + if (request.hdr != null) { + // We need to sync and get consensus on any transactions + try { + zks.getLeader().propose(request); + } catch (XidRolloverException e) { + throw new RequestProcessorException(e.getMessage(), e); + } + syncProcessor.processRequest(request); + } + } + } + + public void shutdown() { + LOG.info("Shutting down"); + nextProcessor.shutdown(); + syncProcessor.shutdown(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/ProposalStats.java b/src/java/main/org/apache/zookeeper/server/quorum/ProposalStats.java new file mode 100644 index 0000000..2f3a9c7 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ProposalStats.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; + +/** + * Provides live statistics about a running Leader. + */ +public class ProposalStats { + /** + * Size of the last generated proposal. This should fit into server's jute.maxbuffer setting. + */ + private int lastProposalSize = -1; + + /** + * Size of the smallest proposal which has been generated since the server was started. + */ + private int minProposalSize = -1; + + /** + * Size of the largest proposal which has been generated since the server was started. + */ + private int maxProposalSize = -1; + + public synchronized int getLastProposalSize() { + return lastProposalSize; + } + + synchronized void setLastProposalSize(int value) { + lastProposalSize = value; + if (minProposalSize == -1 || value < minProposalSize) { + minProposalSize = value; + } + if (value > maxProposalSize) { + maxProposalSize = value; + } + } + + public synchronized int getMinProposalSize() { + return minProposalSize; + } + + public synchronized int getMaxProposalSize() { + return maxProposalSize; + } + + public synchronized void reset() { + lastProposalSize = -1; + minProposalSize = -1; + maxProposalSize = -1; + } + + public synchronized String toString() { + return String.format("%d/%d/%d", lastProposalSize, minProposalSize, maxProposalSize); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumBean.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumBean.java new file mode 100644 index 0000000..ef4036a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumBean.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.quorum; + +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.quorum.QuorumPeer; + +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.getMyid(); + } + + public String getName() { + return name; + } + + public boolean isHidden() { + return false; + } + + public int getQuorumSize() { + return peer.getQuorumSize(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java new file mode 100644 index 0000000..ec6be4a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java @@ -0,0 +1,1152 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.nio.channels.UnresolvedAddressException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ConcurrentHashMap; +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.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; + +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; + + static final int PACKETMAXSIZE = 1024 * 512; + + /* + * Max buffer size to be read from the network. + */ + static public final int maxBuffer = 2048; + + /* + * Negative counter for observer server ids. + */ + + private AtomicLong observerCounter = new AtomicLong(-1); + + /* + * Connection time out value in milliseconds + */ + + private int cnxTO = 5000; + + /* + * Local IP address + */ + final long mySid; + final int socketTimeout; + final Map view; + final boolean tcpKeepAlive = Boolean.getBoolean("zookeeper.tcpKeepAlive"); + 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 ArrayBlockingQueue recvQueue; + /* + * Object to synchronize access to recvQueue + */ + private final Object recvQLock = new Object(); + + /* + * Shutdown flag + */ + + volatile boolean shutdown = false; + + /* + * Listener thread + */ + public final Listener listener; + + /* + * Counter to count worker threads + */ + private AtomicInteger threadCnt = new AtomicInteger(0); + + static public class Message { + + Message(ByteBuffer buffer, long sid) { + this.buffer = buffer; + this.sid = sid; + } + + ByteBuffer buffer; + long sid; + } + + public QuorumCnxManager(final long mySid, + Map view, + QuorumAuthServer authServer, + QuorumAuthLearner authLearner, + int socketTimeout, + boolean listenOnAllIPs, + int quorumCnxnThreadsSize, + boolean quorumSaslAuthEnabled) { + this(mySid, view, authServer, authLearner, socketTimeout, listenOnAllIPs, + quorumCnxnThreadsSize, quorumSaslAuthEnabled, new ConcurrentHashMap()); + } + + // visible for testing + public QuorumCnxManager(final long mySid, + Map view, + QuorumAuthServer authServer, + QuorumAuthLearner authLearner, + int socketTimeout, + boolean listenOnAllIPs, + int quorumCnxnThreadsSize, + boolean quorumSaslAuthEnabled, + ConcurrentHashMap senderWorkerMap) { + this.senderWorkerMap = senderWorkerMap; + + this.recvQueue = new ArrayBlockingQueue(RECV_CAPACITY); + this.queueSendMap = new ConcurrentHashMap>(); + this.lastMessageSent = new ConcurrentHashMap(); + String cnxToValue = System.getProperty("zookeeper.cnxTimeout"); + if(cnxToValue != null){ + this.cnxTO = Integer.parseInt(cnxToValue); + } + + this.mySid = mySid; + this.socketTimeout = socketTimeout; + this.view = view; + this.listenOnAllIPs = listenOnAllIPs; + + initializeAuth(mySid, authServer, authLearner, quorumCnxnThreadsSize, + quorumSaslAuthEnabled); + + // Starts listener thread that waits for connection requests + listener = new Listener(); + } + + private void initializeAuth(final long mySid, + final QuorumAuthServer authServer, + final QuorumAuthLearner authLearner, + final int quorumCnxnThreadsSize, + final boolean quorumSaslAuthEnabled) { + this.authServer = authServer; + this.authLearner = authLearner; + this.quorumSaslAuthEnabled = quorumSaslAuthEnabled; + if (!this.quorumSaslAuthEnabled) { + LOG.debug("Not initializing connection executor as quorum sasl auth is disabled"); + return; + } + + // init connection executors + final AtomicInteger threadIndex = new AtomicInteger(1); + SecurityManager s = System.getSecurityManager(); + final ThreadGroup group = (s != null) ? s.getThreadGroup() + : Thread.currentThread().getThreadGroup(); + ThreadFactory daemonThFactory = new ThreadFactory() { + + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(group, r, "QuorumConnectionThread-" + + "[myid=" + mySid + "]-" + + threadIndex.getAndIncrement()); + return t; + } + }; + 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) throws Exception { + if (LOG.isDebugEnabled()) { + LOG.debug("Opening channel to server " + sid); + } + Socket sock = new Socket(); + setSockOpts(sock); + sock.connect(QuorumPeer.viewToVotingView(view).get(sid).electionAddr, + cnxTO); + initiateConnection(sock, sid); + } + + /** + * 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 Socket sock, final Long sid) { + try { + startConnection(sock, sid); + } catch (IOException e) { + LOG.error("Exception while connecting, id: {}, addr: {}, closing learner connection", + new Object[] { sid, sock.getRemoteSocketAddress() }, e); + closeSocket(sock); + return; + } + } + + /** + * Server will initiate the connection request to its peer server + * asynchronously via separate connection thread. + */ + public void initiateConnectionAsync(final Socket sock, 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); + closeSocket(sock); + return; + } + try { + connectionExecutor.execute( + new QuorumConnectionReqThread(sock, 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); + closeSocket(sock); + } + } + + /** + * Thread to send connection request to peer server. + */ + private class QuorumConnectionReqThread extends ZooKeeperThread { + final Socket sock; + final Long sid; + QuorumConnectionReqThread(final Socket sock, final Long sid) { + super("QuorumConnectionReqThread-" + sid); + this.sock = sock; + this.sid = sid; + } + + @Override + public void run() { + try{ + initiateConnection(sock, sid); + } finally { + inprogressConnections.remove(sid); + } + } + } + + private boolean startConnection(Socket sock, Long sid) + throws IOException { + DataOutputStream dout = null; + DataInputStream din = null; + try { + // Sending id and challenge + dout = new DataOutputStream(sock.getOutputStream()); + dout.writeLong(this.mySid); + 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 + authLearner.authenticate(sock, view.get(sid).hostname); + + // If lost the challenge, then drop the new connection + if (sid > this.mySid) { + LOG.info("Have smaller server identifier, so dropping the " + + "connection: (" + sid + ", " + this.mySid + ")"); + closeSocket(sock); + // Otherwise proceed with the connection + } else { + 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 ArrayBlockingQueue(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 { + din = new DataInputStream( + new BufferedInputStream(sock.getInputStream())); + + handleConnection(sock, din); + } catch (IOException e) { + LOG.error("Exception handling connection, addr: {}, closing server connection", + sock.getRemoteSocketAddress()); + closeSocket(sock); + } + } + + /** + * Server receives a connection request and handles it asynchronously via + * separate thread. + */ + public void receiveConnectionAsync(final Socket sock) { + try { + connectionExecutor.execute( + new QuorumConnectionReceiverThread(sock)); + connectionThreadCnt.incrementAndGet(); + } catch (Throwable e) { + LOG.error("Exception handling connection, addr: {}, closing server connection", + sock.getRemoteSocketAddress()); + 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); + } + } + + private void handleConnection(Socket sock, DataInputStream din) + throws IOException { + Long sid = null; + try { + // Read server id + sid = din.readLong(); + if (sid < 0) { // this is not a server id but a protocol version (see ZOOKEEPER-1633) + sid = din.readLong(); + + // next comes the #bytes in the remainder of the message + // note that 0 bytes is fine (old servers) + int num_remaining_bytes = din.readInt(); + if (num_remaining_bytes < 0 || num_remaining_bytes > maxBuffer) { + LOG.error("Unreasonable buffer length: {}", num_remaining_bytes); + closeSocket(sock); + return; + } + byte[] b = new byte[num_remaining_bytes]; + + // remove the remainder of the message from din + int num_read = din.read(b); + if (num_read != num_remaining_bytes) { + LOG.error("Read only " + num_read + " bytes out of " + num_remaining_bytes + " sent by server " + sid); + } + } + 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) { + closeSocket(sock); + LOG.warn("Exception reading or writing challenge: " + e.toString()); + return; + } + + // do authenticating learner + LOG.debug("Authenticating learner server.id: {}", sid); + authServer.authenticate(sock, din); + + //If wins the challenge, then close the new connection. + if (sid < this.mySid) { + /* + * 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 + */ + LOG.debug("Create new connection to server: " + sid); + closeSocket(sock); + connectOne(sid); + + // Otherwise start worker threads to receive data. + } else { + 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 ArrayBlockingQueue(SEND_CAPACITY)); + + sw.start(); + rw.start(); + + return; + } + } + + /** + * Processes invoke this message to queue a message to send. Currently, + * only leader election uses it. + */ + public void toSend(Long sid, ByteBuffer b) { + /* + * If sending message to myself, then simply enqueue it (loopback). + */ + if (this.mySid == sid) { + b.position(0); + addToRecvQueue(new Message(b.duplicate(), sid)); + /* + * Otherwise send to the corresponding thread to send. + */ + } else { + /* + * Start a new connection if doesn't have one already. + */ + ArrayBlockingQueue bq = new ArrayBlockingQueue(SEND_CAPACITY); + ArrayBlockingQueue bqExisting = queueSendMap.putIfAbsent(sid, bq); + if (bqExisting != null) { + addToSendQueue(bqExisting, b); + } else { + addToSendQueue(bq, b); + } + connectOne(sid); + + } + } + + /** + * Try to establish a connection to server with id sid. + * + * @param sid server id + */ + synchronized public void connectOne(long sid){ + if (!connectedToPeer(sid)){ + InetSocketAddress electionAddr; + if (view.containsKey(sid)) { + electionAddr = view.get(sid).electionAddr; + } else { + LOG.warn("Invalid server id: " + sid); + return; + } + try { + + LOG.debug("Opening channel to server " + sid); + Socket sock = new Socket(); + setSockOpts(sock); + sock.connect(view.get(sid).electionAddr, cnxTO); + LOG.debug("Connected to server " + sid); + + // Sends 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) { + initiateConnectionAsync(sock, sid); + } else { + initiateConnection(sock, sid); + } + } catch (UnresolvedAddressException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, also UAE cannot be wrapped cleanly + // so we log the exception in order to capture this critical + // detail. + LOG.warn("Cannot open channel to " + sid + + " at election address " + electionAddr, e); + // Resolve hostname for this server in case the + // underlying ip address has changed. + if (view.containsKey(sid)) { + view.get(sid).recreateSocketAddresses(); + } + throw e; + } catch (IOException e) { + LOG.warn("Cannot open channel to " + sid + + " at election address " + electionAddr, + e); + // We can't really tell if the server is actually down or it failed + // to connect to the server because the underlying IP address + // changed. Resolve the hostname again just in case. + if (view.containsKey(sid)) { + view.get(sid).recreateSocketAddresses(); + } + } + } else { + LOG.debug("There is a connection already for server " + 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 (ArrayBlockingQueue queue : queueSendMap.values()) { + LOG.debug("Queue size: " + queue.size()); + if (queue.size() == 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(); + + 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("Halting sender: " + 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(socketTimeout); + } + + /** + * Helper method to close a socket. + * + * @param sock + * Reference to socket + */ + private void closeSocket(Socket sock) { + 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 port + */ + public class Listener extends ZooKeeperThread { + + volatile ServerSocket ss = null; + + public Listener() { + // During startup of thread, thread name will be overridden to + // specific election address + super("ListenerThread"); + } + + /** + * Sleeps on accept(). + */ + @Override + public void run() { + int numRetries = 0; + InetSocketAddress addr; + while((!shutdown) && (numRetries < 3)){ + try { + ss = new ServerSocket(); + ss.setReuseAddress(true); + if (listenOnAllIPs) { + int port = view.get(QuorumCnxManager.this.mySid) + .electionAddr.getPort(); + addr = new InetSocketAddress(port); + } else { + addr = view.get(QuorumCnxManager.this.mySid) + .electionAddr; + } + LOG.info("My election bind port: " + addr.toString()); + setName(view.get(QuorumCnxManager.this.mySid) + .electionAddr.toString()); + ss.bind(addr); + while (!shutdown) { + Socket client = ss.accept(); + setSockOpts(client); + LOG.info("Received connection request " + + 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); + } else { + receiveConnection(client); + } + + numRetries = 0; + } + } catch (IOException e) { + LOG.error("Exception while listening", e); + numRetries++; + try { + ss.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); + } + } + } + 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: " + + view.get(QuorumCnxManager.this.mySid).electionAddr); + } + } + + /** + * Halts this listener thread. + */ + void halt(){ + try{ + LOG.debug("Trying to close listener: " + ss); + if(ss != null) { + LOG.debug("Closing listener: " + + QuorumCnxManager.this.mySid); + ss.close(); + } + } catch (IOException e){ + LOG.warn("Exception when shutting down listener: " + e); + } + } + } + + /** + * 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; + + /** + * 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() { + if (LOG.isDebugEnabled()) { + LOG.debug("Calling finish for " + sid); + } + + if(!running){ + /* + * Avoids running finish() twice. + */ + return running; + } + + running = false; + closeSocket(sock); + // channel = null; + + this.interrupt(); + if (recvWorker != null) { + recvWorker.finish(); + } + + if (LOG.isDebugEnabled()) { + 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; + } + dout.writeInt(b.capacity()); + dout.write(b.array()); + 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. + */ + ArrayBlockingQueue 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(); + } + + try { + while (running && !shutdown && sock != null) { + + ByteBuffer b = null; + try { + ArrayBlockingQueue bq = queueSendMap + .get(sid); + if (bq != null) { + b = pollSendQueue(bq, 1000, TimeUnit.MILLISECONDS); + } else { + LOG.error("No queue of incoming messages for " + + "server " + sid); + break; + } + + if(b != null){ + lastMessageSent.put(sid, b); + 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 " + sid + + " my id = " + QuorumCnxManager.this.mySid + + " error = " + e); + } + this.finish(); + LOG.warn("Send worker leaving thread"); + } + } + + /** + * 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() { + if(!running){ + /* + * Avoids running finish() twice. + */ + return running; + } + running = false; + + this.interrupt(); + threadCnt.decrementAndGet(); + return running; + } + + @Override + public void run() { + threadCnt.incrementAndGet(); + try { + while (running && !shutdown && sock != null) { + /** + * Reads the first int to determine the length of the + * message + */ + 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 + */ + byte[] msgArray = new byte[length]; + din.readFully(msgArray, 0, length); + ByteBuffer message = ByteBuffer.wrap(msgArray); + addToRecvQueue(new Message(message.duplicate(), sid)); + } + } catch (Exception e) { + LOG.warn("Connection broken for id " + sid + ", my id = " + + QuorumCnxManager.this.mySid + ", error = " , e); + } finally { + LOG.warn("Interrupting SendWorker"); + sw.finish(); + if (sock != null) { + closeSocket(sock); + } + } + } + } + + /** + * Inserts an element in the specified queue. If the Queue is full, this + * method removes an element from the head of the Queue and then inserts + * the element at the tail. It can happen that the an element is removed + * by another thread in {@link SendWorker#processMessage() processMessage} + * method before this method attempts to remove an element from the queue. + * This will cause {@link ArrayBlockingQueue#remove() remove} to throw an + * exception, which is safe to ignore. + * + * Unlike {@link #addToRecvQueue(Message) addToRecvQueue} this method does + * not need to be synchronized since there is only one thread that inserts + * an element in the queue and another thread that reads from the queue. + * + * @param queue + * Reference to the Queue + * @param buffer + * Reference to the buffer to be inserted in the queue + */ + private void addToSendQueue(ArrayBlockingQueue queue, + ByteBuffer buffer) { + if (queue.remainingCapacity() == 0) { + try { + queue.remove(); + } catch (NoSuchElementException ne) { + // element could be removed by poll() + LOG.debug("Trying to remove from an empty " + + "Queue. Ignoring exception " + ne); + } + } + try { + queue.add(buffer); + } catch (IllegalStateException ie) { + // This should never happen + LOG.error("Unable to insert an element in the queue " + ie); + } + } + + /** + * Returns true if queue is empty. + * @param queue + * Reference to the queue + * @return + * true if the specified queue is empty + */ + private boolean isSendQueueEmpty(ArrayBlockingQueue 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 ArrayBlockingQueue#poll(long, java.util.concurrent.TimeUnit)} + */ + private ByteBuffer pollSendQueue(ArrayBlockingQueue queue, + long timeout, 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. + * + * This method is synchronized to achieve fairness between two threads that + * are trying to insert an element in the queue. Each thread checks if the + * queue is full, then removes the element at the head of the queue, and + * then inserts an element at the tail. This three-step process is done to + * prevent a thread from blocking while inserting an element in the queue. + * If we do not synchronize the call to this method, then a thread can grab + * a slot in the queue created by the second thread. This can cause the call + * to insert by the second thread to fail. + * Note that synchronizing this method does not block another thread + * from polling the queue since that synchronization is provided by the + * queue itself. + * + * @param msg + * Reference to the message to be inserted in the queue + */ + public void addToRecvQueue(Message msg) { + synchronized(recvQLock) { + if (recvQueue.remainingCapacity() == 0) { + try { + recvQueue.remove(); + } catch (NoSuchElementException ne) { + // element could be removed by poll() + LOG.debug("Trying to remove from an empty " + + "recvQueue. Ignoring exception " + ne); + } + } + try { + recvQueue.add(msg); + } catch (IllegalStateException ie) { + // This should never happen + LOG.error("Unable to insert element in the recvQueue " + ie); + } + } + } + + /** + * 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 ArrayBlockingQueue#poll(long, java.util.concurrent.TimeUnit)} + */ + public Message pollRecvQueue(long timeout, TimeUnit unit) + throws InterruptedException { + return recvQueue.poll(timeout, unit); + } + + public boolean connectedToPeer(long peerSid) { + return senderWorkerMap.get(peerSid) != null; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumMXBean.java new file mode 100644 index 0000000..2edce68 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumMXBean.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; + +/** + * An MBean representing a zookeeper cluster nodes (aka quorum peers) + */ +public interface QuorumMXBean { + /** + * @return the name of the quorum + */ + public String getName(); + + /** + * @return configured number of peers in the quorum + */ + public int getQuorumSize(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java new file mode 100644 index 0000000..267f89e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java @@ -0,0 +1,1544 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +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 java.util.concurrent.atomic.AtomicInteger; + +import javax.security.sasl.SaslException; + +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +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.auth.NullQuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.NullQuorumAuthServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +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); + + QuorumBean jmxQuorumBean; + LocalPeerBean jmxLocalPeerBean; + LeaderElectionBean jmxLeaderElectionBean; + QuorumCnxManager qcm; + QuorumAuthServer authServer; + QuorumAuthLearner authLearner; + // VisibleForTesting. This flag is used to know whether qLearner's and + // qServer's login context has been initialized as ApacheDS has concurrency + // issues. Refer https://issues.apache.org/jira/browse/ZOOKEEPER-2712 + private boolean authInitialized = false; + + /* 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; + + public static class QuorumServer { + private QuorumServer(long id, InetSocketAddress addr, + InetSocketAddress electionAddr) { + this.id = id; + this.addr = addr; + this.electionAddr = electionAddr; + } + + // VisibleForTesting + public QuorumServer(long id, InetSocketAddress addr) { + this.id = id; + this.addr = addr; + this.electionAddr = null; + } + + private QuorumServer(long id, InetSocketAddress addr, + InetSocketAddress electionAddr, LearnerType type) { + this.id = id; + this.addr = addr; + this.electionAddr = electionAddr; + this.type = type; + } + + public QuorumServer(long id, String hostname, + Integer port, Integer electionPort, + LearnerType type) { + this.id = id; + this.hostname=hostname; + if (port!=null){ + this.port=port; + } + if (electionPort!=null){ + this.electionPort=electionPort; + } + if (type!=null){ + this.type = type; + } + this.recreateSocketAddresses(); + } + + /** + * Performs a DNS lookup of hostname and (re)creates the this.addr and + * this.electionAddr InetSocketAddress objects as appropriate + * + * If the DNS lookup fails, this.addr and electionAddr remain + * unmodified, unless they were never set. If this.addr is null, then + * it is set with an unresolved InetSocketAddress object. this.electionAddr + * is handled similarly. + */ + public void recreateSocketAddresses() { + InetAddress address = null; + try { + // the time, in milliseconds, before {@link InetAddress#isReachable} aborts + // in {@link #getReachableAddress}. + int ipReachableTimeout = 0; + String ipReachableValue = System.getProperty("zookeeper.ipReachableTimeout"); + if (ipReachableValue != null) { + try { + ipReachableTimeout = Integer.parseInt(ipReachableValue); + } catch (NumberFormatException e) { + LOG.error("{} is not a valid number", ipReachableValue); + } + } + // zookeeper.ipReachableTimeout is not defined + if (ipReachableTimeout <= 0) { + address = InetAddress.getByName(this.hostname); + } else { + address = getReachableAddress(this.hostname, ipReachableTimeout); + } + LOG.info("Resolved hostname: {} to address: {}", this.hostname, address); + this.addr = new InetSocketAddress(address, this.port); + if (this.electionPort > 0){ + this.electionAddr = new InetSocketAddress(address, this.electionPort); + } + } catch (UnknownHostException ex) { + LOG.warn("Failed to resolve address: {}", this.hostname, ex); + // Have we succeeded in the past? + if (this.addr != null) { + // Yes, previously the lookup succeeded. Leave things as they are + return; + } + // The hostname has never resolved. Create our InetSocketAddress(es) as unresolved + this.addr = InetSocketAddress.createUnresolved(this.hostname, this.port); + if (this.electionPort > 0){ + this.electionAddr = InetSocketAddress.createUnresolved(this.hostname, + this.electionPort); + } + } + } + + /** + * Resolve the hostname to IP addresses, and find one reachable address. + * + * @param hostname the name of the host + * @param timeout the time, in milliseconds, before {@link InetAddress#isReachable} + * aborts + * @return a reachable IP address. If no such IP address can be found, + * just return the first IP address of the hostname. + * + * @exception UnknownHostException + */ + public InetAddress getReachableAddress(String hostname, int timeout) + throws UnknownHostException { + InetAddress[] addresses = InetAddress.getAllByName(hostname); + for (InetAddress a : addresses) { + try { + if (a.isReachable(timeout)) { + return a; + } + } catch (IOException e) { + LOG.warn("IP address {} is unreachable", a); + } + } + // All the IP addresses are unreachable, just return the first one. + return addresses[0]; + } + + public InetSocketAddress addr; + + public InetSocketAddress electionAddr; + + public String hostname; + + public int port=2888; + + public int electionPort=-1; + + public long id; + + public LearnerType type = LearnerType.PARTICIPANT; + } + + public enum ServerState { + LOOKING, FOLLOWING, LEADING, OBSERVING; + } + + /* + * 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). + */ + public enum LearnerType { + PARTICIPANT, 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; + + /* + * Default value of peer is participant + */ + private LearnerType learnerType = LearnerType.PARTICIPANT; + + public LearnerType getLearnerType() { + return learnerType; + } + + /** + * Sets the LearnerType both in the QuorumPeer and in the peerMap + */ + public void setLearnerType(LearnerType p) { + learnerType = p; + if (quorumPeers.containsKey(this.myid)) { + this.quorumPeers.get(myid).type = p; + } else { + LOG.error("Setting LearnerType to " + p + " but " + myid + + " not in QuorumPeers. "); + } + + } + /** + * The servers that make up the cluster + */ + protected Map quorumPeers; + public int getQuorumSize(){ + return getVotingView().size(); + } + + /** + * QuorumVerifier implementation; default (majority). + */ + + private QuorumVerifier quorumConfig; + + /** + * My id + */ + private long myid; + + + /** + * get the id of this quorum peer. + */ + public long getId() { + return myid; + } + + /** + * This is who I think the leader currently is. + */ + volatile private Vote currentVote; + + /** + * ... and its counterpart for backward compatibility + */ + volatile private Vote bcVote; + + public synchronized Vote getCurrentVote(){ + return currentVote; + } + + public synchronized void setCurrentVote(Vote v){ + currentVote = v; + } + + synchronized Vote getBCVote() { + if (bcVote == null) { + return currentVote; + } else { + return bcVote; + } + } + + synchronized void setBCVote(Vote v) { + bcVote = v; + } + + volatile boolean running = true; + + /** + * The number of milliseconds of each tick + */ + protected int tickTime; + + /** + * 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 number of ticks that the initial synchronization phase can take + */ + protected int initLimit; + + /** + * The number of ticks that can pass between sending a request and getting + * an acknowledgment + */ + protected int syncLimit; + + /** + * 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; + + /** + * 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; + + /** + * 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; + + /** + * @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"); + } + } + } + + private ServerState state = ServerState.LOOKING; + + public synchronized void setPeerState(ServerState newState){ + state=newState; + } + + public synchronized ServerState getPeerState(){ + return state; + } + + DatagramSocket udpSocket; + + private InetSocketAddress myQuorumAddr; + + public InetSocketAddress getQuorumAddress(){ + return myQuorumAddr; + } + + private int electionType; + + Election electionAlg; + + ServerCnxnFactory cnxnFactory; + private FileTxnSnapLog logFactory = null; + + private final QuorumStats quorumStats; + + public static QuorumPeer testingQuorumPeer() throws SaslException { + return new QuorumPeer(); + } + + protected QuorumPeer() throws SaslException { + super("QuorumPeer"); + quorumStats = new QuorumStats(this); + initialize(); + } + + + /** + * 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, + ServerCnxnFactory cnxnFactory) throws IOException { + this(quorumPeers, dataDir, dataLogDir, electionType, myid, tickTime, + initLimit, syncLimit, false, cnxnFactory, + new QuorumMaj(countParticipants(quorumPeers))); + } + + public QuorumPeer(Map quorumPeers, File dataDir, + File dataLogDir, int electionType, + long myid, int tickTime, int initLimit, int syncLimit, + boolean quorumListenOnAllIPs, + ServerCnxnFactory cnxnFactory, + QuorumVerifier quorumConfig) throws IOException { + this(); + this.cnxnFactory = cnxnFactory; + this.quorumPeers = quorumPeers; + this.electionType = electionType; + this.myid = myid; + this.tickTime = tickTime; + this.initLimit = initLimit; + this.syncLimit = syncLimit; + this.quorumListenOnAllIPs = quorumListenOnAllIPs; + this.logFactory = new FileTxnSnapLog(dataLogDir, dataDir); + this.zkDb = new ZKDatabase(this.logFactory); + if(quorumConfig == null) + this.quorumConfig = new QuorumMaj(countParticipants(quorumPeers)); + else this.quorumConfig = quorumConfig; + } + + 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); + authInitialized = true; + } else { + authServer = new NullQuorumAuthServer(); + authLearner = new NullQuorumAuthLearner(); + } + } + + QuorumStats quorumStats() { + return quorumStats; + } + + @Override + public synchronized void start() { + loadDataBase(); + cnxnFactory.start(); + startLeaderElection(); + super.start(); + } + + private void loadDataBase() { + File updating = new File(getTxnFactory().getSnapDir(), + UPDATING_EPOCH_FILENAME); + try { + zkDb.loadDataBase(); + + // load the epochs + long lastProcessedZxid = zkDb.getDataTree().lastProcessedZxid; + long epochOfZxid = ZxidUtils.getEpochFromZxid(lastProcessedZxid); + try { + currentEpoch = readLongFromFile(CURRENT_EPOCH_FILENAME); + if (epochOfZxid > currentEpoch && updating.exists()) { + LOG.info("{} found. The server was terminated after " + + "taking a snapshot but before updating current " + + "epoch. Setting current epoch to {}.", + UPDATING_EPOCH_FILENAME, epochOfZxid); + setCurrentEpoch(epochOfZxid); + if (!updating.delete()) { + throw new IOException("Failed to delete " + + updating.toString()); + } + } + } catch(FileNotFoundException e) { + // pick a reasonable epoch number + // this should only happen once when moving to a + // new code version + currentEpoch = epochOfZxid; + LOG.info(CURRENT_EPOCH_FILENAME + + " not found! Creating with a reasonable default of {}. This should only happen when you are upgrading your installation", + currentEpoch); + writeLongToFile(CURRENT_EPOCH_FILENAME, currentEpoch); + } + if (epochOfZxid > currentEpoch) { + throw new IOException("The current epoch, " + ZxidUtils.zxidToString(currentEpoch) + ", is older than the last zxid, " + lastProcessedZxid); + } + 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(ACCEPTED_EPOCH_FILENAME + + " not found! Creating with a reasonable default of {}. This should only happen when you are upgrading your installation", + acceptedEpoch); + writeLongToFile(ACCEPTED_EPOCH_FILENAME, acceptedEpoch); + } + 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; + + synchronized public void stopLeaderElection() { + responder.running = false; + responder.interrupt(); + } + synchronized public void startLeaderElection() { + try { + currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch()); + } catch(IOException e) { + RuntimeException re = new RuntimeException(e.getMessage()); + re.setStackTrace(e.getStackTrace()); + throw re; + } + for (QuorumServer p : getView().values()) { + if (p.id == myid) { + myQuorumAddr = p.addr; + break; + } + } + if (myQuorumAddr == null) { + throw new RuntimeException("My id " + myid + " not in the peer list"); + } + if (electionType == 0) { + try { + udpSocket = new DatagramSocket(myQuorumAddr.getPort()); + responder = new ResponderThread(); + responder.start(); + } catch (SocketException e) { + throw new RuntimeException(e); + } + } + this.electionAlg = createElectionAlgorithm(electionType); + } + + /** + * 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) + throws IOException + { + this(quorumPeers, snapDir, logDir, electionAlg, + myid,tickTime, initLimit,syncLimit, false, + ServerCnxnFactory.createFactory(new InetSocketAddress(clientPort), -1), + new QuorumMaj(countParticipants(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, + QuorumVerifier quorumConfig) + throws IOException + { + this(quorumPeers, snapDir, logDir, electionAlg, + myid,tickTime, initLimit,syncLimit, false, + ServerCnxnFactory.createFactory(new InetSocketAddress(clientPort), -1), + quorumConfig); + } + + /** + * 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(); + } + + public Follower follower; + public Leader leader; + public Observer observer; + + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + return new Follower(this, new FollowerZooKeeperServer(logFactory, + this,new ZooKeeperServer.BasicDataTreeBuilder(), this.zkDb)); + } + + protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException { + return new Leader(this, new LeaderZooKeeperServer(logFactory, + this,new ZooKeeperServer.BasicDataTreeBuilder(), this.zkDb)); + } + + protected Observer makeObserver(FileTxnSnapLog logFactory) throws IOException { + return new Observer(this, new ObserverZooKeeperServer(logFactory, + this, new ZooKeeperServer.BasicDataTreeBuilder(), this.zkDb)); + } + + protected Election createElectionAlgorithm(int electionAlgorithm){ + Election le=null; + + //TODO: use a factory rather than a switch + switch (electionAlgorithm) { + case 0: + le = new LeaderElection(this); + break; + case 1: + le = new AuthFastLeaderElection(this); + break; + case 2: + le = new AuthFastLeaderElection(this, true); + break; + case 3: + qcm = createCnxnManager(); + QuorumCnxManager.Listener listener = qcm.listener; + if(listener != null){ + listener.start(); + le = new FastLeaderElection(this, qcm); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + break; + default: + assert false; + } + return le; + } + + protected Election makeLEStrategy(){ + LOG.debug("Initializing leader election protocol..."); + if (getElectionType() == 0) { + electionAlg = new LeaderElection(this); + } + return electionAlg; + } + + synchronized protected void setLeader(Leader newLeader){ + leader=newLeader; + } + + synchronized protected void setFollower(Follower newFollower){ + follower=newFollower; + } + + synchronized protected void setObserver(Observer newObserver){ + observer=newObserver; + } + + synchronized public ZooKeeperServer getActiveServer(){ + if(leader!=null) + return leader.zk; + else if(follower!=null) + return follower.zk; + else if (observer != null) + return observer.zk; + return null; + } + + @Override + public void run() { + setName("QuorumPeer" + "[myid=" + getId() + "]" + + cnxnFactory.getLocalAddress()); + + 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 { + p = new RemotePeerBean(s); + try { + MBeanRegistry.getInstance().register(p, jmxQuorumBean); + } catch (Exception e) { + LOG.warn("Failed to register with JMX", e); + } + } + } + } catch (Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxQuorumBean = null; + } + + try { + /* + * Main loop + */ + while (running) { + switch (getPeerState()) { + case LOOKING: + LOG.info("LOOKING"); + + 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, + new ZooKeeperServer.BasicDataTreeBuilder(), + 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(); + setBCVote(null); + 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 { + setBCVote(null); + setCurrentVote(makeLEStrategy().lookForLeader()); + } catch (Exception e) { + LOG.warn("Unexpected exception", e); + setPeerState(ServerState.LOOKING); + } + } + break; + case OBSERVING: + try { + LOG.info("OBSERVING"); + setObserver(makeObserver(logFactory)); + observer.observeLeader(); + } catch (Exception e) { + LOG.warn("Unexpected exception",e ); + } finally { + observer.shutdown(); + setObserver(null); + setPeerState(ServerState.LOOKING); + } + break; + case FOLLOWING: + try { + LOG.info("FOLLOWING"); + setFollower(makeFollower(logFactory)); + follower.followLeader(); + } catch (Exception e) { + LOG.warn("Unexpected exception",e); + } finally { + follower.shutdown(); + setFollower(null); + setPeerState(ServerState.LOOKING); + } + break; + 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); + } + setPeerState(ServerState.LOOKING); + } + break; + } + } + } finally { + LOG.warn("QuorumPeer main thread exited"); + try { + MBeanRegistry.getInstance().unregisterAll(); + } catch (Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxQuorumBean = null; + jmxLocalPeerBean = null; + } + } + + public void shutdown() { + running = false; + if (leader != null) { + leader.shutdown("quorum Peer shutdown"); + } + if (follower != null) { + follower.shutdown(); + } + cnxnFactory.shutdown(); + if(udpSocket != null) { + udpSocket.close(); + } + + 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() { + return Collections.unmodifiableMap(this.quorumPeers); + } + + /** + * Observers are not contained in this view, only nodes with + * PeerType=PARTICIPANT. + */ + public Map getVotingView() { + return QuorumPeer.viewToVotingView(getView()); + } + + static Map viewToVotingView( + Map view) { + Map ret = + new HashMap(); + for (QuorumServer server : view.values()) { + if (server.type == LearnerType.PARTICIPANT) { + ret.put(server.id, server); + } + } + return ret; + } + + /** + * Returns only observers, no followers. + */ + public Map getObservingView() { + Map ret = + new HashMap(); + Map view = getView(); + for (QuorumServer server : view.values()) { + if (server.type == LearnerType.OBSERVER) { + ret.put(server.id, server); + } + } + return ret; + } + + /** + * 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.quorumPeers.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 = fh.getSocket().getRemoteSocketAddress().toString(); + if (leader.isLearnerSynced(fh)) + s += "*"; + l.add(s); + } + } + } else if (follower != null) { + l.add(follower.sock.getRemoteSocketAddress().toString()); + } + } + 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; + } + + + /** + * get the id of this quorum peer. + */ + public long getMyid() { + return myid; + } + + /** + * set the id of this quorum peer. + */ + public void setMyid(long myid) { + this.myid = myid; + } + + /** + * 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() { + ServerCnxnFactory fac = getCnxnFactory(); + if (fac == null) { + return -1; + } + return fac.getMaxClientCnxnsPerHost(); + } + + /** minimum session timeout in milliseconds */ + public int getMinSessionTimeout() { + return minSessionTimeout == -1 ? tickTime * 2 : 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 == -1 ? tickTime * 20 : maxSessionTimeout; + } + + /** minimum session timeout in milliseconds */ + public void setMaxSessionTimeout(int max) { + LOG.info("maxSessionTimeout set to " + max); + this.maxSessionTimeout = max; + } + + /** + * 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(); + } + + /** + * Return QuorumVerifier object + */ + + public QuorumVerifier getQuorumVerifier(){ + return quorumConfig; + + } + + public void setQuorumVerifier(QuorumVerifier quorumConfig){ + this.quorumConfig = quorumConfig; + } + + /** + * 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) { + this.syncLimit = syncLimit; + } + + + /** + * 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 ServerCnxnFactory getCnxnFactory() { + return cnxnFactory; + } + + public void setCnxnFactory(ServerCnxnFactory cnxnFactory) { + this.cnxnFactory = cnxnFactory; + } + + public void setQuorumPeers(Map quorumPeers) { + this.quorumPeers = quorumPeers; + } + + public int getClientPort() { + return cnxnFactory.getLocalPort(); + } + + public void setClientPortAddress(InetSocketAddress addr) { + } + + 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 void setRunning(boolean running) { + this.running = running; + } + + public boolean isRunning() { + return running; + } + + /** + * get reference to QuorumCnxManager + */ + public QuorumCnxManager getQuorumCnxManager() { + return qcm; + } + 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(); + } + } + + private long acceptedEpoch = -1; + private long currentEpoch = -1; + + public static final String CURRENT_EPOCH_FILENAME = "currentEpoch"; + + public static final String ACCEPTED_EPOCH_FILENAME = "acceptedEpoch"; + + public static final String UPDATING_EPOCH_FILENAME = "updatingEpoch"; + + /** + * 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 + */ + private void writeLongToFile(String name, long value) throws IOException { + File file = new File(logFactory.getSnapDir(), name); + AtomicFileOutputStream out = new AtomicFileOutputStream(file); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); + boolean aborted = false; + try { + bw.write(Long.toString(value)); + bw.flush(); + + out.flush(); + } catch (IOException e) { + LOG.error("Failed to write new file " + file, e); + // worst case here the tmp file/resources(fd) are not cleaned up + // and the caller will be notified (IOException) + aborted = true; + out.abort(); + throw e; + } finally { + if (!aborted) { + // if the close operation (rename) fails we'll get notified. + // worst case the tmp file may still exist + out.close(); + } + } + } + + 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 { + currentEpoch = e; + writeLongToFile(CURRENT_EPOCH_FILENAME, e); + + } + + public void setAcceptedEpoch(long e) throws IOException { + acceptedEpoch = e; + writeLongToFile(ACCEPTED_EPOCH_FILENAME, e); + } + + /** + * Updates leader election info to avoid inconsistencies when + * a new server tries to join the ensemble. + * See ZOOKEEPER-1732 for more info. + */ + protected void updateElectionVote(long newEpoch) { + Vote currentVote = getCurrentVote(); + setBCVote(currentVote); + if (currentVote != null) { + setCurrentVote(new Vote(currentVote.getId(), + currentVote.getZxid(), + currentVote.getElectionEpoch(), + newEpoch, + currentVote.getState())); + } + } + + 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!"); + } 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; + } + + // VisibleForTesting. Returns true if both the quorumlearner and + // quorumserver login has been finished. Otherwse, false. + public boolean hasAuthInitialized(){ + return authInitialized; + } + + public QuorumCnxManager createCnxnManager() { + return new QuorumCnxManager(this.getId(), + this.getView(), + this.authServer, + this.authLearner, + this.tickTime * this.syncLimit, + this.getQuorumListenOnAllIPs(), + this.quorumCnxnThreadsSize, + this.isQuorumSaslAuthEnabled()); + } + + /** + * 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; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java new file mode 100644 index 0000000..83d41b3 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java @@ -0,0 +1,483 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Map.Entry; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +import org.apache.zookeeper.server.ZooKeeperServer; +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; + +@InterfaceAudience.Public +public class QuorumPeerConfig { + private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerConfig.class); + + protected InetSocketAddress clientPortAddress; + protected String dataDir; + protected String dataLogDir; + 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 int initLimit; + protected int syncLimit; + protected int electionAlg = 3; + protected int electionPort = 2182; + protected boolean quorumListenOnAllIPs = false; + protected final HashMap servers = + new HashMap(); + protected final HashMap observers = + new HashMap(); + + protected long serverId; + protected HashMap serverWeight = new HashMap(); + protected HashMap serverGroup = new HashMap(); + protected int numGroups = 0; + protected QuorumVerifier quorumVerifier; + protected int snapRetainCount = 3; + protected int purgeInterval = 0; + protected boolean syncEnabled = true; + + 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; + + /** + * Minimum snapshot retain count. + * @see org.apache.zookeeper.server.PurgeTxnLog#purge(File, File, int) + */ + private final int MIN_SNAP_RETAIN_COUNT = 3; + + @SuppressWarnings("serial") + public static class ConfigException extends Exception { + public ConfigException(String msg) { + super(msg); + } + public ConfigException(String msg, Exception e) { + super(msg, e); + } + } + + private static String[] splitWithLeadingHostname(String s) + throws ConfigException + { + /* Does it start with an IPv6 literal? */ + if (s.startsWith("[")) { + int i = s.indexOf("]:"); + if (i < 0) { + throw new ConfigException(s + " starts with '[' but has no matching ']:'"); + } + + 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; + } else { + return s.split(":"); + } + } + + /** + * 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 { + File configFile = new File(path); + + 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(); + } + + parseProperties(cfg); + } catch (IOException e) { + throw new ConfigException("Error processing " + path, e); + } catch (IllegalArgumentException e) { + throw new ConfigException("Error processing " + path, e); + } + } + + /** + * Parse config from a Properties. + * @param zkProp Properties to parse from. + * @throws IOException + * @throws ConfigException + */ + public void parseProperties(Properties zkProp) + throws IOException, ConfigException { + int clientPort = 0; + String clientPortAddress = null; + for (Entry entry : zkProp.entrySet()) { + String key = entry.getKey().toString().trim(); + String value = entry.getValue().toString().trim(); + if (key.equals("dataDir")) { + dataDir = value; + } else if (key.equals("dataLogDir")) { + dataLogDir = value; + } else if (key.equals("clientPort")) { + clientPort = Integer.parseInt(value); + } else if (key.equals("clientPortAddress")) { + clientPortAddress = value.trim(); + } 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("electionAlg")) { + electionAlg = Integer.parseInt(value); + } else if (key.equals("quorumListenOnAllIPs")) { + quorumListenOnAllIPs = Boolean.parseBoolean(value); + } 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("autopurge.snapRetainCount")) { + snapRetainCount = Integer.parseInt(value); + } else if (key.equals("autopurge.purgeInterval")) { + purgeInterval = Integer.parseInt(value); + } else if (key.startsWith("server.")) { + int dot = key.indexOf('.'); + long sid = Long.parseLong(key.substring(dot + 1)); + String parts[] = splitWithLeadingHostname(value); + if ((parts.length != 2) && (parts.length != 3) && (parts.length !=4)) { + LOG.error(value + + " does not have the form host:port or host:port:port " + + " or host:port:port:type"); + } + LearnerType type = null; + String hostname = parts[0]; + Integer port = Integer.parseInt(parts[1]); + Integer electionPort = null; + if (parts.length > 2){ + electionPort=Integer.parseInt(parts[2]); + } + if (parts.length > 3){ + if (parts[3].toLowerCase().equals("observer")) { + type = LearnerType.OBSERVER; + } else if (parts[3].toLowerCase().equals("participant")) { + type = LearnerType.PARTICIPANT; + } else { + throw new ConfigException("Unrecognised peertype: " + value); + } + } + if (type == LearnerType.OBSERVER){ + observers.put(Long.valueOf(sid), new QuorumServer(sid, hostname, port, electionPort, type)); + } else { + servers.put(Long.valueOf(sid), new QuorumServer(sid, hostname, port, electionPort, type)); + } + } 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(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 { + System.setProperty("zookeeper." + key, value); + } + } + 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) { + throw new IllegalArgumentException("clientPort is not set"); + } + if (clientPortAddress != null) { + this.clientPortAddress = new InetSocketAddress( + InetAddress.getByName(clientPortAddress), clientPort); + } else { + this.clientPortAddress = new InetSocketAddress(clientPort); + } + + if (tickTime == 0) { + throw new IllegalArgumentException("tickTime is not set"); + } + if (minSessionTimeout > maxSessionTimeout) { + throw new IllegalArgumentException( + "minSessionTimeout must not be larger than maxSessionTimeout"); + } + if (servers.size() == 0) { + if (observers.size() > 0) { + throw new IllegalArgumentException("Observers w/o participants is an invalid configuration"); + } + // Not a quorum configuration so return immediately - not an error + // case (for b/w compatibility), server will default to standalone + // mode. + return; + } else if (servers.size() == 1) { + if (observers.size() > 0) { + throw new IllegalArgumentException("Observers w/o quorum is an invalid configuration"); + } + + // HBase currently adds a single server line to the config, for + // b/w compatibility reasons we need to keep this here. + LOG.error("Invalid configuration, only one server specified (ignoring)"); + servers.clear(); + } else if (servers.size() > 1) { + if (servers.size() == 2) { + LOG.warn("No server failure will be tolerated. " + + "You need at least 3 servers."); + } else if (servers.size() % 2 == 0) { + LOG.warn("Non-optimial configuration, consider an odd number of servers."); + } + if (initLimit == 0) { + throw new IllegalArgumentException("initLimit is not set"); + } + if (syncLimit == 0) { + throw new IllegalArgumentException("syncLimit is not set"); + } + /* + * If using FLE, then every server requires a separate election + * port. + */ + if (electionAlg != 0) { + for (QuorumServer s : servers.values()) { + if (s.electionAddr == null) + throw new IllegalArgumentException( + "Missing election port for server: " + s.id); + } + } + + /* + * Default of quorum config is majority + */ + if(serverGroup.size() > 0){ + if(servers.size() != serverGroup.size()) + throw new ConfigException("Every server must be in exactly one group"); + /* + * The deafult weight of a server is 1 + */ + for(QuorumServer s : servers.values()){ + if(!serverWeight.containsKey(s.id)) + serverWeight.put(s.id, (long) 1); + } + + /* + * Set the quorumVerifier to be QuorumHierarchical + */ + quorumVerifier = new QuorumHierarchical(numGroups, + serverWeight, serverGroup); + } else { + /* + * The default QuorumVerifier is QuorumMaj + */ + + LOG.info("Defaulting to majority quorums"); + quorumVerifier = new QuorumMaj(servers.size()); + } + + // Now add observers to servers, once the quorums have been + // figured out + servers.putAll(observers); + + File myIdFile = new File(dataDir, "myid"); + if (!myIdFile.exists()) { + throw new IllegalArgumentException(myIdFile.toString() + + " file is missing"); + } + BufferedReader br = new BufferedReader(new FileReader(myIdFile)); + String myIdString; + try { + myIdString = br.readLine(); + } finally { + br.close(); + } + try { + serverId = Long.parseLong(myIdString); + MDC.put("myid", myIdString); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("serverid " + myIdString + + " is not a number"); + } + + // Warn about inconsistent peer type + LearnerType roleByServersList = observers.containsKey(serverId) ? LearnerType.OBSERVER + : LearnerType.PARTICIPANT; + if (roleByServersList != peerType) { + LOG.warn("Peer type from servers list (" + roleByServersList + + ") doesn't match peerType (" + peerType + + "). Defaulting to servers list."); + + peerType = roleByServersList; + } + } + } + + public InetSocketAddress getClientPortAddress() { return clientPortAddress; } + public String getDataDir() { return dataDir; } + public String getDataLogDir() { return dataLogDir; } + public int getTickTime() { return tickTime; } + public int getMaxClientCnxns() { return maxClientCnxns; } + public int getMinSessionTimeout() { return minSessionTimeout; } + public int getMaxSessionTimeout() { return maxSessionTimeout; } + + public int getInitLimit() { return initLimit; } + public int getSyncLimit() { return syncLimit; } + 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 Map getServers() { + return Collections.unmodifiableMap(servers); + } + + public long getServerId() { return serverId; } + + public boolean isDistributed() { return servers.size() > 1; } + + public LearnerType getPeerType() { + return peerType; + } + + public Boolean getQuorumListenOnAllIPs() { + return quorumListenOnAllIPs; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerMain.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerMain.java new file mode 100644 index 0000000..156c439 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerMain.java @@ -0,0 +1,182 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 javax.management.JMException; +import javax.security.sasl.SaslException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.jmx.ManagedUtil; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.DatadirCleanupManager; +import org.apache.zookeeper.server.ZooKeeperServerMain; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * + *

    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 { + private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerMain.class); + + private static final String USAGE = "Usage: QuorumPeerMain configfile"; + + 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) { + QuorumPeerMain main = new QuorumPeerMain(); + try { + main.initializeAndRun(args); + } catch (IllegalArgumentException e) { + LOG.error("Invalid arguments, exiting abnormally", e); + LOG.info(USAGE); + System.err.println(USAGE); + System.exit(2); + } catch (ConfigException e) { + LOG.error("Invalid config, exiting abnormally", e); + System.err.println("Invalid config, exiting abnormally"); + System.exit(2); + } catch (Exception e) { + LOG.error("Unexpected exception, exiting abnormally", e); + System.exit(1); + } + LOG.info("Exiting normally"); + System.exit(0); + } + + protected void initializeAndRun(String[] args) + throws ConfigException, IOException + { + QuorumPeerConfig config = new QuorumPeerConfig(); + if (args.length == 1) { + config.parse(args[0]); + } + + // Start and schedule the the purge task + DatadirCleanupManager purgeMgr = new DatadirCleanupManager(config + .getDataDir(), config.getDataLogDir(), config + .getSnapRetainCount(), config.getPurgeInterval()); + purgeMgr.start(); + + if (args.length == 1 && config.servers.size() > 0) { + 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); + } + } + + public void runFromConfig(QuorumPeerConfig config) throws IOException { + try { + ManagedUtil.registerLog4jMBeans(); + } catch (JMException e) { + LOG.warn("Unable to register log4j JMX control", e); + } + + LOG.info("Starting quorum peer"); + try { + ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); + cnxnFactory.configure(config.getClientPortAddress(), + config.getMaxClientCnxns()); + + quorumPeer = getQuorumPeer(); + + quorumPeer.setQuorumPeers(config.getServers()); + quorumPeer.setTxnFactory(new FileTxnSnapLog( + new File(config.getDataLogDir()), + new File(config.getDataDir()))); + quorumPeer.setElectionType(config.getElectionAlg()); + quorumPeer.setMyid(config.getServerId()); + quorumPeer.setTickTime(config.getTickTime()); + quorumPeer.setInitLimit(config.getInitLimit()); + quorumPeer.setSyncLimit(config.getSyncLimit()); + quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs()); + quorumPeer.setCnxnFactory(cnxnFactory); + quorumPeer.setQuorumVerifier(config.getQuorumVerifier()); + quorumPeer.setClientPortAddress(config.getClientPortAddress()); + quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); + quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); + quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); + quorumPeer.setLearnerType(config.getPeerType()); + quorumPeer.setSyncEnabled(config.getSyncEnabled()); + + // 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(); + + quorumPeer.start(); + quorumPeer.join(); + } catch (InterruptedException e) { + // warn, but generally this is ok + LOG.warn("Quorum Peer interrupted", e); + } + } + + // @VisibleForTesting + protected QuorumPeer getQuorumPeer() throws SaslException { + return new QuorumPeer(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/QuorumStats.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumStats.java new file mode 100644 index 0000000..b6c62e4 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumStats.java @@ -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. + */ + +package org.apache.zookeeper.server.quorum; + +public class QuorumStats { + private final Provider provider; + + public interface Provider { + static public final String UNKNOWN_STATE = "unknown"; + static public final String LOOKING_STATE = "leaderelection"; + static public final String LEADING_STATE = "leading"; + static public final String FOLLOWING_STATE = "following"; + static public final String OBSERVING_STATE = "observing"; + public String[] getQuorumPeers(); + public 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/src/java/main/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.java new file mode 100644 index 0000000..93088dd --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.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.quorum; + +import java.io.PrintWriter; + +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 { + protected final QuorumPeer self; + + protected QuorumZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, + int minSessionTimeout, int maxSessionTimeout, + DataTreeBuilder treeBuilder, ZKDatabase zkDb, QuorumPeer self) + { + super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, + treeBuilder, zkDb); + this.self = self; + } + + @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.quorumPeers.get(self.getId()).electionAddr + .getPort()); + pwriter.print("quorumPort="); + pwriter.println(self.quorumPeers.get(self.getId()).addr.getPort()); + pwriter.print("peerType="); + pwriter.println(self.getLearnerType().ordinal()); + } + + @Override + protected void setState(State state) { + this.state = state; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyBean.java b/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyBean.java new file mode 100644 index 0000000..a2b6fe3 --- /dev/null +++ b/src/java/main/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/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.java new file mode 100644 index 0000000..dd4ad45 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.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 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.apache.zookeeper.server.quorum.Leader.XidRolloverException; +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 LinkedBlockingQueue queuedRequests = new LinkedBlockingQueue(); + + private boolean finished = false; + + private RequestProcessor nextProcessor; + + private 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.delete: + case OpCode.setData: + 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 (RequestProcessorException e) { + if (e.getCause() instanceof XidRolloverException) { + LOG.info(e.getCause().getMessage()); + } + handleException(this.getName(), e); + } 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/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.java b/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.java new file mode 100644 index 0000000..c9950cc --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.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.quorum; + +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 QuorumZooKeeperServer { + + private volatile boolean shutdown = false; + ReadOnlyZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, + DataTreeBuilder treeBuilder, ZKDatabase zkDb) { + super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, + treeBuilder, zkDb, 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.cnxnFactory.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 setState(State state) { + this.state = state; + } + + @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.cnxnFactory.setZooKeeperServer(null); + // clear all the connections + self.cnxnFactory.closeAll(); + + // shutdown the server itself + super.shutdown(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerBean.java b/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerBean.java new file mode 100644 index 0000000..5841f6c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerBean.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.quorum; + +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.quorum.QuorumPeer; + +/** + * 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; + + public RemotePeerBean(QuorumPeer.QuorumServer peer){ + this.peer=peer; + } + public String getName() { + return "replica."+peer.id; + } + public boolean isHidden() { + return false; + } + + public String getQuorumAddress() { + return peer.addr.getHostName()+":"+peer.addr.getPort(); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerMXBean.java new file mode 100644 index 0000000..1d2ec6b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/RemotePeerMXBean.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.server.quorum; + +/** + * A proxy for a remote quorum peer. + */ +public interface RemotePeerMXBean { + /** + * @return name of the peer + */ + public String getName(); + /** + * @return IP address of the quorum peer + */ + public String getQuorumAddress(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.java b/src/java/main/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.java new file mode 100644 index 0000000..7f81a5a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.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.quorum; + +import java.io.Flushable; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; + +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.hdr.getZxid(), null, + null); + try { + 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/src/java/main/org/apache/zookeeper/server/quorum/ServerBean.java b/src/java/main/org/apache/zookeeper/server/quorum/ServerBean.java new file mode 100644 index 0000000..281c05b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ServerBean.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 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/src/java/main/org/apache/zookeeper/server/quorum/ServerMXBean.java b/src/java/main/org/apache/zookeeper/server/quorum/ServerMXBean.java new file mode 100644 index 0000000..df52aea --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/ServerMXBean.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.server.quorum; + +/** + * A quorum server MBean. + */ +public interface ServerMXBean { + /** + * @return name of the server MBean + */ + public String getName(); + /** + * @return the start time the server + */ + public String getStartTime(); +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/StateSummary.java b/src/java/main/org/apache/zookeeper/server/quorum/StateSummary.java new file mode 100644 index 0000000..aaaa4f8 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/StateSummary.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.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/src/java/main/org/apache/zookeeper/server/quorum/Vote.java b/src/java/main/org/apache/zookeeper/server/quorum/Vote.java new file mode 100644 index 0000000..aca9a9a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/Vote.java @@ -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. + */ + +package org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class Vote { + private static final Logger LOG = LoggerFactory.getLogger(Vote.class); + + 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; + this.zxid = 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; + } + + final private int version; + + final private long id; + + final private long zxid; + + final private long electionEpoch; + + final private 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; + } + + final private ServerState state; + + @Override + public boolean equals(Object o) { + if (!(o instanceof Vote)) { + return false; + } + Vote other = (Vote) o; + + + /* + * There are two things going on in the logic below. + * First, we compare votes of servers out of election + * using only id and peer epoch. Second, if one version + * is 0x0 and the other isn't, then we only use the + * leader id. This case is here to enable rolling upgrades. + * + * {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1805} + */ + if ((state == ServerState.LOOKING) || + (other.state == ServerState.LOOKING)) { + return (id == other.id + && zxid == other.zxid + && electionEpoch == other.electionEpoch + && peerEpoch == other.peerEpoch); + } else { + 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 String.format("(%d, %s, %s)", + id, + Long.toHexString(zxid), + Long.toHexString(peerEpoch)); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.java new file mode 100644 index 0000000..0af891c --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.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.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/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.java new file mode 100644 index 0000000..b26a54a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.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.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) { + return; // simply return don't require auth + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuth.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuth.java new file mode 100644 index 0000000..8bfa394 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuth.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.quorum.auth; + +import java.io.DataInputStream; +import java.io.IOException; +import org.apache.jute.BinaryInputArchive; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.quorum.QuorumAuthPacket; + +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/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.java new file mode 100644 index 0000000..af71257 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.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.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 + */ + public void authenticate(Socket sock, String hostname) throws IOException; +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuthServer.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/QuorumAuthServer.java new file mode 100644 index 0000000..e9de8f0 --- /dev/null +++ b/src/java/main/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 + */ + public void authenticate(Socket sock, DataInputStream din) + throws IOException; +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java new file mode 100644 index 0000000..8c76d3e --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java @@ -0,0 +1,225 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.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("SASL-authentication failed because" + + " the specified JAAS configuration " + + "section '" + loginContext + + "' could not be found."); + } + this.learnerLogin = new Login(loginContext, + new SaslClientCallbackHandler(null, "QuorumLearner")); + 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" + + ". Server addr: " + + sock.getRemoteSocketAddress()); + } + 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); + } + } + } + return; + } + + 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); + } + } + } 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/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java new file mode 100644 index 0000000..1aa2b32 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java @@ -0,0 +1,180 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.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 final static 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("SASL-authentication failed" + + " because the specified JAAS configuration " + + "section '" + loginContext + "' could not be found."); + } + SaslQuorumServerCallbackHandler saslServerCallbackHandler = new SaslQuorumServerCallbackHandler( + Configuration.getConfiguration(), loginContext, authzHosts); + serverLogin = new Login(loginContext, saslServerCallbackHandler); + 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 " + sock.getRemoteSocketAddress() + + " 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: {}, {}: {}", + new Object[] { sock.getRemoteSocketAddress(), + QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, + quorumRequireSasl }); + return; // let it through, we don't require auth + } + } finally { + if (ss != null) { + try { + ss.dispose(); + } catch (SaslException e) { + LOG.error("SaslServer dispose() failed", e); + } + } + } + return; + } + + 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/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java new file mode 100644 index 0000000..3e71bb1 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.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.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/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java new file mode 100644 index 0000000..9993f91 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java @@ -0,0 +1,275 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Properties; +import java.util.Map.Entry; +import java.util.Set; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + + +/** + * 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); + + HashMap serverWeight; + HashMap serverGroup; + HashMap groupWeight; + + int numGroups; + + /** + * This contructor 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 { + this.serverWeight = new HashMap(); + this.serverGroup = new HashMap(); + this.groupWeight = new HashMap(); + this.numGroups = 0; + + 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 { + this.serverWeight = new HashMap(); + this.serverGroup = new HashMap(); + this.groupWeight = new HashMap(); + this.numGroups = 0; + + parse(qp); + + LOG.info(serverWeight.size() + ", " + serverGroup.size() + ", " + groupWeight.size()); + } + + /** + * This contructor takes the two hash maps needed to enable + * validating quorums. We use it with QuorumPeerConfig. That is, + * we declare weights and groups in the server configuration + * file along with the other parameters. + * @param numGroups + * @param serverWeight + * @param serverGroup + */ + public QuorumHierarchical(int numGroups, + HashMap serverWeight, + HashMap serverGroup) + { + this.serverWeight = serverWeight; + this.serverGroup = serverGroup; + this.groupWeight = new HashMap(); + + this.numGroups = numGroups; + computeGroupWeight(); + } + + + /** + * Returns the weight of a server. + * + * @param id + */ + public long getWeight(long id){ + return serverWeight.get(id); + } + + /** + * Reads a configration 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. + */ + private void parse(Properties quorumProp){ + for (Entry entry : quorumProp.entrySet()) { + String key = entry.getKey().toString(); + String value = entry.getValue().toString(); + 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); + 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)); + } + } + + computeGroupWeight(); + } + + /** + * 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 + */ + if(set.size() == 0) return false; + else LOG.debug("Set size: " + set.size()); + + for(long sid : set){ + Long gid = serverGroup.get(sid); + 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; + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.java b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.java new file mode 100644 index 0000000..8f9b573 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.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.quorum.flexible; + +import java.util.Set; + +//import org.apache.zookeeper.server.quorum.QuorumCnxManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements a validator for majority quorums. The + * implementation is straightforward. + * + */ +public class QuorumMaj implements QuorumVerifier { + private static final Logger LOG = LoggerFactory.getLogger(QuorumMaj.class); + + int half; + + /** + * Defines a majority to avoid computing it every time. + * + * @param n number of servers + */ + public QuorumMaj(int n){ + this.half = n/2; + } + + /** + * Returns weight of 1 by default. + * + * @param id + */ + public long getWeight(long id){ + return (long) 1; + } + + /** + * Verifies if a set is a majority. + */ + public boolean containsQuorum(Set set){ + return (set.size() > half); + } + +} diff --git a/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.java b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.java new file mode 100644 index 0000000..6649129 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.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.server.quorum.flexible; + +import java.util.Set; + +/** + * All quorum validators have to implement a method called + * containsQuorum, which verifies if a Set of server + * identifiers constitutes a quorum. + * + */ + +public interface QuorumVerifier { + long getWeight(long id); + boolean containsQuorum(Set set); +} diff --git a/src/java/main/org/apache/zookeeper/server/upgrade/DataNodeV1.java b/src/java/main/org/apache/zookeeper/server/upgrade/DataNodeV1.java new file mode 100644 index 0000000..8c4fda0 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/upgrade/DataNodeV1.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.upgrade; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +import org.apache.jute.Index; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.data.StatPersistedV1; + +/** + * 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. + * + */ +public class DataNodeV1 implements Record { + DataNodeV1() { + // default rather than public constructor + } + + DataNodeV1(DataNodeV1 parent, byte data[], List acl, StatPersistedV1 stat) { + this.parent = parent; + this.data = data; + this.acl = acl; + this.stat = stat; + this.children = new HashSet(); + } + + + /** + * convenience method for creating DataNode + * fully + * @param children + */ + public void setChildren(HashSet children) { + this.children = children; + } + + /** + * convenience methods to get the children + * @return the children of this datanode + */ + public HashSet getChildren() { + return this.children; + } + + DataNodeV1 parent; + + byte data[]; + + public List acl; + + public StatPersistedV1 stat; + + HashSet children = new HashSet(); + + public void copyStat(Stat to) { + to.setAversion(stat.getAversion()); + to.setCtime(stat.getCtime()); + to.setCversion(stat.getCversion()); + to.setCzxid(stat.getCzxid()); + to.setMtime(stat.getMtime()); + to.setMzxid(stat.getMzxid()); + to.setVersion(stat.getVersion()); + to.setEphemeralOwner(stat.getEphemeralOwner()); + to.setDataLength(data.length); + to.setNumChildren(children.size()); + } + + public void deserialize(InputArchive archive, String tag) + throws IOException { + archive.startRecord("node"); + data = archive.readBuffer("data"); + Index i = archive.startVector("acl"); + if (i != null) { + acl = new ArrayList(); + while (!i.done()) { + ACL a = new ACL(); + a.deserialize(archive, "aclEntry"); + acl.add(a); + i.incr(); + } + } + archive.endVector("acl"); + stat = new StatPersistedV1(); + stat.deserialize(archive, "stat"); + archive.endRecord("node"); + } + + synchronized public void serialize(OutputArchive archive, String tag) + throws IOException { + archive.startRecord(this, "node"); + archive.writeBuffer(data, "data"); + archive.startVector(acl, "acl"); + if (acl != null) { + for (ACL a : acl) { + a.serialize(archive, "aclEntry"); + } + } + archive.endVector(acl, "acl"); + stat.serialize(archive, "stat"); + archive.endRecord(this, "node"); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/upgrade/DataTreeV1.java b/src/java/main/org/apache/zookeeper/server/upgrade/DataTreeV1.java new file mode 100644 index 0000000..678f4c6 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/upgrade/DataTreeV1.java @@ -0,0 +1,567 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.upgrade; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.Watcher.Event; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.data.StatPersistedV1; +import org.apache.zookeeper.server.WatchManager; +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; + +/** + * 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 DataTreeV1 { + private static final Logger LOG = LoggerFactory.getLogger(DataTreeV1.class); + + /** + * This hashtable provides a fast lookup to the datanodes. The tree is the + * source of truth and is where all the locking occurs + */ + private ConcurrentHashMap nodes = new ConcurrentHashMap(); + + private WatchManager dataWatches = new WatchManager(); + + private WatchManager childWatches = new WatchManager(); + + /** + * This hashtable lists the paths of the ephemeral nodes of a session. + */ + private Map> ephemerals = new ConcurrentHashMap>(); + + /** + * return the ephemerals for this tree + * @return the ephemerals for this tree + */ + public Map> getEphemeralsMap() { + return this.ephemerals; + } + + public void setEphemeralsMap(Map> ephemerals) { + this.ephemerals = ephemerals; + } + + @SuppressWarnings("unchecked") + public HashSet getEphemerals(long sessionId) { + HashSet retv = ephemerals.get(sessionId); + if (retv == null) { + return new HashSet(); + } + HashSet cloned = null; + synchronized(retv) { + cloned = (HashSet) retv.clone(); + } + return cloned; + } + + public Collection getSessions() { + return ephemerals.keySet(); + } + + public DataNodeV1 getNode(String path) { + return nodes.get(path); + } + + /** + * 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 DataNodeV1 root = new DataNodeV1(null, new byte[0], null, new StatPersistedV1()); + + public DataTreeV1() { + /* Rather than fight it, let root have an alias */ + nodes.put("", root); + nodes.put("/", root); + } + + static public void copyStatPersisted(StatPersistedV1 from, StatPersistedV1 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.setVersion(from.getVersion()); + to.setEphemeralOwner(from.getEphemeralOwner()); + } + + static public 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.setVersion(from.getVersion()); + to.setEphemeralOwner(from.getEphemeralOwner()); + to.setDataLength(from.getDataLength()); + to.setNumChildren(from.getNumChildren()); + } + + + // public void remooveInterest(String path, Watcher nw) { + // DataNode n = nodes.get(path); + // if (n == null) { + // synchronized (nonExistentWatches) { + // HashSet list = nonExistentWatches.get(path); + // if (list != null) { + // list.remove(nw); + // } + // } + // } + // synchronized (n) { + // n.dataWatchers.remove(nw); + // n.childWatchers.remove(nw); + // } + // } + + /** + * @param path + * @param data + * @param acl + * @param ephemeralOwner + * the session id that owns this node. -1 indicates this is + * not an ephemeral node. + * @param zxid + * @param time + * @return the patch of the created node + * @throws KeeperException + */ + public String createNode(String path, byte data[], List acl, + long ephemeralOwner, long zxid, long time) + throws KeeperException.NoNodeException, KeeperException.NodeExistsException { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + String childName = path.substring(lastSlash + 1); + StatPersistedV1 stat = new StatPersistedV1(); + stat.setCtime(time); + stat.setMtime(time); + stat.setCzxid(zxid); + stat.setMzxid(zxid); + stat.setVersion(0); + stat.setAversion(0); + stat.setEphemeralOwner(ephemeralOwner); + DataNodeV1 parent = nodes.get(parentName); + if (parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (parent) { + if (parent.children.contains(childName)) { + throw new KeeperException.NodeExistsException(); + } + int cver = parent.stat.getCversion(); + cver++; + parent.stat.setCversion(cver); + DataNodeV1 child = new DataNodeV1(parent, data, acl, stat); + parent.children.add(childName); + nodes.put(path, child); + if (ephemeralOwner != 0) { + HashSet list = ephemerals.get(ephemeralOwner); + if (list == null) { + list = new HashSet(); + ephemerals.put(ephemeralOwner, list); + } + synchronized(list) { + list.add(path); + } + } + } + dataWatches.triggerWatch(path, Event.EventType.NodeCreated); + childWatches.triggerWatch(parentName.equals("")?"/":parentName, Event.EventType.NodeChildrenChanged); + return path; + } + + public void deleteNode(String path) throws KeeperException.NoNodeException { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + String childName = path.substring(lastSlash + 1); + DataNodeV1 node = nodes.get(path); + if (node == null) { + throw new KeeperException.NoNodeException(); + } + nodes.remove(path); + DataNodeV1 parent = nodes.get(parentName); + if (parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (parent) { + parent.children.remove(childName); + parent.stat.setCversion(parent.stat.getCversion() + 1); + long eowner = node.stat.getEphemeralOwner(); + if (eowner != 0) { + HashSet nodes = ephemerals.get(eowner); + if (nodes != null) { + synchronized(nodes) { + nodes.remove(path); + } + } + } + node.parent = null; + } + Set processed = + dataWatches.triggerWatch(path, EventType.NodeDeleted); + childWatches.triggerWatch(path, EventType.NodeDeleted, processed); + childWatches.triggerWatch(parentName.equals("")?"/":parentName, EventType.NodeChildrenChanged); + } + + public Stat setData(String path, byte data[], int version, long zxid, + long time) throws KeeperException.NoNodeException { + Stat s = new Stat(); + DataNodeV1 n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.data = data; + n.stat.setMtime(time); + n.stat.setMzxid(zxid); + n.stat.setVersion(version); + n.copyStat(s); + } + dataWatches.triggerWatch(path, EventType.NodeDataChanged); + return s; + } + + public byte[] getData(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + DataNodeV1 n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + if (watcher != null) { + dataWatches.addWatch(path, watcher); + } + return n.data; + } + } + + public Stat statNode(String path, Watcher watcher) throws KeeperException.NoNodeException { + Stat stat = new Stat(); + DataNodeV1 n = nodes.get(path); + if (watcher != null) { + dataWatches.addWatch(path, watcher); + } + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + return stat; + } + } + + public ArrayList getChildren(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + DataNodeV1 n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + ArrayList children = new ArrayList(); + children.addAll(n.children); + if (watcher != null) { + childWatches.addWatch(path, watcher); + } + return children; + } + } + + public Stat setACL(String path, List acl, int version) throws KeeperException.NoNodeException { + Stat stat = new Stat(); + DataNodeV1 n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.stat.setAversion(version); + n.acl = acl; + n.copyStat(stat); + return stat; + } + } + + @SuppressWarnings("unchecked") + public List getACL(String path, Stat stat) throws KeeperException.NoNodeException { + DataNodeV1 n = nodes.get(path); + if (n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized (n) { + n.copyStat(stat); + return new ArrayList(n.acl); + } + } + + static public class ProcessTxnResult { + public long clientId; + + public int cxid; + + public long zxid; + + /** + * 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; + + @SuppressWarnings("unchecked") + public ProcessTxnResult processTxn(TxnHeader header, Record txn) { + ProcessTxnResult rc = new ProcessTxnResult(); + + String debug = ""; + try { + rc.clientId = header.getClientId(); + rc.cxid = header.getCxid(); + rc.zxid = header.getZxid(); + if (rc.zxid > lastProcessedZxid) { + lastProcessedZxid = rc.zxid; + } + switch (header.getType()) { + case OpCode.create: + CreateTxn createTxn = (CreateTxn) txn; + debug = "Create transaction for " + createTxn.getPath(); + createNode(createTxn.getPath(), createTxn.getData(), createTxn + .getAcl(), createTxn.getEphemeral() ? header + .getClientId() : 0, header.getZxid(), header.getTime()); + break; + case OpCode.delete: + DeleteTxn deleteTxn = (DeleteTxn) txn; + debug = "Delete transaction for " + deleteTxn.getPath(); + deleteNode(deleteTxn.getPath()); + break; + case OpCode.setData: + SetDataTxn setDataTxn = (SetDataTxn) txn; + debug = "Set data for transaction for " + setDataTxn.getPath(); + break; + case OpCode.setACL: + SetACLTxn setACLTxn = (SetACLTxn) txn; + debug = "Set ACL for transaction for " + setACLTxn.getPath(); + break; + case OpCode.closeSession: + killSession(header.getClientId()); + break; + case OpCode.error: + ErrorTxn errTxn = (ErrorTxn) txn; + break; + } + } catch (KeeperException e) { + // These are expected errors since we take a lazy snapshot + if (initialized + || (e.code() != Code.NONODE + && e.code() != Code.NODEEXISTS)) { + LOG.warn("Failed:" + debug, e); + } + } + return rc; + } + + void killSession(long session) { + // the list is already removed from the ephemerals + // so we do not have to worry about synchronyzing on + // the list. This is only called from FinalRequestProcessor + // so there is no need for synchornization. The list is not + // changed here. Only create and delete change the list which + // are again called from FinalRequestProcessor in sequence. + HashSet list = ephemerals.remove(session); + if (list != null) { + for (String path : list) { + try { + deleteNode(path); + if (LOG.isDebugEnabled()) { + LOG.debug("Deleting ephemeral node " + path + + " for session 0x" + + Long.toHexString(session)); + } + } catch (NoNodeException e) { + LOG.warn("Ignoring NoNodeException for path " + path + + " while removing ephemeral for dead session 0x" + + Long.toHexString(session)); + } + } + } + } + + /** + * 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 + * @throws InterruptedException + */ + void serializeNode(OutputArchive oa, StringBuilder path) + throws IOException, InterruptedException { + String pathString = path.toString(); + DataNodeV1 node = getNode(pathString); + if (node == null) { + return; + } + String children[] = null; + synchronized (node) { + scount++; + oa.writeString(pathString, "path"); + oa.writeRecord(node, "node"); + children = node.children.toArray(new String[node.children.size()]); + } + path.append('/'); + int off = path.length(); + if (children != null) { + 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); + } + } + } + + int scount; + + public boolean initialized = false; + + public void serialize(OutputArchive oa, String tag) throws IOException, + InterruptedException { + scount = 0; + 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 deserialize(InputArchive ia, String tag) throws IOException { + nodes.clear(); + String path = ia.readString("path"); + while (!path.equals("/")) { + DataNodeV1 node = new DataNodeV1(); + ia.readRecord(node, "node"); + nodes.put(path, node); + int lastSlash = path.lastIndexOf('/'); + if (lastSlash == -1) { + root = node; + } else { + String parentPath = path.substring(0, lastSlash); + node.parent = nodes.get(parentPath); + node.parent.children.add(path.substring(lastSlash + 1)); + long eowner = node.stat.getEphemeralOwner(); + if (eowner != 0) { + HashSet list = ephemerals.get(eowner); + if (list == null) { + list = new HashSet(); + ephemerals.put(eowner, list); + } + list.add(path); + } + } + path = ia.readString("path"); + } + nodes.put("/", root); + } + + public String dumpEphemerals() { + Set keys = ephemerals.keySet(); + StringBuilder sb = new StringBuilder("Sessions with Ephemerals (" + + keys.size() + "):\n"); + for (long k : keys) { + sb.append("0x" + Long.toHexString(k)); + sb.append(":\n"); + HashSet tmp = ephemerals.get(k); + synchronized(tmp) { + for (String path : tmp) { + sb.append("\t" + path + "\n"); + } + } + } + return sb.toString(); + } + + public void removeCnxn(Watcher watcher) { + dataWatches.removeWatcher(watcher); + childWatches.removeWatcher(watcher); + } + + public void clear() { + root = null; + nodes.clear(); + ephemerals.clear(); + // dataWatches = null; + // childWatches = null; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeMain.java b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeMain.java new file mode 100644 index 0000000..8d6c0ab --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeMain.java @@ -0,0 +1,185 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.upgrade; + +import java.io.File; +import java.io.IOException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; + +/** + * This class upgrades the older database + * to a new database for the zookeeper + * servers. + * The way to run it is + * java -class path zookeeper.jar Upgrade dataDir snapShotDir + * or using zookeeper scripts with zkServer -upgrade dataDir snapShotDir + * it creates a backup in the dataDir/.bkup and snapShotDir/.bkup which + * can be retrieved back to the snapShotDir and dataDir + */ +@InterfaceAudience.Public +public class UpgradeMain { + File snapShotDir; + File dataDir; + File bkupsnapShotDir; + File bkupdataDir; + File currentdataDir; + File currentsnapShotDir; + + private static final Logger LOG = LoggerFactory.getLogger(UpgradeMain.class); + private static final String USAGE = "Usage: UpgradeMain dataDir snapShotDir"; + private static final int LASTVERSION = 1; + private static final int CURRENTVERSION = FileTxnSnapLog.VERSION; + private static final String dirName = FileTxnSnapLog.version; + private static final String manual = "Please take manual steps to " + + "sanitize your database.\n Please read the upgrade manual"; + + /** + * upgrade class that takes the two file + * directories. + * @param dataDir the directory that contains the + * transaction logs + * @param snapShotDir the directory that contains + * the snapshots + */ + public UpgradeMain(File dataDir, File snapShotDir) { + this.snapShotDir = snapShotDir; + this.dataDir = dataDir; + this.bkupdataDir = new File(dataDir, dirName + LASTVERSION); + this.bkupsnapShotDir = new File(snapShotDir, dirName + LASTVERSION ); + this.currentsnapShotDir = new File(snapShotDir, dirName + CURRENTVERSION); + this.currentdataDir = new File(dataDir, dirName + CURRENTVERSION); + } + + /** + * create all the bkup directories and the current + * database directories + * @throws IOException + */ + private void createAllDirs() throws IOException { + String error = "backup directory " + bkupdataDir + " already exists"; + LOG.info("Creating previous version data dir " + bkupdataDir); + if (!bkupdataDir.mkdirs()) { + LOG.error(error); + LOG.error(manual); + throw new IOException(error); + } + LOG.info("Creating previous version snapshot dir " + bkupdataDir); + if (!bkupsnapShotDir.mkdirs() && !bkupsnapShotDir.exists()) { + LOG.error(error); + LOG.error(manual); + throw new IOException(error); + } + error = "current directory " + currentdataDir + " already exists"; + LOG.info("Creating current data dir " + currentdataDir); + if (!currentdataDir.mkdirs()) { + LOG.error(error); + LOG.error(manual); + throw new IOException(error); + } + LOG.info("Creating current snapshot dir " + currentdataDir); + if (!currentsnapShotDir.mkdirs() && !currentsnapShotDir.exists()) { + LOG.error(error); + LOG.error(manual); + throw new IOException(error); + } + } + + /** + * copy files from srcdir to dstdir that have the string + * filter in the srcdir filenames + * @param srcDir the source directory + * @param dstDir the destination directory + * @param filter the filter of filenames that + * need to be copied. + * @throws IOException + */ + void copyFiles(File srcDir, File dstDir, String filter) throws IOException { + File[] list = srcDir.listFiles(); + if (list != null) { + for (File file: list) { + String name = file.getName(); + if (name.startsWith(filter)) { + // we need to copy this file + File dest = new File(dstDir, name); + LOG.info("Renaming " + file + " to " + dest); + if (!file.renameTo(dest)) { + throw new IOException("Unable to rename " + + file + " to " + dest); + } + } + } + } + } + + /** + * run the upgrade + * @throws IOException + */ + public void runUpgrade() throws IOException { + if (!dataDir.exists()) { + throw new IOException(dataDir + " does not exist"); + } + if (!snapShotDir.exists()) { + throw new IOException(snapShotDir + " does not exist"); + } + // create the bkup directorya + createAllDirs(); + //copy all the files for backup + try { + copyFiles(dataDir, bkupdataDir, "log"); + copyFiles(snapShotDir, bkupsnapShotDir, "snapshot"); + } catch(IOException io) { + LOG.error("Failed in backing up."); + throw io; + } + + //evrything is backed up + // read old database and create + // an old snapshot + UpgradeSnapShotV1 upgrade = new UpgradeSnapShotV1(bkupdataDir, + bkupsnapShotDir); + LOG.info("Creating new data tree"); + DataTree dt = upgrade.getNewDataTree(); + FileTxnSnapLog filesnapLog = new FileTxnSnapLog(dataDir, + snapShotDir); + LOG.info("snapshotting the new datatree"); + filesnapLog.save(dt, upgrade.getSessionWithTimeOuts()); + //done saving. + LOG.info("Upgrade is complete"); + } + + public static void main(String[] argv) { + if (argv.length < 2) { + LOG.error(USAGE); + System.exit(-1); + } + try { + UpgradeMain upgrade = new UpgradeMain(new File(argv[0]), new File(argv[1])); + upgrade.runUpgrade(); + } catch(Throwable th) { + LOG.error("Upgrade Error: Please read the " + + "docs for manual failure recovery ", th); + } + } +} diff --git a/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShot.java b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShot.java new file mode 100644 index 0000000..8cfbd93 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShot.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.server.upgrade; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.zookeeper.server.DataTree; + +/** + * interface for snapshot conversion. + * + */ +public interface UpgradeSnapShot { + public DataTree getNewDataTree() throws IOException; + public ConcurrentHashMap getSessionWithTimeOuts(); +} diff --git a/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShotV1.java b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShotV1.java new file mode 100644 index 0000000..dcbd313 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/upgrade/UpgradeSnapShotV1.java @@ -0,0 +1,332 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.upgrade; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.data.StatPersistedV1; +import org.apache.zookeeper.server.DataNode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * This class gets the old snapshot + * and the old dataDir and creates + * an brand new snapshot that is + * then converted to the new snapshot + * for upgrading. + */ +public class UpgradeSnapShotV1 implements UpgradeSnapShot { + private static final Logger LOG = LoggerFactory.getLogger(UpgradeSnapShotV1.class); + + ConcurrentHashMap sessionsWithTimeouts = + new ConcurrentHashMap(); + File dataDir; + File snapShotDir; + DataTreeV1 oldDataTree; + + /** + * upgrade from version 1 to version 2 + * @param dataDir + * @param snapShotDir + */ + public UpgradeSnapShotV1(File dataDir, File snapShotDir) { + this.dataDir = dataDir; + this.snapShotDir = snapShotDir; + oldDataTree = new DataTreeV1(); + } + + /** + * deseriluize from an inputarchive + * @param oldTree the tree to be created + * @param ia the input archive to be read from + * @param sessions the sessions to be created + * @throws IOException + */ + private void deserializeSnapshot(DataTreeV1 oldTree, InputArchive ia, + Map sessions) throws IOException { + int count = ia.readInt("count"); + while (count > 0) { + long id = ia.readLong("id"); + int to = ia.readInt("timeout"); + sessions.put(id, to); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "loadData --- session in archive: " + id + + " with timeout: " + to); + } + count--; + } + oldTree.deserialize(ia, "tree"); + } + + /** + * play the log from this logstream into the datatree + * @param logStream + * @return + * @throws IOException + */ + public long playLog(InputArchive logStream) throws IOException { + long highestZxid = 0; + try { + while (true) { + byte[] bytes = logStream.readBuffer("txnEntry"); + if (bytes.length == 0) { + // Since we preallocate, we define EOF to be an + // empty transaction + throw new EOFException(); + } + TxnHeader hdr = new TxnHeader(); + Record txn = SerializeUtils.deserializeTxn(bytes, hdr); + if (logStream.readByte("EOR") != 'B') { + LOG.warn("Last transaction was partial."); + throw new EOFException("Last transaction was partial."); + } + if (hdr.getZxid() <= highestZxid && highestZxid != 0) { + LOG.error(highestZxid + "(higestZxid) >= " + + hdr.getZxid() + "(next log) for type " + + hdr.getType()); + } else { + highestZxid = hdr.getZxid(); + } + switch (hdr.getType()) { + case OpCode.createSession: + sessionsWithTimeouts.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 + oldDataTree.processTxn(hdr, txn); + break; + case OpCode.closeSession: + sessionsWithTimeouts.remove(hdr.getClientId()); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, + ZooTrace.SESSION_TRACE_MASK, + "playLog --- close session in log: 0x" + + Long.toHexString(hdr.getClientId())); + } + oldDataTree.processTxn(hdr, txn); + break; + default: + oldDataTree.processTxn(hdr, txn); + } + Request r = new Request(null, 0, hdr.getCxid(), hdr.getType(), + null, null); + r.txn = txn; + r.hdr = hdr; + r.zxid = hdr.getZxid(); + } + } catch (EOFException e) { + // expected in some cases - see comments in try block + } + return highestZxid; + } + + + + /** + * apply the log files to the datatree + * @param oldTree the datatreee to apply the logs to + * @param logFiles the logs to be applied + * @throws IOException + */ + private long processLogFiles(DataTreeV1 oldTree, + File[] logFiles) throws IOException { + long zxid = 0; + for (File f: logFiles) { + LOG.info("Processing log file: " + f); + InputStream logIs = + new BufferedInputStream(new FileInputStream(f)); + zxid = playLog(BinaryInputArchive.getArchive(logIs)); + logIs.close(); + } + return zxid; + } + + /** + * create the old snapshot database + * apply logs to it and create the final + * database + * @throws IOException + */ + private void loadThisSnapShot() throws IOException { + // pick the most recent snapshot + File snapshot = findMostRecentSnapshot(); + if (snapshot == null) { + throw new IOException("Invalid snapshots " + + "or not snapshots in " + snapShotDir); + } + InputStream inputstream = new BufferedInputStream( + new FileInputStream(snapshot)); + InputArchive ia = BinaryInputArchive.getArchive(inputstream); + deserializeSnapshot(oldDataTree, ia, sessionsWithTimeouts); + //ok done with the snapshot + // now apply the logs + long snapshotZxid = oldDataTree.lastProcessedZxid; + File[] files = FileTxnLog.getLogFiles( + dataDir.listFiles(), snapshotZxid); + long zxid = processLogFiles(oldDataTree, files); + //check for this zxid to be sane + if (zxid != oldDataTree.lastProcessedZxid) { + LOG.error("Zxids not equal " + " log zxid " + + zxid + " datatree processed " + oldDataTree.lastProcessedZxid); + } + } + + /** + * find the most recent snapshot + * in the snapshot directory + * @return + * @throws IOException + */ + private File findMostRecentSnapshot() throws IOException { + List files = Util.sortDataDir(snapShotDir.listFiles(), + "snapshot", false); + for (File f: files) { + try { + if (Util.isValidSnapshot(f)) + return f; + } catch(IOException e) { + LOG.info("Invalid snapshot " + f, e); + } + } + return null; + } + + /** + * convert the old stat to new stat + * @param oldStat the old stat + * @return the new stat + */ + private StatPersisted convertStat(StatPersistedV1 oldStat) { + StatPersisted stat = new StatPersisted(); + stat.setAversion(oldStat.getAversion()); + stat.setCtime(oldStat.getCtime()); + stat.setCversion(oldStat.getCversion()); + stat.setCzxid(oldStat.getCzxid()); + stat.setEphemeralOwner(oldStat.getEphemeralOwner()); + stat.setMtime(oldStat.getMtime()); + stat.setMzxid(oldStat.getMzxid()); + stat.setVersion(oldStat.getVersion()); + return stat; + } + + /** + * convert a given old datanode to new datanode + * @param dt the new datatree + * @param parent the parent of the datanode to be constructed + * @param oldDataNode the old datanode + * @return the new datanode + */ + private DataNode convertDataNode(DataTree dt, DataNode parent, + DataNodeV1 oldDataNode) { + StatPersisted stat = convertStat(oldDataNode.stat); + DataNode dataNode = new DataNode(parent, oldDataNode.data, + dt.getACL(oldDataNode), stat); + dataNode.setChildren(oldDataNode.children); + return dataNode; + } + + /** + * recurse through the old datatree and construct the + * new data tree + * @param dataTree the new datatree to be constructed + * @param path the path to start with + */ + private void recurseThroughDataTree(DataTree dataTree, String path) { + if (path == null) + return; + DataNodeV1 oldDataNode = oldDataTree.getNode(path); + HashSet children = oldDataNode.children; + DataNode parent = null; + if ("".equals(path)) { + parent = null; + } + else { + int lastSlash = path.lastIndexOf('/'); + String parentPath = path.substring(0, lastSlash); + parent = dataTree.getNode(parentPath); + } + DataNode thisDatNode = convertDataNode(dataTree, parent, + oldDataNode); + dataTree.addDataNode(path, thisDatNode); + if (children == null || children.size() == 0) { + return; + } + else { + for (String child: children) { + recurseThroughDataTree(dataTree, path + "/" +child); + } + } + } + + private DataTree convertThisSnapShot() throws IOException { + // create a datatree + DataTree dataTree = new DataTree(); + DataNodeV1 oldDataNode = oldDataTree.getNode(""); + if (oldDataNode == null) { + //should never happen + LOG.error("Upgrading from an empty snapshot."); + } + + recurseThroughDataTree(dataTree, ""); + dataTree.lastProcessedZxid = oldDataTree.lastProcessedZxid; + return dataTree; + } + + public DataTree getNewDataTree() throws IOException { + loadThisSnapShot(); + DataTree dt = convertThisSnapShot(); + return dt; + } + + public ConcurrentHashMap getSessionWithTimeOuts() { + return this.sessionsWithTimeouts; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/util/KerberosUtil.java b/src/java/main/org/apache/zookeeper/server/util/KerberosUtil.java new file mode 100644 index 0000000..509bc30 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/util/KerberosUtil.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.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class KerberosUtil { + + public static String getDefaultRealm() + throws ClassNotFoundException, NoSuchMethodException, + IllegalArgumentException, IllegalAccessException, + InvocationTargetException { + Object kerbConf; + Class classRef; + Method getInstanceMethod; + Method getDefaultRealmMethod; + if (System.getProperty("java.vendor").contains("IBM")) { + classRef = Class.forName("com.ibm.security.krb5.internal.Config"); + } else { + classRef = Class.forName("sun.security.krb5.Config"); + } + getInstanceMethod = classRef.getMethod("getInstance"); + kerbConf = getInstanceMethod.invoke(classRef); + getDefaultRealmMethod = classRef.getDeclaredMethod("getDefaultRealm"); + return (String)getDefaultRealmMethod.invoke(kerbConf, new Object[0]); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/util/OSMXBean.java b/src/java/main/org/apache/zookeeper/server/util/OSMXBean.java new file mode 100644 index 0000000..240a5be --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/util/OSMXBean.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.util; + +import java.lang.management.ManagementFactory; +import java.lang.management.OperatingSystemMXBean; +import java.lang.management.RuntimeMXBean; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.lang.reflect.Method; + +/** + * 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 +{ + 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 : true); + } + + /** + * 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/src/java/main/org/apache/zookeeper/server/util/Profiler.java b/src/java/main/org/apache/zookeeper/server/util/Profiler.java new file mode 100644 index 0000000..f81d270 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/util/Profiler.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.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Profiler { + private static final Logger LOG = LoggerFactory.getLogger(Profiler.class); + + public interface Operation { + public T execute() throws Exception; + } + + public static T profile(Operation op, long timeout, String message) + throws Exception { + long start = System.currentTimeMillis(); + T res = op.execute(); + long end = System.currentTimeMillis(); + if (end - start > timeout) { + LOG.info("Elapsed "+(end - start) + " ms: " + message); + } + return res; + } +} diff --git a/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java b/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.java new file mode 100644 index 0000000..ebb30bb --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/util/SerializeUtils.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.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +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.BinaryOutputArchive; +import org.apache.zookeeper.server.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.ZooTrace; +import org.apache.zookeeper.txn.CreateSessionTxn; +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.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.txn.MultiTxn; + +public class SerializeUtils { + private static final Logger LOG = LoggerFactory.getLogger(SerializeUtils.class); + + public static Record deserializeTxn(byte txnBytes[], TxnHeader hdr) + throws IOException { + 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: + return null; + case OpCode.create: + txn = new CreateTxn(); + break; + case OpCode.delete: + txn = new DeleteTxn(); + break; + 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 { + throw e; + } + } + } + return txn; + } + + public static void deserializeSnapshot(DataTree dt,InputArchive ia, + Map sessions) throws IOException { + int count = ia.readInt("count"); + while (count > 0) { + long id = ia.readLong("id"); + int to = ia.readInt("timeout"); + sessions.put(id, to); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "loadData --- session in archive: " + id + + " with timeout: " + to); + } + count--; + } + 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.hdr == null) return null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + try { + request.hdr.serialize(boa, "hdr"); + if (request.txn != null) { + request.txn.serialize(boa, "txn"); + } + baos.close(); + } catch (IOException e) { + LOG.error("This really should be impossible", e); + } + return baos.toByteArray(); + } +} diff --git a/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java b/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.java new file mode 100644 index 0000000..39e076a --- /dev/null +++ b/src/java/main/org/apache/zookeeper/server/util/ZxidUtils.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.util; + +public class ZxidUtils { + static public long getEpochFromZxid(long zxid) { + return zxid >> 32L; + } + static public long getCounterFromZxid(long zxid) { + return zxid & 0xffffffffL; + } + static public long makeZxid(long epoch, long counter) { + return (epoch << 32L) | (counter & 0xffffffffL); + } + static public String zxidToString(long zxid) { + return Long.toHexString(zxid); + } +} diff --git a/src/java/main/org/apache/zookeeper/util/SecurityUtils.java b/src/java/main/org/apache/zookeeper/util/SecurityUtils.java new file mode 100644 index 0000000..67484e4 --- /dev/null +++ b/src/java/main/org/apache/zookeeper/util/SecurityUtils.java @@ -0,0 +1,298 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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()); + KerberosName serviceKerberosName = new KerberosName( + servicePrincipal + "@" + serverRealm); + 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={}", + new Object[] { 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, + servicePrincipalNameAndHostname.length()); + + 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/src/java/main/org/apache/zookeeper/version/util/VerGen.java b/src/java/main/org/apache/zookeeper/version/util/VerGen.java new file mode 100644 index 0000000..7285a2b --- /dev/null +++ b/src/java/main/org/apache/zookeeper/version/util/VerGen.java @@ -0,0 +1,178 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VerGen { + private static final String PACKAGE_NAME = "org.apache.zookeeper.version"; + private static final String TYPE_NAME = "Info"; + + static void printUsage() { + System.out.print("Usage:\tjava -cp org.apache.zookeeper." + + "version.util.VerGen maj.min.micro[-qualifier] rev buildDate"); + System.exit(1); + } + + public static void generateFile(File outputDir, Version version, String rev, String buildDate) throws IOException + { + 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(1); + } + } else if (!pkgdir.isDirectory()) { + // not a directory + System.out.println(path + " is not a directory."); + System.exit(1); + } + File file = new File(pkgdir, TYPE_NAME + ".java"); + FileWriter w = null; + try { + w = new FileWriter(file); + 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 " + TYPE_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 ? null : + "\"" + version.qualifier + "\"") + + ";\n"); + if (rev.equals("-1")) { + System.out.println("Unknown REVISION number, using " + rev); + } + w.write(" int REVISION=-1; //TODO: remove as related to SVN VCS\n"); + w.write(" String REVISION_HASH=\"" + rev + "\";\n"); + w.write(" String BUILD_DATE=\"" + buildDate + + "\";\n"); + w.write("}\n"); + } finally { + if (w != null) { + try { + w.close(); + } catch (IOException e) { + System.out.println("Unable to close file writer" + + e.getMessage()); + } + } + } + } + + 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) + printUsage(); + try { + Version version = parseVersionString(args[0]); + if (version == null) { + System.err.println( + "Invalid version number format, must be \"x.y.z(-.*)?\""); + System.exit(1); + } + String rev = args[1]; + if (rev == null || rev.trim().isEmpty()) { + rev = "-1"; + } else { + rev = rev.trim(); + } + generateFile(new File("."), version, rev, args[2]); + } catch (NumberFormatException e) { + System.err.println( + "All version-related parameters must be valid integers!"); + throw e; + } catch (IOException e) { + System.out.println("Unable to generate version.Info file: " + + e.getMessage()); + System.exit(1); + } + } + +} diff --git a/src/java/main/overview.html b/src/java/main/overview.html new file mode 100644 index 0000000..db9b16f --- /dev/null +++ b/src/java/main/overview.html @@ -0,0 +1,26 @@ + + + + + + ZooKeeper + + +ZooKeeper is a service for coordinating processes of distributed applications. + + diff --git a/src/java/systest/README.txt b/src/java/systest/README.txt new file mode 100644 index 0000000..8a2985c --- /dev/null +++ b/src/java/systest/README.txt @@ -0,0 +1,62 @@ +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) transfer the fatjar from the release directory to all systems + participating in the test. fatjar is in contrib/fatjar directory. + + (developers can generate by running "ant jar compile-test" + targets in trunk, then compiling using "ant jar" in src/contrib/jarjar) + +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/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java b/src/java/systest/org/apache/zookeeper/test/system/BaseSysTest.java new file mode 100644 index 0000000..421f699 --- /dev/null +++ b/src/java/systest/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 junit.framework.TestCase; + +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.Ignore; +import org.junit.runner.JUnitCore; + +@Ignore("No tests in this class.") +public class BaseSysTest extends TestCase { + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/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; + @Override + protected void setUp() throws Exception { + if (!fakeMachines) { + String localHost = InetAddress.getLocalHost().getCanonicalHostName(); + zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {public void process(WatchedEvent e){}}); + im = new InstanceManager(zk, prefix); + } + } + @Override + protected 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]); + sbServer.append(r[1]); + } + 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[]; + HashMap 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++) { + peers.put(Long.valueOf(i), new QuorumServer( + i, "127.0.0.1", fakeBasePort + i, serverCount + fakeBasePort + i, null)); + } + 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; + + public void startServer(int index) throws IOException { + int port = fakeBasePort+10+index; + if (fakeMachines) { + qps[index] = new QuorumPeer(peers, qpsDirs[index], qpsDirs[index], port, 0, index+1, tickTime, initLimit, syncLimit); + 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) throws IOException, ClassNotFoundException { + fakeBaseClients = new Instance[count]; + for(int i = 0; i < count; i++) { + try { + fakeBaseClients[i] = clazz.getDeclaredConstructor().newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + return; + } catch (IllegalAccessException e) { + e.printStackTrace(); + return; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return; + } catch (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/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java b/src/java/systest/org/apache/zookeeper/test/system/DuplicateNameException.java new file mode 100644 index 0000000..8a96c57 --- /dev/null +++ b/src/java/systest/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/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java b/src/java/systest/org/apache/zookeeper/test/system/GenerateLoad.java new file mode 100644 index 0000000..b6ac04a --- /dev/null +++ b/src/java/systest/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.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(); + } + } + } + + public boolean isConnected() { + return connected; + } + + 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]); + quorumHostPort.append(r[1]); + } + 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(2); + } + } 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(2); + } +} diff --git a/src/java/systest/org/apache/zookeeper/test/system/Instance.java b/src/java/systest/org/apache/zookeeper/test/system/Instance.java new file mode 100644 index 0000000..9b980de --- /dev/null +++ b/src/java/systest/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/src/java/systest/org/apache/zookeeper/test/system/InstanceContainer.java b/src/java/systest/org/apache/zookeeper/test/system/InstanceContainer.java new file mode 100644 index 0000000..244e4d8 --- /dev/null +++ b/src/java/systest/org/apache/zookeeper/test/system/InstanceContainer.java @@ -0,0 +1,310 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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(2); + } + 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(4); + } + if (event.getPath() != null && event.getPath().equals(assignmentsNode)) { + // children have changed, so read in the new list + zk.getChildren(assignmentsNode, true, this, null); + } + } + + HashMap instances = new HashMap(); + 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; + } + HashMap 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.getDeclaredConstructor().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/src/java/systest/org/apache/zookeeper/test/system/InstanceManager.java b/src/java/systest/org/apache/zookeeper/test/system/InstanceManager.java new file mode 100644 index 0000000..93c51b8 --- /dev/null +++ b/src/java/systest/org/apache/zookeeper/test/system/InstanceManager.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.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.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; + +/** + * 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; + String instance; + int weight; + Assigned(String container, String instance, int weight) { + this.container = container; + this.instance = instance; + 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 HashMap> assignments = new HashMap>(); + private HashMap 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); + } + HashMap> 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; + } + private void removeAssignmentNode(String dead) throws KeeperException, InterruptedException { + String deadNode = assignmentsNode + '/' + dead; + List children = zk.getChildren(deadNode, false); + for(String c: children) { + zk.delete(deadNode + '/' + c, -1); + } + try { + zk.delete(deadNode, -1); + } catch(NoNodeException e) { /* this is ok */ } + } + + 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, name, 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 = System.currentTimeMillis() + timeout; + KeeperException lastException = null; + for(int i = 0; i < maxTries && endTime > System.currentTimeMillis(); 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 - System.currentTimeMillis()); + } + } + 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/src/java/systest/org/apache/zookeeper/test/system/NoAssignmentException.java b/src/java/systest/org/apache/zookeeper/test/system/NoAssignmentException.java new file mode 100644 index 0000000..1ac1390 --- /dev/null +++ b/src/java/systest/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/src/java/systest/org/apache/zookeeper/test/system/NoAvailableContainers.java b/src/java/systest/org/apache/zookeeper/test/system/NoAvailableContainers.java new file mode 100644 index 0000000..b8aabcc --- /dev/null +++ b/src/java/systest/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/src/java/systest/org/apache/zookeeper/test/system/QuorumPeerInstance.java b/src/java/systest/org/apache/zookeeper/test/system/QuorumPeerInstance.java new file mode 100644 index 0000000..95aebc8 --- /dev/null +++ b/src/java/systest/org/apache/zookeeper/test/system/QuorumPeerInstance.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.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.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; + +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", "build/test/data")); + + private static final int syncLimit = 3; + private static final int initLimit = 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 quorumAddr; + HashMap 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.info("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()); + quorumAddr = (InetSocketAddress) ss.getLocalSocketAddress(); + ss.close(); + } catch(IOException e) { + e.printStackTrace(); + } + String report = clientAddr.getHostName() + ':' + clientAddr.getPort() + + ',' + quorumAddr.getHostName() + ':' + quorumAddr.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++) { + String subparts[] = parts[i].split(":"); + peers.put(Long.valueOf(i), new QuorumServer(i, subparts[0], Integer.parseInt(subparts[1]), 0, null)); + } + 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(), 0, serverId, tickTime, initLimit, syncLimit); + 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() { + } + + static private void recursiveDelete(File dir) { + if (!dir.isDirectory()) { + dir.delete(); + return; + } + for(File f: dir.listFiles()) { + recursiveDelete(f); + } + dir.delete(); + } + + public void stop() { + if (LOG.isDebugEnabled()) { + LOG.debug("Stopping peer " + serverId); + } + if (peer != null) { + peer.shutdown(); + } + if (logDir != null) { + recursiveDelete(logDir); + } + if (snapDir != null) { + recursiveDelete(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 fo 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/src/java/systest/org/apache/zookeeper/test/system/SimpleClient.java b/src/java/systest/org/apache/zookeeper/test/system/SimpleClient.java new file mode 100644 index 0000000..a8cd3e5 --- /dev/null +++ b/src/java/systest/org/apache/zookeeper/test/system/SimpleClient.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.system; + +import java.io.IOException; + +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/src/java/systest/org/apache/zookeeper/test/system/SimpleSysTest.java b/src/java/systest/org/apache/zookeeper/test/system/SimpleSysTest.java new file mode 100644 index 0000000..9cdf4d9 --- /dev/null +++ b/src/java/systest/org/apache/zookeeper/test/system/SimpleSysTest.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.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.Test; + +/** + * 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 = System.currentTimeMillis() + timeout; + while(!connected && end > System.currentTimeMillis()) { + 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); + assertEquals("orig", new String(b)); + } catch(NoNodeException e) { + if (j+1 == maxTries) { + 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) { + assertTrue("Connection didn't recover", waitForConnect(zk, 10000)); + try { + zk.setData("/simpleCase", "new".getBytes(), -1); + } catch(ConnectionLossException e) { + 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 + assertTrue("Servers didn't bounce", waitForConnect(zk, 15000)); + try { + zk.getData("/simpleCase", false, stat); + } catch(ConnectionLossException e) { + 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) { + 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) { + 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/src/java/test/bin/check_compatibility.py b/src/java/test/bin/check_compatibility.py new file mode 100644 index 0000000..cad8195 --- /dev/null +++ b/src/java/test/bin/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/src/java/test/bin/test-github-pr.sh b/src/java/test/bin/test-github-pr.sh new file mode 100644 index 0000000..e155769 --- /dev/null +++ b/src/java/test/bin/test-github-pr.sh @@ -0,0 +1,616 @@ +#!/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 [[ $# != 14 ]] ; 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 + FORREST_HOME=${10} + BASEDIR=${11} + JIRA_PASSWD=${12} + JAVA5_HOME=${13} + CURL=${14} + 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 [[ $# != 10 ]] ; 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 + FORREST_HOME=$8 + BASEDIR=$9 + JAVA5_HOME=${10} + ### 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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= findbugs" + $ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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 -Dforrest.home=$FORREST_HOME -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 -Dforrest.home=$FORREST_HOME -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 [[ $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/src/java/test/bin/test-patch.properties b/src/java/test/bin/test-patch.properties new file mode 100644 index 0000000..c467e63 --- /dev/null +++ b/src/java/test/bin/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=24 +OK_FINDBUGS_WARNINGS=0 +OK_JAVADOC_WARNINGS=0 diff --git a/src/java/test/bin/test-patch.sh b/src/java/test/bin/test-patch.sh new file mode 100644 index 0000000..d42789b --- /dev/null +++ b/src/java/test/bin/test-patch.sh @@ -0,0 +1,637 @@ +#!/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 +ulimit -n 1024 + +### Setup some variables. +### SVN_REVISION 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 [[ $# != 15 ]] ; then + echo "ERROR: usage $0 HUDSON " + cleanupAndExit 0 + fi + PATCH_DIR=$2 + PS=$3 + WGET=$4 + JIRACLI=$5 + SVN=$6 + GREP=$7 + PATCH=$8 + FINDBUGS_HOME=$9 + FORREST_HOME=${10} + BASEDIR=${11} + JIRA_PASSWD=${12} + JAVA5_HOME=${13} + CURL=${14} + defect=${15} + + ### 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 [[ $# != 10 ]] ; 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 + SVN=$4 + GREP=$5 + PATCH=$6 + FINDBUGS_HOME=$7 + FORREST_HOME=$8 + BASEDIR=$9 + JAVA5_HOME=${10} + ### 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 + status=`$SVN stat --ignore-externals | sed -e '/^X[ ]*/D'` + if [[ $HUDSON == "false" ]] ; then + if [[ "$status" != "" ]] ; then + echo "ERROR: can't run in a workspace that contains the following modifications" + echo "$status" + cleanupAndExit 1 + fi + else + cd $BASEDIR + $SVN revert -R . + rm -rf `$SVN status --no-ignore` + $SVN update + 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 ${SVN_REVISION}." + + ### 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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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} -Dforrest.home=${FORREST_HOME} -DZookeeperPatchProcess= findbugs" + $ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -Dforrest.home=${FORREST_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 -Dcompile.c++=yes -Dforrest.home=$FORREST_HOME -Djava5.home=$JAVA5_HOME test-core" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=yes -Dcompile.c++=yes -Dforrest.home=$FORREST_HOME -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=hudson + $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/src/java/test/checkstyle-noframes-sorted.xsl b/src/java/test/checkstyle-noframes-sorted.xsl new file mode 100644 index 0000000..5f9e93b --- /dev/null +++ b/src/java/test/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/src/java/test/checkstyle.xml b/src/java/test/checkstyle.xml new file mode 100644 index 0000000..a5d5182 --- /dev/null +++ b/src/java/test/checkstyle.xml @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java/test/config/findbugsExcludeFile.xml b/src/java/test/config/findbugsExcludeFile.xml new file mode 100644 index 0000000..c023f4f --- /dev/null +++ b/src/java/test/config/findbugsExcludeFile.xml @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java/test/data/buffersize/create/version-2/log.1 b/src/java/test/data/buffersize/create/version-2/log.1 new file mode 100644 index 0000000..4f05bc1 Binary files /dev/null and b/src/java/test/data/buffersize/create/version-2/log.1 differ diff --git a/src/java/test/data/buffersize/create/version-2/snapshot.0 b/src/java/test/data/buffersize/create/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/src/java/test/data/buffersize/create/version-2/snapshot.0 differ diff --git a/src/java/test/data/buffersize/set/version-2/log.1 b/src/java/test/data/buffersize/set/version-2/log.1 new file mode 100644 index 0000000..8586f16 Binary files /dev/null and b/src/java/test/data/buffersize/set/version-2/log.1 differ diff --git a/src/java/test/data/buffersize/set/version-2/snapshot.0 b/src/java/test/data/buffersize/set/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/src/java/test/data/buffersize/set/version-2/snapshot.0 differ diff --git a/src/java/test/data/buffersize/snapshot/version-2/log.1 b/src/java/test/data/buffersize/snapshot/version-2/log.1 new file mode 100644 index 0000000..2f145a6 Binary files /dev/null and b/src/java/test/data/buffersize/snapshot/version-2/log.1 differ diff --git a/src/java/test/data/buffersize/snapshot/version-2/snapshot.0 b/src/java/test/data/buffersize/snapshot/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/src/java/test/data/buffersize/snapshot/version-2/snapshot.0 differ diff --git a/src/java/test/data/buffersize/snapshot/version-2/snapshot.2 b/src/java/test/data/buffersize/snapshot/version-2/snapshot.2 new file mode 100644 index 0000000..59dfa70 Binary files /dev/null and b/src/java/test/data/buffersize/snapshot/version-2/snapshot.2 differ diff --git a/src/java/test/data/invalidsnap/version-2/log.1 b/src/java/test/data/invalidsnap/version-2/log.1 new file mode 100644 index 0000000..9dc15ae Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/log.1 differ diff --git a/src/java/test/data/invalidsnap/version-2/log.274 b/src/java/test/data/invalidsnap/version-2/log.274 new file mode 100644 index 0000000..3bb868e Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/log.274 differ diff --git a/src/java/test/data/invalidsnap/version-2/log.42 b/src/java/test/data/invalidsnap/version-2/log.42 new file mode 100644 index 0000000..5385be5 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/log.42 differ diff --git a/src/java/test/data/invalidsnap/version-2/log.63b b/src/java/test/data/invalidsnap/version-2/log.63b new file mode 100644 index 0000000..4ee0a98 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/log.63b differ diff --git a/src/java/test/data/invalidsnap/version-2/snapshot.0 b/src/java/test/data/invalidsnap/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/snapshot.0 differ diff --git a/src/java/test/data/invalidsnap/version-2/snapshot.272 b/src/java/test/data/invalidsnap/version-2/snapshot.272 new file mode 100644 index 0000000..71f32a5 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/snapshot.272 differ diff --git a/src/java/test/data/invalidsnap/version-2/snapshot.273 b/src/java/test/data/invalidsnap/version-2/snapshot.273 new file mode 100644 index 0000000..3146f56 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/snapshot.273 differ diff --git a/src/java/test/data/invalidsnap/version-2/snapshot.639 b/src/java/test/data/invalidsnap/version-2/snapshot.639 new file mode 100644 index 0000000..cf9e389 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/snapshot.639 differ diff --git a/src/java/test/data/invalidsnap/version-2/snapshot.83f b/src/java/test/data/invalidsnap/version-2/snapshot.83f new file mode 100644 index 0000000..26dc5f6 Binary files /dev/null and b/src/java/test/data/invalidsnap/version-2/snapshot.83f differ diff --git a/src/java/test/data/kerberos/minikdc-krb5.conf b/src/java/test/data/kerberos/minikdc-krb5.conf new file mode 100644 index 0000000..43ec7c4 --- /dev/null +++ b/src/java/test/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/src/java/test/data/kerberos/minikdc.ldiff b/src/java/test/data/kerberos/minikdc.ldiff new file mode 100644 index 0000000..20c8d77 --- /dev/null +++ b/src/java/test/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/src/java/test/data/upgrade/log.100000001 b/src/java/test/data/upgrade/log.100000001 new file mode 100644 index 0000000..00655f1 Binary files /dev/null and b/src/java/test/data/upgrade/log.100000001 differ diff --git a/src/java/test/data/upgrade/log.100001bf0 b/src/java/test/data/upgrade/log.100001bf0 new file mode 100644 index 0000000..83fa697 Binary files /dev/null and b/src/java/test/data/upgrade/log.100001bf0 differ diff --git a/src/java/test/data/upgrade/snapshot.100000000 b/src/java/test/data/upgrade/snapshot.100000000 new file mode 100644 index 0000000..56f9015 Binary files /dev/null and b/src/java/test/data/upgrade/snapshot.100000000 differ diff --git a/src/java/test/data/upgrade/snapshot.100001bec b/src/java/test/data/upgrade/snapshot.100001bec new file mode 100644 index 0000000..0010d1e Binary files /dev/null and b/src/java/test/data/upgrade/snapshot.100001bec differ diff --git a/src/java/test/org/apache/jute/BinaryInputArchiveTest.java b/src/java/test/org/apache/jute/BinaryInputArchiveTest.java new file mode 100644 index 0000000..a8d067d --- /dev/null +++ b/src/java/test/org/apache/jute/BinaryInputArchiveTest.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.jute; + +import junit.framework.Assert; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + + +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(""); + Assert.fail("Should have thrown an IOException"); + } catch (IOException e) { + Assert.assertTrue("Not 'Unreasonable length' exception: " + e, + e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH)); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/ClientReconnectTest.java b/src/java/test/org/apache/zookeeper/ClientReconnectTest.java new file mode 100644 index 0000000..8d66a7e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/ClientReconnectTest.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; + + +import static org.mockito.Matchers.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 junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.zookeeper.client.HostProvider; +import org.junit.Test; + +public class ClientReconnectTest extends TestCase { + private SocketChannel sc; + private CountDownLatch countDownLatch = new CountDownLatch(3); + + class MockCnxn extends ClientCnxnSocketNIO { + MockCnxn() throws IOException { + super(); + } + + @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(1111); + when(hostProvider.next(anyLong())).thenReturn(inaddr); + ZooKeeper zk = mock(ZooKeeper.class); + 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); + Assert.assertTrue(countDownLatch.getCount() == 0); + clientCnxn.close(); + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/JUnit4ZKTestRunner.java b/src/java/test/org/apache/zookeeper/JUnit4ZKTestRunner.java new file mode 100644 index 0000000..37149cc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/JUnit4ZKTestRunner.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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +/** + * 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); + } + + public 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.info("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/src/java/test/org/apache/zookeeper/JaasConfiguration.java b/src/java/test/org/apache/zookeeper/JaasConfiguration.java new file mode 100644 index 0000000..bf145d0 --- /dev/null +++ b/src/java/test/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); + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/MockPacket.java b/src/java/test/org/apache/zookeeper/MockPacket.java new file mode 100644 index 0000000..f4bb19a --- /dev/null +++ b/src/java/test/org/apache/zookeeper/MockPacket.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.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooKeeper.WatchRegistration; +import java.nio.ByteBuffer; + +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/src/java/test/org/apache/zookeeper/MultiResponseTest.java b/src/java/test/org/apache/zookeeper/MultiResponseTest.java new file mode 100644 index 0000000..a03feb3 --- /dev/null +++ b/src/java/test/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 junit.framework.TestCase; +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; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +public class MultiResponseTest extends TestCase { + 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/src/java/test/org/apache/zookeeper/MultiTransactionRecordTest.java b/src/java/test/org/apache/zookeeper/MultiTransactionRecordTest.java new file mode 100644 index 0000000..3749564 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/MultiTransactionRecordTest.java @@ -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. + */ + +package org.apache.zookeeper; + +import junit.framework.TestCase; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +public class MultiTransactionRecordTest extends TestCase { + @Test + public void testRoundTrip() throws IOException { + MultiTransactionRecord request = new MultiTransactionRecord(); + 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)); + + MultiTransactionRecord decodedRequest = codeDecode(request); + + assertEquals(request, decodedRequest); + assertEquals(request.hashCode(), decodedRequest.hashCode()); + } + + @Test + public void testEmptyRoundTrip() throws IOException { + MultiTransactionRecord request = new MultiTransactionRecord(); + MultiTransactionRecord decodedRequest = codeDecode(request); + + assertEquals(request, decodedRequest); + assertEquals(request.hashCode(), decodedRequest.hashCode()); + } + + private MultiTransactionRecord codeDecode(MultiTransactionRecord 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)); + MultiTransactionRecord decodedRequest = new MultiTransactionRecord(); + decodedRequest.deserialize(bia, "request"); + return decodedRequest; + } +} diff --git a/src/java/test/org/apache/zookeeper/PortAssignment.java b/src/java/test/org/apache/zookeeper/PortAssignment.java new file mode 100644 index 0000000..06f9361 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/PortAssignment.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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Assign ports to tests */ +public class PortAssignment { + private static final Logger LOG = LoggerFactory.getLogger(PortAssignment.class); + + private static int nextPort = 11221; + + /** Assign a new, unique port to the test */ + public synchronized static int unique() { + LOG.info("assigning port " + nextPort); + return nextPort++; + } +} diff --git a/src/java/test/org/apache/zookeeper/SaslAuthTest.java b/src/java/test/org/apache/zookeeper/SaslAuthTest.java new file mode 100644 index 0000000..eac0703 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/SaslAuthTest.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; + +import static org.junit.Assert.assertTrue; + +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.atomic.AtomicInteger; + +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.Assert; +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 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 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); + Assert.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); + } + +} diff --git a/src/java/test/org/apache/zookeeper/ServerConfigTest.java b/src/java/test/org/apache/zookeeper/ServerConfigTest.java new file mode 100644 index 0000000..27faa74 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/ServerConfigTest.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 org.apache.zookeeper.server.ServerConfig; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +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); + } + + 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); + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/TestableZooKeeper.java b/src/java/test/org/apache/zookeeper/TestableZooKeeper.java new file mode 100644 index 0000000..dd70056 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/TestableZooKeeper.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; + +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.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; + +public class TestableZooKeeper extends ZooKeeper { + + public TestableZooKeeper(String host, int sessionTimeout, + Watcher watcher) throws IOException { + super(host, sessionTimeout, watcher); + } + + @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 boolean testableWaitForShutdown(int wait) + throws InterruptedException + { + return super.testableWaitForShutdown(wait); + } + + 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/src/java/test/org/apache/zookeeper/VerGenTest.java b/src/java/test/org/apache/zookeeper/VerGenTest.java new file mode 100644 index 0000000..607edbb --- /dev/null +++ b/src/java/test/org/apache/zookeeper/VerGenTest.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; + +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.Assert; +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) +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); + Assert.assertEquals(expected[0], v.maj); + Assert.assertEquals(expected[1], v.min); + Assert.assertEquals(expected[2], v.micro); + Assert.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/src/java/test/org/apache/zookeeper/ZKTestCase.java b/src/java/test/org/apache/zookeeper/ZKTestCase.java new file mode 100644 index 0000000..9098fc4 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/ZKTestCase.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 org.junit.Assume; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.junit.Rule; +import org.junit.rules.MethodRule; +import org.junit.rules.TestWatchman; +import org.junit.runner.RunWith; +import org.junit.runners.model.FrameworkMethod; + +import java.io.IOException; +import java.net.Inet6Address; +import java.net.InetAddress; + +/** + * 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 { + private static final Logger LOG = LoggerFactory.getLogger(ZKTestCase.class); + + private String testName; + + protected String getTestName() { + return testName; + } + + @Rule + public MethodRule watchman = new TestWatchman() { + @Override + public void starting(FrameworkMethod method) { + testName = method.getName(); + // ZOOKEEPER-2693 disables all 4lw by default. + // Here we enable the 4lw which ZooKeeper tests depends. + System.setProperty("zookeeper.4lw.commands.whitelist", "*"); + + LOG.info("STARTING " + testName); + } + + @Override + public void finished(FrameworkMethod method) { + LOG.info("FINISHED " + testName); + } + + @Override + public void succeeded(FrameworkMethod method) { + LOG.info("SUCCEEDED " + testName); + } + + @Override + public void failed(Throwable e, FrameworkMethod method) { + LOG.info("FAILED " + testName, e); + } + + }; + + protected void assumeIPv6Available() { + try { + InetAddress address = Inet6Address.getByName("0:0:0:0:0:0:0:1"); + Assume.assumeTrue(address.isReachable(1000)); + } catch (IOException exception) { + Assume.assumeTrue(false); + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/ZooKeeperTest.java b/src/java/test/org/apache/zookeeper/ZooKeeperTest.java new file mode 100644 index 0000000..2cdc4cb --- /dev/null +++ b/src/java/test/org/apache/zookeeper/ZooKeeperTest.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; + +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +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.test.ClientBase; +import org.junit.Assert; +import org.junit.Test; + +/** + * + * Testing Zookeeper public methods + * + */ +public class ZooKeeperTest extends ClientBase { + + @Test + public void testDeleteRecursive() 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); + + List children = zk.getChildren("/a", false); + + Assert.assertEquals("2 children - b & c should be present ", children + .size(), 2); + Assert.assertTrue(children.contains("b")); + Assert.assertTrue(children.contains("c")); + + ZKUtil.deleteRecursive(zk, "/a"); + Assert.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); + + Assert.assertEquals("2 children - b & c should be present ", children + .size(), 2); + Assert.assertTrue(children.contains("b")); + Assert.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(); + } + Assert.assertEquals(4, ((AtomicInteger) ctx).get()); + } + + @Test + public void testStatWhenPathDoesNotExist() throws IOException, + InterruptedException { + final ZooKeeper zk = createClient(); + ZooKeeperMain main = new ZooKeeperMain(zk); + String cmdstring = "stat /invalidPath"; + main.cl.parseCommand(cmdstring); + try { + main.processZKCmd(main.cl); + Assert.fail("As Node does not exist, command should fail by throwing No Node Exception."); + } catch (KeeperException e) { + Assert.assertEquals("KeeperErrorCode = NoNode for /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); + Assert.assertEquals("Spaces also considered as characters", zkMain.cl.getNumArguments(), 2); + Assert.assertEquals("ls is not taken as first argument", zkMain.cl.getCmdArgument(0), "ls"); + Assert.assertEquals("/ is not taken as second argument", zkMain.cl.getCmdArgument(1), "/"); + } + + @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 + try{ + zkMain.executeLine(cmdstring); + }catch(KeeperException.InvalidACLException e){ + fail("For Invalid ACls should not throw exception"); + } + } + + @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); + try{ + zkMain.executeLine(cmdstring1); + + }catch(KeeperException.BadVersionException e){ + fail("For Invalid dataversion number should not throw exception"); + } + } + + @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!"); + } + } + + @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); + Assert.assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); + Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + Assert.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); + Assert.assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); + Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + Assert.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); + Assert.assertEquals("empty quotes should produce arguments", zkMain.cl.getNumArguments(), 3); + Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + Assert.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); + Assert.assertEquals("expected 5 arguments", zkMain.cl.getNumArguments(), 4); + Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + Assert.assertEquals("empty string is not taken as third argument", zkMain.cl.getCmdArgument(2), ""); + Assert.assertEquals("empty string is not taken as fourth argument", zkMain.cl.getCmdArgument(3), ""); + } + + // 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"; + + // setup redirect out/err streams to get System.in/err, use this + // judiciously! + final PrintStream systemOut = System.out; // get current out + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + try { + zkMain.executeLine(cmd1); + Assert.assertEquals("Command index out of range", outContent + .toString().trim()); + } finally { + // revert redirect of out/err streams - important step! + System.setOut(systemOut); + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/common/TimeTest.java b/src/java/test/org/apache/zookeeper/common/TimeTest.java new file mode 100644 index 0000000..d938556 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/common/TimeTest.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 org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Calendar; +import java.util.Date; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * 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.printf("Starting\n"); + final TimeTest test = new TimeTest(); + System.out.printf("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 new Watcher() { + @Override + public void process(WatchedEvent 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); + + Assert.assertEquals(calculatedDate, realDate); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/CRCTest.java b/src/java/test/org/apache/zookeeper/server/CRCTest.java new file mode 100644 index 0000000..2b7fb46 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/CRCTest.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; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +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.concurrent.CountDownLatch; +import java.util.zip.Adler32; +import java.util.zip.CheckedInputStream; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +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.Assert; +import org.junit.Test; + +public class CRCTest extends ZKTestCase implements Watcher { + private static final Logger LOG = LoggerFactory.getLogger(CRCTest.class); + + private static final String HOSTPORT = + "127.0.0.1:" + PortAssignment.unique(); + private volatile CountDownLatch startSignal; + + /** + * 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"); + Assert.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 < 2000; i++) { + zk.create("/crctest- " + i , ("/crctest- " + i).getBytes(), + Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + Assert.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()) { + } + Assert.assertTrue(false); + } catch(IOException ie) { + LOG.info("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); + } + Assert.assertTrue(cfile); + } + + 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/src/java/test/org/apache/zookeeper/server/DataNodeTest.java b/src/java/test/org/apache/zookeeper/server/DataNodeTest.java new file mode 100644 index 0000000..6289766 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/DataNodeTest.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; + +import static org.junit.Assert.*; + +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/src/java/test/org/apache/zookeeper/server/DataTreeTest.java b/src/java/test/org/apache/zookeeper/server/DataTreeTest.java new file mode 100644 index 0000000..a484cf5 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/DataTreeTest.java @@ -0,0 +1,235 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.DataTree; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.apache.zookeeper.server.DataNode; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.zookeeper.Quotas; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.common.PathTrie; +import java.lang.reflect.*; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +public class DataTreeTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(DataTreeTest.class); + + private DataTree dt; + + @Before + public void setUp() throws Exception { + dt=new DataTree(); + } + + @After + public void tearDown() throws Exception { + dt=null; + } + + /** + * 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(); + Assert.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 { + class MyWatcher implements Watcher{ + boolean fired=false; + public void process(WatchedEvent event) { + if(event.getPath().equals("/")) + fired=true; + } + } + MyWatcher watcher=new MyWatcher(); + // set a watch on the root node + dt.getChildren("/", new Stat(), watcher); + // add a new node, should trigger a watch + dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion()+1, 1, 1); + Assert.assertFalse("Root node watch not triggered",!watcher.fired); + } + + /** + * For ZOOKEEPER-1046 test if cversion is getting incremented correctly. + */ + @Test(timeout = 60000) + public void testIncrementCversion() throws Exception { + 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(); + dt.setCversionPzxid("/test/", prevCversion + 1, prevPzxid + 1); + int newCversion = zk.stat.getCversion(); + long newPzxid = zk.stat.getPzxid(); + Assert.assertTrue(" verification failed. Expected: <" + + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + + newCversion + ", " + newPzxid + ">", + (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); + } + + @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 + Assert.assertEquals("/bug is still in pTrie", "", pTrie.findMaxPrefix("/bug")); + } + + /* + * 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 { + 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 + Assert.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 + Assert.assertTrue("Didn't find the expected node", ranTestCase.get()); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/DataTreeUnitTest.java b/src/java/test/org/apache/zookeeper/server/DataTreeUnitTest.java new file mode 100644 index 0000000..287c384 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/DataTreeUnitTest.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 org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class DataTreeUnitTest extends ZKTestCase { + DataTree dt; + + @Before + public void setUp() throws Exception { + dt=new DataTree(); + } + + @After + public void tearDown() throws Exception { + dt=null; + } + + @Test + public void testRootWatchTriggered() throws Exception { + class MyWatcher implements Watcher{ + boolean fired=false; + public void process(WatchedEvent event) { + if(event.getPath().equals("/")) + fired=true; + } + } + MyWatcher watcher=new MyWatcher(); + // set a watch on the root node + dt.getChildren("/", new Stat(), watcher); + // add a new node, should trigger a watch + dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion()+1, 1, 1); + Assert.assertFalse("Root node watch not triggered",!watcher.fired); + } + +} diff --git a/src/java/test/org/apache/zookeeper/server/DatadirCleanupManagerTest.java b/src/java/test/org/apache/zookeeper/server/DatadirCleanupManagerTest.java new file mode 100644 index 0000000..6b2f45f --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/DatadirCleanupManagerTest.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; + +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 java.io.File; + +import junit.framework.Assert; + +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DatadirCleanupManagerTest { + + private DatadirCleanupManager purgeMgr; + private String snapDir; + private String dataLogDir; + + @Before + public void setUp() throws Exception { + File dataDir = ClientBase.createTmpDir(); + snapDir = dataDir.getAbsolutePath(); + dataLogDir = dataDir.getAbsolutePath(); + } + + @Test + public void testPurgeTask() throws Exception { + purgeMgr = new DatadirCleanupManager(snapDir, dataLogDir, 3, 1); + purgeMgr.start(); + Assert.assertEquals("Data log directory is not set as configured", + dataLogDir, purgeMgr.getDataLogDir()); + Assert.assertEquals("Snapshot directory is not set as configured", + snapDir, purgeMgr.getSnapDir()); + Assert.assertEquals("Snapshot retain count is not set as configured", + 3, purgeMgr.getSnapRetainCount()); + Assert.assertEquals("Purge task is not started", STARTED, purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + Assert.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(); + Assert.assertEquals("Purge task is scheduled with zero purge interval", NOT_STARTED, + purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + Assert.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(); + Assert.assertEquals("Purge task is scheduled with negative purge interval", + NOT_STARTED, purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + Assert.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/src/java/test/org/apache/zookeeper/server/DeserializationPerfTest.java b/src/java/test/org/apache/zookeeper/server/DeserializationPerfTest.java new file mode 100644 index 0000000..fd850e7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/DeserializationPerfTest.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.server; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Assert; +import org.junit.Test; + +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, tree.getNode("/").stat.getCversion(), width, 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; + + Assert.assertEquals(count, dserTree.getNodeCount()); + + LOG.info("Deserialized " + count + " nodes in " + durationms + + " ms (" + pernodeus + "us/node), depth=" + depth + " width=" + + width + " datalen=" + 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/src/java/test/org/apache/zookeeper/server/InvalidSnapCountTest.java b/src/java/test/org/apache/zookeeper/server/InvalidSnapCountTest.java new file mode 100644 index 0000000..3a66164 --- /dev/null +++ b/src/java/test/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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Assert; +import org.junit.Test; + +/** + * 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 = dataDir.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"); + 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(); + + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, + CONNECTION_TIMEOUT)); + + Assert.assertEquals(SyncRequestProcessor.getSnapCount(), 2); + + main.shutdown(); + + } + + public void process(WatchedEvent event) { + // ignore for this test + } +} diff --git a/src/java/test/org/apache/zookeeper/server/InvalidSnapshotTest.java b/src/java/test/org/apache/zookeeper/server/InvalidSnapshotTest.java new file mode 100644 index 0000000..97ad792 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/InvalidSnapshotTest.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; + +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/src/java/test/org/apache/zookeeper/server/MockNIOServerCnxn.java b/src/java/test/org/apache/zookeeper/server/MockNIOServerCnxn.java new file mode 100644 index 0000000..53ad1f6 --- /dev/null +++ b/src/java/test/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.nio.channels.SelectionKey; +import java.nio.channels.SocketChannel; +import java.io.IOException; + +public class MockNIOServerCnxn extends NIOServerCnxn { + + public MockNIOServerCnxn(ZooKeeperServer zk, SocketChannel sock, + SelectionKey sk, NIOServerCnxnFactory factory) + throws IOException { + super(zk, sock, sk, factory); + } + + /** + * 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/src/java/test/org/apache/zookeeper/server/NIOServerCnxnTest.java b/src/java/test/org/apache/zookeeper/server/NIOServerCnxnTest.java new file mode 100644 index 0000000..bdee20f --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/NIOServerCnxnTest.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; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.CancelledKeyException; + +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.test.ClientBase; +import org.junit.Assert; +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); + Assert.assertNotNull("Didn't create znode:" + path, + zk.exists(path, false)); + // Defaults ServerCnxnFactory would be instantiated with + // NIOServerCnxnFactory + Assert.assertTrue( + "Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!", + serverFactory instanceof NIOServerCnxnFactory); + Iterable connections = serverFactory.getConnections(); + for (ServerCnxn serverCnxn : connections) { + serverCnxn.close(); + try { + serverCnxn.toString(); + } catch (Exception e) { + LOG.error("Exception while getting connection details!", e); + Assert.fail("Shouldn't throw exception while " + + "getting connection details!"); + } + } + } finally { + zk.close(); + } + } + + /** + * Mock extension of NIOServerCnxn to test for + * CancelledKeyException (ZOOKEEPER-2044). + */ + private static class MockNIOServerCnxn extends NIOServerCnxn { + public MockNIOServerCnxn(NIOServerCnxn cnxn) + throws IOException { + super(cnxn.zkServer, cnxn.sock, cnxn.sk, cnxn.factory); + } + + public void mockSendBuffer(ByteBuffer bb) throws Exception { + super.internalSendBuffer(bb); + } + } + + @Test(timeout = 30000) + public void testValidSelectionKey() throws Exception { + final ZooKeeper zk = createZKClient(hostPort, 3000); + try { + Iterable connections = serverFactory.getConnections(); + for (ServerCnxn serverCnxn : connections) { + MockNIOServerCnxn mock = new MockNIOServerCnxn((NIOServerCnxn) serverCnxn); + // Cancel key + ((NIOServerCnxn) serverCnxn).sock.keyFor(((NIOServerCnxnFactory) serverFactory).selector).cancel();; + mock.mockSendBuffer(ByteBuffer.allocate(8)); + } + } catch (CancelledKeyException e) { + LOG.error("Exception while sending bytes!", e); + Assert.fail(e.toString()); + } finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/NettyServerCnxnTest.java b/src/java/test/org/apache/zookeeper/server/NettyServerCnxnTest.java new file mode 100644 index 0000000..52265c6 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/NettyServerCnxnTest.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; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import junit.framework.Assert; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.NettyServerCnxnFactory.CnxnChannelHandler; +import org.apache.zookeeper.test.ClientBase; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelStateEvent; +import org.junit.Test; +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"); + super.setUp(); + } + + /** + * 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 = 30000) + public void testSendCloseSession() throws Exception { + Assert.assertTrue( + "Didn't instantiate ServerCnxnFactory with NettyServerCnxnFactory!", + serverFactory instanceof NettyServerCnxnFactory); + + NettyServerCnxnFactory nettyServerFactory = (NettyServerCnxnFactory) serverFactory; + final CountDownLatch channelLatch = new CountDownLatch(1); + CnxnChannelHandler channelHandler = nettyServerFactory.new CnxnChannelHandler() { + @Override + public void channelDisconnected(ChannelHandlerContext ctx, + ChannelStateEvent e) throws Exception { + LOG.info("Recieves channel disconnected event"); + channelLatch.countDown(); + } + }; + LOG.info("Adding custom channel handler for simulation"); + nettyServerFactory.bootstrap.getPipeline().remove("servercnxnfactory"); + nettyServerFactory.bootstrap.getPipeline().addLast("servercnxnfactory", + channelHandler); + + final ZooKeeper zk = createClient(); + final String path = "/a"; + try { + // make sure zkclient works + zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + Assert.assertNotNull("Didn't create znode:" + path, + zk.exists(path, false)); + Iterable connections = serverFactory.getConnections(); + Assert.assertEquals("Mismatch in number of live connections!", 1, + serverFactory.getNumAliveConnections()); + for (ServerCnxn serverCnxn : connections) { + serverCnxn.sendCloseSession(); + } + LOG.info("Waiting for the channel disconnected event"); + channelLatch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertEquals("Mismatch in number of live connections!", 0, + serverFactory.getNumAliveConnections()); + } finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java b/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java new file mode 100644 index 0000000..d86cf2a --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/PrepRequestProcessorTest.java @@ -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. + */ + +package org.apache.zookeeper.server; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.MultiTransactionRecord; +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.SetDataRequest; +import org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.ErrorTxn; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +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; + + @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); + Assert.assertTrue("waiting for server being up ", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zks.sessionTracker = new MySessionTracker(); + } + + @After + public void teardown() throws Exception { + if (servcnxnf != null) { + servcnxnf.shutdown(); + } + if (zks != null) { + zks.shutdown(); + } + } + + @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); + + Assert.assertEquals("Request should have marshalling error", new ErrorTxn(KeeperException.Code.MARSHALLINGERROR.intValue()), + outcome.txn); + Assert.assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); + } + + 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(); + + // Id + List ids = Arrays.asList(Ids.ANYONE_ID_UNSAFE); + + return new Request(null, 1l, 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 MultiTransactionRecord(ops); + Request req = createRequest(record, OpCode.multi); + + processor.pRequest(req); + Assert.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); + + Assert.assertNull(zks.outstandingChangesForPath.get("/foo")); + + process(Arrays.asList( + Op.setData("/foo", new byte[0], -1))); + + ChangeRecord cr = zks.outstandingChangesForPath.get("/foo"); + Assert.assertNotNull("Change record wasn't set", cr); + Assert.assertEquals("Record zxid wasn't set correctly", + 1, cr.zxid); + + process(Arrays.asList( + Op.delete("/foo", -1))); + cr = zks.outstandingChangesForPath.get("/foo"); + Assert.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. + Assert.assertEquals("Record zxid wasn't set correctly", + 2, cr.zxid); + } + + /** + * 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); + + Assert.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. + Assert.assertNull(zks.outstandingChangesForPath.get("/foo")); + } + + /** + * 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); + processor.pRequest(req); + pLatch.await(); + Assert.assertEquals(outcome.hdr.getType(), OpCode.error); + Assert.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 void addSession(long id, int to) { + // TODO Auto-generated method stub + + } + @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 + + } + @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 + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/PurgeTxnTest.java b/src/java/test/org/apache/zookeeper/server/PurgeTxnTest.java new file mode 100644 index 0000000..1685a5b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/PurgeTxnTest.java @@ -0,0 +1,604 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.File; +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.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.data.Stat; + +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.PurgeTxnLog; +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.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PurgeTxnTest extends ZKTestCase implements Watcher { + 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 = 90000; + private File tmpDir; + + @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 { + 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); + Assert.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< 2000; i++) { + zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.getTxnLogFactory().close(); + Assert.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.findNRecentSnapshots(4); + int numSnaps = 0; + for (File ff: listLogs) { + if (ff.getName().startsWith("snapshot")) { + numSnaps++; + } + } + Assert.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 { + tmpDir = ClientBase.createTmpDir(); + 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); + Assert.assertTrue("waiting for server being up ", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + final ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + 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"); + Assert.assertTrue("Purging is not finished!", purgeFinished.await( + OP_TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS)); + Assert.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); + Assert.fail("Unexpected exception when visiting znode!"); + } + } + zk.close(); + f.shutdown(); + zks.shutdown(); + zks.getTxnLogFactory().close(); + } + + /** + * Tests finding n recent snapshots from set of snapshots and data logs + */ + @Test + public void testFindNRecentSnapshots() throws Exception { + int nRecentSnap = 4; // n recent snap shots + int nRecentCount = 30; + int offset = 0; + + tmpDir = ClientBase.createTmpDir(); + File version2 = new File(tmpDir.toString(), "version-2"); + Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(), + version2.mkdir()); + + // Test that with no snaps, findNRecentSnapshots returns empty list + FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); + List foundSnaps = txnLog.findNRecentSnapshots(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)); + Assert.assertTrue("Failed to create log File:" + logFile.toString(), + logFile.createNewFile()); + // simulate snapshot file + File snapFile = new File(version2 + "/snapshot." + + Long.toHexString(--counter)); + Assert.assertTrue("Failed to create snap File:" + snapFile.toString(), + snapFile.createNewFile()); + // 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 nRecentSnapFiles = txnLog.findNRecentSnapshots(nRecentSnap); + Assert.assertEquals("exactly 4 snapshots ", 4, + nRecentSnapFiles.size()); + expectedNRecentSnapFiles.removeAll(nRecentSnapFiles); + Assert.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) + nRecentSnapFiles = txnLog.findNRecentSnapshots(nRecentCount + 5); + assertEquals(nRecentCount, nRecentSnapFiles.size()); + for (File f: nRecentSnapFiles) { + Assert.assertTrue("findNRecentSnapshots() 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); + tmpDir = ClientBase.createTmpDir(); + File version2 = new File(tmpDir.toString(), "version-2"); + Assert.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); + tmpDir = ClientBase.createTmpDir(); + File version2 = new File(tmpDir.toString(), "version-2"); + Assert.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); + tmpDir = ClientBase.createTmpDir(); + File version2 = new File(tmpDir.toString(), "version-2"); + Assert.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 { + tmpDir = ClientBase.createTmpDir(); + 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(); + } + + 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); + ClientBase.recursiveDelete(tmpDir); + + } + + /** + * 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 { + tmpDir = ClientBase.createTmpDir(); + 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(); + } + + 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); + ClientBase.recursiveDelete(tmpDir); + + } + + /** + * 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. + 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); + Assert.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(); + Assert.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); + Assert.assertNotNull("Last znode does not exist: " + lastZnode, stat); + + // Shutdown for the last time. + f.shutdown(); + zks.getTxnLogFactory().close(); + zks.shutdown(); + } + + private File createDataDirLogFile(File version_2, int Zxid) throws IOException { + File logFile = new File(version_2 + "/log." + Long.toHexString(Zxid)); + Assert.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)); + Assert.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) { + Assert.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); + for (int indx = 0; indx < thCount; indx++) { + final String myprefix = prefix + "-" + indx; + Thread th = new Thread() { + public void run() { + for (int i = 0; i < 1000; 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 occurred!", e); + } + if (i == 200) { + doPurge.countDown(); + } + } + finished.countDown(); + }; + }; + ths[indx] = th; + } + + for (Thread thread : ths) { + thread.start(); + } + try { + Assert.assertTrue("ZkClient ops is not finished!", + finished.await(OP_TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException ie) { + LOG.error("Unexpected exception occurred!", ie); + Assert.fail("Unexpected exception occurred!"); + } + return znodes; + } + + public void process(WatchedEvent event) { + // do nothing + } + +} diff --git a/src/java/test/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java b/src/java/test/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java new file mode 100644 index 0000000..795472f --- /dev/null +++ b/src/java/test/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 org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +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; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +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; + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/SerializationPerfTest.java b/src/java/test/org/apache/zookeeper/server/SerializationPerfTest.java new file mode 100644 index 0000000..4398232 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/SerializationPerfTest.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.IOException; +import java.io.OutputStream; + +import org.apache.jute.BinaryOutputArchive; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +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 " + count + " nodes in " + + durationms + " ms (" + pernodeus + "us/node), depth=" + + depth + " width=" + width + " datalen=" + 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/src/java/test/org/apache/zookeeper/server/ServerCnxnFactoryAccessor.java b/src/java/test/org/apache/zookeeper/server/ServerCnxnFactoryAccessor.java new file mode 100644 index 0000000..0dfdcfe --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ServerCnxnFactoryAccessor.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.server; + +public class ServerCnxnFactoryAccessor { + public static ZooKeeperServer getZkServer(ServerCnxnFactory fac) { + return fac.zkServer; + } +} diff --git a/src/java/test/org/apache/zookeeper/server/ServerStatsTest.java b/src/java/test/org/apache/zookeeper/server/ServerStatsTest.java new file mode 100644 index 0000000..89c78f9 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ServerStatsTest.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; + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.Time; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; + +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 ... + Assert.assertEquals(incrementCount, serverStats.getPacketsSent()); + Assert.assertEquals(incrementCount*2, serverStats.getPacketsReceived()); + + // When reset ... + serverStats.resetRequestCounters(); + + // Then ... + assertAllPacketsZero(serverStats); + + } + + @Test + public void testLatencyMetrics() { + // Given ... + ServerStats serverStats = new ServerStats(providerMock); + + // When incremented... + serverStats.updateLatency(Time.currentElapsedTime()-1000); + serverStats.updateLatency(Time.currentElapsedTime()-2000); + + // Then ... + assertThat("Max latency check", 2000L, + greaterThanOrEqualTo(serverStats.getMaxLatency())); + assertThat("Min latency check", 1000L, + greaterThanOrEqualTo(serverStats.getMinLatency())); + assertThat("Avg latency check", 1500L, + greaterThanOrEqualTo(serverStats.getAvgLatency())); + + // 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 ... + Assert.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 ... + serverStats.incrementPacketsSent(); + serverStats.incrementPacketsReceived(); + serverStats.updateLatency(Time.currentElapsedTime()-1000); + + serverStats.reset(); + + // Then ... + assertAllPacketsZero(serverStats); + assertAllLatencyZero(serverStats); + } + + private void assertAllPacketsZero(ServerStats serverStats) { + Assert.assertEquals(0L, serverStats.getPacketsSent()); + Assert.assertEquals(0L, serverStats.getPacketsReceived()); + } + + private void assertAllLatencyZero(ServerStats serverStats) { + Assert.assertEquals(0L, serverStats.getMaxLatency()); + Assert.assertEquals(0L, serverStats.getMinLatency()); + Assert.assertEquals(0L, serverStats.getAvgLatency()); + } + + private void assertFsyncThresholdExceedCountZero(ServerStats serverStats) { + Assert.assertEquals(0L, serverStats.getFsyncThresholdExceedCount()); + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/SessionTrackerTest.java b/src/java/test/org/apache/zookeeper/server/SessionTrackerTest.java new file mode 100644 index 0000000..f427102 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/SessionTrackerTest.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; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import junit.framework.Assert; + +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 { + ZooKeeperServer zks = setupSessionTracker(); + + latch = new CountDownLatch(1); + zks.sessionTracker.addSession(sessionId, sessionTimeout); + SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; + SessionImpl sessionImpl = sessionTrackerImpl.sessionsById + .get(sessionId); + Assert.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.addSession(sessionId, sessionTimeout); + try { + sessionTrackerImpl.checkSession(sessionId, sessionOwner); + Assert.fail("Should throw session expiry exception " + + "as the session has expired and closed"); + } catch (KeeperException.SessionExpiredException e) { + // expected behaviour + } + Assert.assertTrue("Session didn't expired", sessionImpl.isClosing()); + Assert.assertFalse("Session didn't expired", sessionTrackerImpl + .touchSession(sessionId, sessionTimeout)); + Assert.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.addSession(sessionId, sessionTimeout); + SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; + SessionImpl sessionImpl = sessionTrackerImpl.sessionsById + .get(sessionId); + Assert.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); + Assert.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(); + 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/src/java/test/org/apache/zookeeper/server/ToStringTest.java b/src/java/test/org/apache/zookeeper/server/ToStringTest.java new file mode 100644 index 0000000..f19469b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ToStringTest.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; + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.proto.SetDataRequest; +import org.junit.Assert; +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); + Assert.assertNotSame("ERROR", req.toString()); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/ZooKeeperServerMainTest.java b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerMainTest.java new file mode 100644 index 0000000..2ffb91e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerMainTest.java @@ -0,0 +1,615 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.fail; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.jute.Record; +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; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.jboss.netty.channel.Channel; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test stand-alone server. + * + */ +public class ZooKeeperServerMainTest extends ZKTestCase implements Watcher { + protected static final Logger LOG = + LoggerFactory.getLogger(ZooKeeperServerMainTest.class); + + 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) throws IOException { + this(clientPort, preCreateDirs, ClientBase.createTmpDir()); + } + + public MainThread(int clientPort, boolean preCreateDirs, File tmpDir) throws IOException { + super("Standalone server with clientPort:" + clientPort); + this.tmpDir = tmpDir; + confFile = new File(this.tmpDir, "zoo.cfg"); + + FileWriter fwriter = new FileWriter(confFile); + fwriter.write("tickTime=2000\n"); + fwriter.write("initLimit=10\n"); + fwriter.write("syncLimit=5\n"); + + 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); + } + } + + String dataDirPath = dataDir.toString(); + String logDirPath = logDir.toString(); + + // Convert windows path to UNIX to avoid problems with "\" + String osname = java.lang.System.getProperty("os.name"); + if (osname.toLowerCase().contains("windows")) { + dataDirPath = dataDirPath.replace('\\', '/'); + logDirPath = logDirPath.replace('\\', '/'); + } + fwriter.write("dataDir=" + dataDirPath + "\n"); + fwriter.write("dataLogDir=" + logDirPath + "\n"); + fwriter.write("clientPort=" + clientPort + "\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 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) on ZooKeeper service will be available + */ + @Test(timeout = 30000) + public void testNonRecoverableError() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + + MainThread main = new MainThread(CLIENT_PORT, true); + main.start(); + + Assert.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); + Assert.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(); + Assert.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); + File tmpDir = main.tmpDir; + main.start(); + Assert.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, false, tmpDir); + main.start(); + + Assert.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); + File tmpDir = main.tmpDir; + main.start(); + Assert.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, false, tmpDir); + main.start(); + + Assert.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); + main.start(); + + Assert.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("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, + ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * Test verifies the auto creation of data dir and data log dir. + */ + @Test(timeout = 30000) + public void testAutoCreateDataLogDir() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + MainThread main = new MainThread(CLIENT_PORT, false); + String args[] = new String[1]; + args[0] = main.confFile.toString(); + main.start(); + + Assert.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("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + Assert.assertEquals(new String(zk.getData("/foo", null, null)), + "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + Assert.assertTrue("waiting for server down", ClientBase + .waitForServerDown("127.0.0.1:" + CLIENT_PORT, + ClientBase.CONNECTION_TIMEOUT)); + } + + @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); + } + } + } + + /** + * Test case to verify that ZooKeeper server is able to shutdown properly + * when there are pending request(s) in the RequestProcessor chain. + * + * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-2347} + */ + @Test(timeout = 30000) + public void testRaceBetweenSyncFlushAndZKShutdown() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + File testDir = File.createTempFile("test", ".dir", tmpDir); + testDir.delete(); + + // Following are the sequence of steps to simulate the deadlock + // situation - SyncRequestProcessor#shutdown holds a lock and waits on + // FinalRequestProcessor to complete a pending operation, which in turn + // also needs the ZooKeeperServer lock + + // 1. start zk server + FileTxnSnapLog ftsl = new FileTxnSnapLog(testDir, testDir); + final SimpleZooKeeperServer zkServer = new SimpleZooKeeperServer(ftsl); + zkServer.startup(); + // 2. Wait for setting up request processor chain. At the end of setup, + // it will add a mock request into the chain + // 3. Also, waiting for FinalRequestProcessor to start processing request + zkServer.waitForFinalProcessRequest(); + // 4. Above step ensures that there is a request in the processor chain. + // Now invoke shutdown, which will acquire zks lock + Thread shutdownThread = new Thread() { + public void run() { + zkServer.shutdown(); + }; + }; + shutdownThread.start(); + // 5. Wait for SyncRequestProcessor to trigger shutdown function. + // This is to ensure that zks lock is acquired + zkServer.waitForSyncReqProcessorShutdown(); + // 6. Now resume FinalRequestProcessor which in turn call + // zks#decInProcess() function and tries to acquire zks lock. + // This results in deadlock + zkServer.resumeFinalProcessRequest(); + // 7. Waiting to finish server shutdown. Testing that + // SyncRequestProcessor#shutdown holds a lock and waits on + // FinalRequestProcessor to complete a pending operation, which in turn + // also needs the ZooKeeperServer lock + shutdownThread.join(); + } + + private class SimpleZooKeeperServer extends ZooKeeperServer { + private SimpleSyncRequestProcessor syncProcessor; + private SimpleFinalRequestProcessor finalProcessor; + + SimpleZooKeeperServer(FileTxnSnapLog ftsl) throws IOException { + super(ftsl, 2000, 2000, 4000, null, new ZKDatabase(ftsl)); + } + + @Override + protected void setupRequestProcessors() { + finalProcessor = new SimpleFinalRequestProcessor(this); + syncProcessor = new SimpleSyncRequestProcessor(this, + finalProcessor); + syncProcessor.start(); + firstProcessor = new PrepRequestProcessor(this, syncProcessor); + ((PrepRequestProcessor) firstProcessor).start(); + + // add request to the chain + addRequestToSyncProcessor(); + } + + private void addRequestToSyncProcessor() { + long zxid = ZxidUtils.makeZxid(3, 7); + TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, + ZooDefs.OpCode.setData); + Record txn = new SetDataTxn("/foo" + zxid, new byte[0], 1); + byte[] buf; + try { + buf = Util.marshallTxnEntry(hdr, txn); + } catch (IOException e) { + LOG.error("IOException while adding request to SyncRequestProcessor", e); + Assert.fail("IOException while adding request to SyncRequestProcessor!"); + return; + } + NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); + final MockNettyServerCnxn nettyCnxn = new MockNettyServerCnxn(null, + this, factory); + Request req = new Request(nettyCnxn, 1, 1, ZooDefs.OpCode.setData, + ByteBuffer.wrap(buf), null); + req.hdr = hdr; + req.txn = txn; + syncProcessor.processRequest(req); + } + + void waitForFinalProcessRequest() throws InterruptedException { + Assert.assertTrue("Waiting for FinalRequestProcessor to start processing request", + finalProcessor.waitForProcessRequestToBeCalled()); + } + + void waitForSyncReqProcessorShutdown() throws InterruptedException { + Assert.assertTrue("Waiting for SyncRequestProcessor to shut down", + syncProcessor.waitForShutdownToBeCalled()); + } + + void resumeFinalProcessRequest() throws InterruptedException { + finalProcessor.resumeProcessRequest(); + } + } + + private class MockNettyServerCnxn extends NettyServerCnxn { + public MockNettyServerCnxn(Channel channel, ZooKeeperServer zks, + NettyServerCnxnFactory factory) { + super(null, null, factory); + } + + @Override + protected synchronized void updateStatsForResponse(long cxid, long zxid, + String op, long start, long end) { + return; + } + + @Override + public synchronized void sendResponse(ReplyHeader h, Record r, + String tag) { + return; + } + } + + private class SimpleFinalRequestProcessor extends FinalRequestProcessor { + private CountDownLatch finalReqProcessCalled = new CountDownLatch(1); + private CountDownLatch resumeFinalReqProcess = new CountDownLatch(1); + private volatile boolean interrupted = false; + public SimpleFinalRequestProcessor(ZooKeeperServer zks) { + super(zks); + } + + @Override + public void processRequest(Request request) { + finalReqProcessCalled.countDown(); + try { + resumeFinalReqProcess.await(ClientBase.CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + LOG.error("Interrupted while waiting to process request", e); + interrupted = true; // Marked as interrupted + resumeFinalReqProcess.countDown(); + return; + } + super.processRequest(request); + } + + boolean waitForProcessRequestToBeCalled() throws InterruptedException { + return finalReqProcessCalled.await(ClientBase.CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + } + + void resumeProcessRequest() throws InterruptedException { + resumeFinalReqProcess.countDown(); + resumeFinalReqProcess.await(ClientBase.CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + Assert.assertFalse("Interrupted while waiting to process request", + interrupted); + } + } + + private class SimpleSyncRequestProcessor extends SyncRequestProcessor { + private final CountDownLatch shutdownCalled = new CountDownLatch(1); + + public SimpleSyncRequestProcessor(ZooKeeperServer zks, + RequestProcessor nextProcessor) { + super(zks, nextProcessor); + } + + @Override + public void shutdown() { + shutdownCalled.countDown(); + super.shutdown(); + } + + boolean waitForShutdownToBeCalled() throws InterruptedException { + return shutdownCalled.await(ClientBase.CONNECTION_TIMEOUT / 3, + TimeUnit.MILLISECONDS); + } + } + + 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); + Assert.assertNotNull("JMX initialization failed!", zks.jmxServerBean); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, + CONNECTION_TIMEOUT)); + return f; + } + + public void process(WatchedEvent event) { + // ignore for this test + } +} diff --git a/src/java/test/org/apache/zookeeper/server/ZooKeeperServerStartupTest.java b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerStartupTest.java new file mode 100644 index 0000000..9435711 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerStartupTest.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.server; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; + +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.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; + +/** + * 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 static final String ZK_NOT_SERVING = "This ZooKeeper instance is not currently serving requests"; + + 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(); + } + if (tmpDir != null) { + ClientBase.recursiveDelete(tmpDir); + } + } + + /** + * Test case for + * {@link 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; + Assert.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); + + Assert.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(); + + Assert.assertTrue("waiting for server being up ", ClientBase + .waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + Assert.assertTrue( + "Failed to invoke zks#createSession() method during client session creation", + simplezks.waitForSessionCreation(5)); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zkClient.close(); + } + + /** + * Test case for + * {@link 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; + Assert.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); + + Assert.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(); + + Assert.assertTrue("waiting for server being up ", ClientBase + .waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + Assert.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); + } else { + System.setProperty( + ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, + originalServerCnxnFactory); + } + } + } + + /** + * Test case for + * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-2383}. + */ + @Test(timeout = 30000) + public void testFourLetterWordsWithNIOServerCnxn() throws Exception { + startSimpleZKServer(startupDelayLatch); + verify("conf", ZK_NOT_SERVING); + verify("crst", ZK_NOT_SERVING); + verify("cons", ZK_NOT_SERVING); + verify("dump", ZK_NOT_SERVING); + verify("mntr", ZK_NOT_SERVING); + verify("stat", ZK_NOT_SERVING); + verify("srst", ZK_NOT_SERVING); + verify("wchs", ZK_NOT_SERVING); + verify("isro", "null"); + } + + /** + * Test case for + * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-2383}. + */ + @Test(timeout = 30000) + public void testFourLetterWordsWithNettyServerCnxn() throws Exception { + String originalServerCnxnFactory = System + .getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + try { + startSimpleZKServer(startupDelayLatch); + verify("conf", ZK_NOT_SERVING); + verify("crst", ZK_NOT_SERVING); + verify("cons", ZK_NOT_SERVING); + verify("dump", ZK_NOT_SERVING); + verify("mntr", ZK_NOT_SERVING); + verify("stat", ZK_NOT_SERVING); + verify("srst", ZK_NOT_SERVING); + verify("wchs", ZK_NOT_SERVING); + verify("isro", "null"); + } finally { + // reset cnxn factory + if (originalServerCnxnFactory == null) { + System.clearProperty( + ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } else { + System.setProperty( + ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, + originalServerCnxnFactory); + } + } + } + + private void verify(String cmd, String expected) + throws IOException { + String resp = sendRequest(cmd); + LOG.info("cmd " + cmd + " expected " + expected + " got " + resp); + Assert.assertTrue("Unexpected response: " + resp, + resp.contains(expected)); + } + + private String sendRequest(String cmd) + throws IOException { + 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) { + Assert.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); + } + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/ZooKeeperServerTest.java b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerTest.java new file mode 100644 index 0000000..2d7aad1 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ZooKeeperServerTest.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.server; + +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.Util; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Assert; +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); + + Assert.assertEquals(orig[2], filelist.get(0)); + Assert.assertEquals(orig[3], filelist.get(1)); + Assert.assertEquals(orig[0], filelist.get(2)); + Assert.assertEquals(orig[1], filelist.get(3)); + Assert.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); + + Assert.assertEquals(orig[4], filelist.get(0)); + Assert.assertEquals(orig[1], filelist.get(1)); + Assert.assertEquals(orig[0], filelist.get(2)); + Assert.assertEquals(orig[3], filelist.get(3)); + Assert.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)); + + Assert.assertEquals(3, filelist.length); + Assert.assertEquals(orig[0], filelist[0]); + Assert.assertEquals(orig[1], filelist[1]); + Assert.assertEquals(orig[4], filelist[2]); + } + + @Test + public void testForceSyncDefaultEnabled() { + File file = new File("foo.10027c6de"); + FileTxnLog log = new FileTxnLog(file); + Assert.assertTrue(log.isForceSync()); + } + + @Test + public void testForceSyncDefaultDisabled() { + try { + File file = new File("foo.10027c6de"); + System.setProperty("zookeeper.forceSync","no"); + FileTxnLog log = new FileTxnLog(file); + Assert.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(); + } + Assert.assertFalse("Snapshot file size is greater than 9 bytes", Util.isValidSnapshot(f)); + Assert.assertTrue("Can't delete file", f.delete()); + } catch (IOException e) { + } finally { + if (null != tmpFileDir) { + ClientBase.recursiveDelete(tmpFileDir); + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/ZooKeeperThreadTest.java b/src/java/test/org/apache/zookeeper/server/ZooKeeperThreadTest.java new file mode 100644 index 0000000..4db02c7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ZooKeeperThreadTest.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; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import junit.framework.Assert; + +import org.junit.Test; + +public class ZooKeeperThreadTest { + 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(); + Assert.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(); + Assert.assertTrue("Uncaught exception is not properly handled.", + runningLatch.await(10000, TimeUnit.MILLISECONDS)); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/ZxidRolloverTest.java b/src/java/test/org/apache/zookeeper/server/ZxidRolloverTest.java new file mode 100644 index 0000000..281f9eb --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/ZxidRolloverTest.java @@ -0,0 +1,444 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +import org.apache.log4j.Logger; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +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.Assert; +import org.junit.Test; + +/** + * Verify ZOOKEEPER-1277 - ensure that we handle epoch rollover correctly. + */ +public class ZxidRolloverTest extends TestCase { + private static final Logger LOG = Logger.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]; + } + + @Override + protected void setUp() throws Exception { + LOG.info("STARTING " + getName()); + + // 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); + Assert.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(",")) { + Assert.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); + Assert.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 (AssertionFailedError 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); + } + + @Override + protected 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 " + start + " thru " + (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 " + start + " thru " + (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/src/java/test/org/apache/zookeeper/server/persistence/FileTxnLogTest.java b/src/java/test/org/apache/zookeeper/server/persistence/FileTxnLogTest.java new file mode 100644 index 0000000..8b74fa7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/persistence/FileTxnLogTest.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 org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.Mockito.mock; + +public class FileTxnLogTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(FileTxnLogTest.class); + + private static final int KB = 1024; + + @Test + public void testInvalidPreallocSize() { + Assert.assertEquals("file should not be padded", + 10 * KB, FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, 0)); + Assert.assertEquals("file should not be padded", + 10 * KB, FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, -1)); + } + + @Test + public void testCalculateFileSizeWithPaddingWhenNotToCurrentSize() { + Assert.assertEquals("file should not be padded", + 10 * KB, FilePadding.calculateFileSizeWithPadding(5 * KB, 10 * KB, 10 * KB)); + } + + @Test + public void testCalculateFileSizeWithPaddingWhenCloseToCurrentSize() { + Assert.assertEquals("file should be padded an additional 10 KB", + 20 * KB, FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, 10 * KB)); + } + + @Test + public void testFileSizeGreaterThanPosition() { + Assert.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(); + Assert.assertTrue(Arrays.equals(createTxn.getData(), data)); + + // Verify the data in the second transaction + fileTxnIterator.next(); + createTxn = (CreateTxn) fileTxnIterator.getTxn(); + Assert.assertTrue(Arrays.equals(createTxn.getData(), new byte[]{})); + } + + @Test + public void testSetPreallocSize() { + long customPreallocSize = 10101; + FileTxnLog.setPreallocSize(customPreallocSize); + Assert.assertThat(FilePadding.getPreAllocSize(), is(equalTo(customPreallocSize))); + } + + @Test + 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("zookeeper.fsync.warningthresholdms", "-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 + Assert.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) + // Assert.assertEquals((long) i + 1 , serverStats.getFsyncThresholdExceedCount()); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.java b/src/java/test/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.java new file mode 100644 index 0000000..0c2da9c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.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.persistence; + +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.TestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +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); + } + + @Test + public void testDirCheckWithCorrectFiles() throws IOException { + twoDirSetupWithCorrectFiles(); + + try { + new FileTxnSnapLog(logDir, snapDir); + } catch (FileTxnSnapLog.LogDirContentCheckException e) { + Assert.fail("Should not throw LogDirContentCheckException."); + } catch ( FileTxnSnapLog.SnapDirContentCheckException e){ + Assert.fail("Should not throw SnapDirContentCheckException."); + } + } + + @Test + public void testDirCheckWithSingleDirSetup() throws IOException { + singleDirSetupWithCorrectFiles(); + + try { + new FileTxnSnapLog(logDir, logDir); + } catch (FileTxnSnapLog.LogDirContentCheckException e) { + Assert.fail("Should not throw LogDirContentCheckException."); + } catch ( FileTxnSnapLog.SnapDirContentCheckException e){ + Assert.fail("Should not throw SnapDirContentCheckException."); + } + } + + @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); + + new FileTxnSnapLog(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); + + new FileTxnSnapLog(logDir, snapDir); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParserTest.java b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParserTest.java new file mode 100644 index 0000000..ee4dc06 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitCliParserTest.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.persistence; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class TxnLogToolkitCliParserTest { + + private TxnLogToolkitCliParser parser; + + @Before + public void setUp() { + parser = new TxnLogToolkitCliParser(); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitParseException.class) + public void testParseWithNoArguments() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(null); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitParseException.class) + public void testParseWithEmptyArgs() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[0]); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitParseException.class) + public void testParseWith2Filenames() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "file1.log", "file2.log "}); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitParseException.class) + public void testParseWithInvalidShortSwitch() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-v", "-i", "txnlog.txt" }); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitParseException.class) + public void testParseWithInvalidLongSwitch() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-v", "--invalid", "txnlog.txt" }); + } + + @Test + public void testParseRecoveryModeSwitchShort() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-r", "txnlog.txt"}); + assertThat("Recovery short switch should turn on recovery mode", parser.isRecoveryMode(), is(true)); + } + + @Test + public void testParseRecoveryModeSwitchLong() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "--recover", "txnlog.txt"}); + assertThat("Recovery long switch should turn on recovery mode", parser.isRecoveryMode(), is(true)); + } + + @Test + public void testParseVerboseModeSwitchShort() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-v", "txnlog.txt"}); + assertThat("Verbose short switch should turn on verbose mode", parser.isVerbose(), is(true)); + } + + @Test + public void testParseVerboseModeSwitchLong() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "--verbose", "txnlog.txt"}); + assertThat("Verbose long switch should turn on verbose mode", parser.isVerbose(), is(true)); + } + + @Test + public void testParseDumpModeSwitchShort() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-r", "txnlog.txt"}); // turn on + parser.parse(new String[] { "-d", "txnlog.txt"}); // turn off + assertThat("Dump short switch should turn off recover mode", parser.isRecoveryMode(), is(false)); + } + + @Test + public void testParseDumpModeSwitchLong() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-r", "txnlog.txt"}); // turn on + parser.parse(new String[] { "--dump", "txnlog.txt"}); // turn off + assertThat("Dump long switch should turn off recovery mode", parser.isRecoveryMode(), is(false)); + } + + @Test + public void testParseForceModeSwitchShort() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "-y", "txnlog.txt"}); + assertThat("Force short switch should turn on force mode", parser.isForce(), is(true)); + } + + @Test + public void testParseForceModeSwitchLong() throws TxnLogToolkit.TxnLogToolkitParseException { + parser.parse(new String[] { "--yes", "txnlog.txt"}); + assertThat("Force long switch should turn on force mode", parser.isForce(), is(true)); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java new file mode 100644 index 0000000..da99b5c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.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.persistence; + +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +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 static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.matchers.JUnitMatchers.containsString; + + +public class TxnLogToolkitTest { + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/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/src/java/test/org/apache/zookeeper/server/quorum/CnxManagerTest.java b/src/java/test/org/apache/zookeeper/server/quorum/CnxManagerTest.java new file mode 100644 index 0000000..a82a728 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/CnxManagerTest.java @@ -0,0 +1,463 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.DataOutputStream; +import java.io.File; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.net.Socket; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumCnxManager; +import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message; +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.test.ClientBase; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class CnxManagerTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(CnxManagerTest.class); + protected static final int THRESHOLD = 4; + + int count; + HashMap 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.valueOf(i), + new QuorumServer(i, "0.0.0.0", + peerQuorumPort[i], + PortAssignment.unique(), null)); + peerTmpdir[i] = ClientBase.createTmpDir(); + } + } + + ByteBuffer createMsg(int state, long leader, long zxid, long epoch){ + return FastLeaderElection.buildMsg(state, leader, zxid, 0, epoch); + } + + 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); + 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; + return; + } + } catch (Exception e) { + LOG.error("Exception while running mock thread", e); + Assert.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); + 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(new Long(0), 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(); + } + + Assert.assertTrue("Exceeded number of retries", numRetries <= THRESHOLD); + + thread.join(5000); + if (thread.isAlive()) { + Assert.fail("Thread didn't join"); + } else { + if(thread.failed) + Assert.fail("Did not receive expected message"); + } + + } + + @Test + public void testCnxManagerTimeout() throws Exception { + Random rand = new Random(); + byte b = (byte) rand.nextInt(); + int finalOctet = b & 0xFF; + int deadPort = PortAssignment.unique(); + String deadAddress = new String("192.0.2." + finalOctet); + + LOG.info("This is the dead address I'm trying: " + deadAddress); + + peers.put(Long.valueOf(2), + new QuorumServer(2, deadAddress, deadPort, PortAssignment.unique(), null)); + peerTmpdir[2] = ClientBase.createTmpDir(); + + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 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 = System.currentTimeMillis(); + cnxManager.toSend(new Long(2), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1)); + long end = System.currentTimeMillis(); + + if((end - begin) > 6000) Assert.fail("Waited more than necessary"); + + } + + /** + * 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); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if(listener != null){ + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + + int port = peers.get(peer.getId()).electionAddr.getPort(); + LOG.info("Election port: " + port); + InetSocketAddress addr = new InetSocketAddress(port); + + Thread.sleep(1000); + + SocketChannel sc = SocketChannel.open(); + sc.socket().connect(peers.get(new Long(1)).electionAddr, 5000); + + /* + * Write id first then negative length. + */ + byte[] msgBytes = new byte[8]; + ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes); + msgBuffer.putLong(new Long(2)); + msgBuffer.position(0); + sc.write(msgBuffer); + + 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); + } + Assert.fail("Socket has not been closed"); + } catch (Exception e) { + LOG.info("Socket has been closed as expected"); + } + peer.shutdown(); + cnxManager.halt(); + } + + /* + * Class used with testCnxFromFutureVersion + */ + class TestCnxManager extends QuorumCnxManager { + + TestCnxManager(QuorumPeer self) { + super(self.getId(), self.getView(), self.authServer, + self.authLearner, self.tickTime * self.syncLimit, + self.getQuorumListenOnAllIPs(), + self.quorumCnxnThreadsSize, false); + } + + boolean senderWorkerMapContains(Long l){ + return senderWorkerMap.containsKey(l); + } + + long getSid(Message m){ + return m.sid; + } + + String getMsgString(Message m){ + return new String(m.buffer.array()); + } + } + + /** + * Before 3.5.0 a server sends its id when connecting to another server. + * Starting with 3.5.0 a server will send a protocol version, followed by + * its id, then number of bytes in the remainder of the message and finally + * the rest of the message. The test makes sure that a 3.4.6 server is able + * to detect that a connection message has this new format, extract the id, + * and skip the remainder of the message. + * + * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1633} + * + * @throws Exception + */ + @Test + public void testCnxFromFutureVersion() throws Exception { + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 20); + TestCnxManager cnxManager = new TestCnxManager(peer); + QuorumCnxManager.Listener listener = cnxManager.listener; + if(listener != null){ + listener.start(); + } else { + Assert.fail("Null listener when initializing cnx manager"); + } + + int port = peers.get(peer.getId()).electionAddr.getPort(); + LOG.info("Election port: " + port); + + Thread.sleep(1000); + + SocketChannel sc = SocketChannel.open(); + sc.socket().connect(peers.get(new Long(1)).electionAddr, 5000); + + InetSocketAddress otherAddr = peers.get(new Long(2)).electionAddr; + DataOutputStream dout = new DataOutputStream(sc.socket().getOutputStream()); + // protocol version - a negative number + dout.writeLong(0xffff0000); + // server id + dout.writeLong(new Long(2)); + // other stuff that a 3.5.0 server will send - not important for 3.4.6 + // the 3.4.6 server should just skip it + String addr = otherAddr.getHostName()+ ":" + otherAddr.getPort(); + byte[] addr_bytes = addr.getBytes(); + dout.writeInt(addr_bytes.length); + dout.write(addr_bytes); + dout.flush(); + + Thread.sleep(1000); + + Assert.assertEquals("Server 1 got connection request from server 2", + true, cnxManager.senderWorkerMapContains(new Long(2))); + + // send another message to make sure the connection message was processed + // properly (mainly that its suffix was removed from the stream) + String testStr = "this is a test message string"; + byte[] testStr_bytes = testStr.getBytes(); + dout.writeInt(testStr_bytes.length); + dout.write(testStr_bytes); + dout.flush(); + + 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){ + Assert.fail("Test message hasn't been found in recvQueue"); + } + + //Assert.assertEquals("Message sender should be 2", 2, m.sid); + Assert.assertEquals("Message sender should be 2", 2, cnxManager.getSid(m)); + Assert.assertEquals("Message from 2 doesn't match test sring", testStr, + cnxManager.getMsgString(m)); + + peer.shutdown(); + cnxManager.halt(); + } + + + /* + * 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); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if(listener != null){ + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + int port = peers.get(peer.getId()).electionAddr.getPort(); + LOG.info("Election port: " + port); + InetSocketAddress addr = new InetSocketAddress(port); + Thread.sleep(1000); + + Socket sock = new Socket(); + sock.connect(peers.get(new Long(1)).electionAddr, 5000); + long begin = System.currentTimeMillis(); + // Read without sending data. Verify timeout. + cnxManager.receiveConnection(sock); + long end = System.currentTimeMillis(); + if((end - begin) > ((peer.getSyncLimit() * peer.getTickTime()) + 500)) Assert.fail("Waited more than necessary"); + } + + /* + * 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); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + } + String failure = verifyThreadCount(peerList, 4); + if (failure != null) { + Assert.fail(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 {}", new Object[] { i, + peer.getId() }); + peer.shutdown(); + peerList.remove(myid); + failure = verifyThreadCount(peerList, 2); + Assert.assertNull(failure, failure); + // Restart halted node and verify count + peer = new QuorumPeer(peers, peerTmpdir[myid], peerTmpdir[myid], + peerClientPort[myid], 3, myid, 1000, 2, 2); + LOG.info("Round {}, restarting peer {}" + + new Object[] { i, peer.getId() }); + peer.start(); + peerList.add(myid, peer); + failure = verifyThreadCount(peerList, 4); + Assert.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 String(new Date() + + " Incorrect number of Worker threads for sid=" + myid + + " expected " + ecnt + " found " + cnt); + } + } + return null; + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.java b/src/java/test/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.java new file mode 100644 index 0000000..c34f243 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.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.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import java.net.SocketTimeoutException; + +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.Assert; +import org.junit.Test; + +import javax.security.sasl.SaslException; + +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(); + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + System.out.println(currentQuorumCfgSection); + // start all the servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection) { + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + 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)); + } + + 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 + */ + + // 1: create ephemeral node + String nodePath = "/e1"; + zk.create(nodePath, "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + // 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); + + Assert.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); + Assert.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 + zk.create(nodePath, "2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + // close the session and newly created ephemeral node should be deleted + zk.close(); + + 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, null /*DataTreeBuilder is never used*/, + 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(); + } + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.java b/src/java/test/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.java new file mode 100644 index 0000000..2d3ad5c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.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.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumCnxManager; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.Vote; +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; + + +public class FLEBackwardElectionRoundTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(FLELostMessageTest.class); + + int count; + HashMap 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. + * + * {@link 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, "0.0.0.0", clientport, + PortAssignment.unique(), null)); + 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); + 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); + cnxManagers[0] = mockPeer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManagers[0].listener; + 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); + cnxManagers[1] = mockPeer.createCnxnManager(); + listener = cnxManagers[1].listener; + 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()) { + Assert.fail("Should not have joined"); + } + + } + + private ByteBuffer getMsg() { + return FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), 0, 0, 1); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/FLECompatibilityTest.java b/src/java/test/org/apache/zookeeper/server/quorum/FLECompatibilityTest.java new file mode 100644 index 0000000..f1c04ca --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/FLECompatibilityTest.java @@ -0,0 +1,350 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.FastLeaderElection.Notification; +import org.apache.zookeeper.server.quorum.FastLeaderElection.ToSend; +import org.apache.zookeeper.server.quorum.FastLeaderElection.Messenger.WorkerReceiver; +import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message; +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.apache.zookeeper.test.FLETest; +import org.apache.zookeeper.test.QuorumBase; + +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 FLECompatibilityTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(FLECompatibilityTest.class); + + int count; + HashMap peers; + File tmpdir[]; + int port[]; + + @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 { + + } + + class MockFLEMessengerBackward { + QuorumCnxManager manager; + QuorumPeer self; + long logicalclock = 1L; + LinkedBlockingQueue sendqueue = new LinkedBlockingQueue(); + LinkedBlockingQueue internalqueue = new LinkedBlockingQueue(); + LinkedBlockingQueue recvqueue = new LinkedBlockingQueue(); + WorkerReceiver wr; + + MockFLEMessengerBackward(QuorumPeer self, QuorumCnxManager manager){ + this.manager = manager; + this.self = self; + + this.wr = new WorkerReceiver(manager); + + Thread t = new Thread(this.wr, + "WorkerReceiver[myid=" + self.getId() + "]"); + t.setDaemon(true); + t.start(); + } + + void halt() { + wr.stop = true; + } + + /* + * This class has been copied from before adding versions to notifications. + * + * {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1808} + */ + class WorkerReceiver implements Runnable { + volatile boolean stop; + QuorumCnxManager manager; + final long proposedLeader = 2; + final long proposedZxid = 0x1; + final long proposedEpoch = 1; + + WorkerReceiver(QuorumCnxManager manager) { + this.stop = false; + this.manager = manager; + } + + /* + * The vote we return here is fixed for test purposes. + */ + Vote getVote(){ + return new Vote(proposedLeader, proposedZxid, proposedEpoch); + } + + public void run() { + + Message response; + while (!stop) { + // Sleeps on receive + try{ + response = manager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if(response == null) continue; + + /* + * If it is from an observer, respond right away. + * Note that the following predicate assumes that + * if a server is not a follower, then it must be + * an observer. If we ever have any other type of + * learner in the future, we'll have to change the + * way we check for observers. + */ + if(!self.getVotingView().containsKey(response.sid)){ + Vote current = self.getCurrentVote(); + ToSend notmsg = new ToSend(ToSend.mType.notification, + current.getId(), + current.getZxid(), + logicalclock, + self.getPeerState(), + response.sid, + current.getPeerEpoch()); + + internalqueue.offer(notmsg); + } else { + // Receive new message + if (LOG.isDebugEnabled()) { + LOG.debug("Receive new notification message. My id = " + + self.getId()); + } + + /* + * We check for 28 bytes for backward compatibility + */ + if (response.buffer.capacity() < 28) { + LOG.error("Got a short response: " + + response.buffer.capacity()); + continue; + } + boolean backCompatibility = (response.buffer.capacity() == 28); + response.buffer.clear(); + + // State of peer that sent this message + QuorumPeer.ServerState ackstate = QuorumPeer.ServerState.LOOKING; + switch (response.buffer.getInt()) { + 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; + } + + // Instantiate Notification and set its attributes + Notification n = new Notification(); + n.leader = response.buffer.getLong(); + n.zxid = response.buffer.getLong(); + n.electionEpoch = response.buffer.getLong(); + n.state = ackstate; + n.sid = response.sid; + if(!backCompatibility){ + n.peerEpoch = response.buffer.getLong(); + } else { + if(LOG.isInfoEnabled()){ + LOG.info("Backward compatibility mode, server id=" + n.sid); + } + n.peerEpoch = ZxidUtils.getEpochFromZxid(n.zxid); + } + + /* + * If this server is looking, then send proposed leader + */ + + if(self.getPeerState() == QuorumPeer.ServerState.LOOKING){ + 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)){ + Vote v = getVote(); + ToSend notmsg = new ToSend(ToSend.mType.notification, + v.getId(), + v.getZxid(), + logicalclock, + self.getPeerState(), + response.sid, + v.getPeerEpoch()); + internalqueue.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(LOG.isDebugEnabled()){ + LOG.debug("Sending new notification. My id = " + + self.getId() + " recipient=" + + response.sid + " zxid=0x" + + Long.toHexString(current.getZxid()) + + " leader=" + current.getId()); + } + ToSend notmsg = new ToSend( + ToSend.mType.notification, + current.getId(), + current.getZxid(), + current.getElectionEpoch(), + self.getPeerState(), + response.sid, + current.getPeerEpoch()); + internalqueue.offer(notmsg); + } + } + } + } catch (InterruptedException e) { + System.out.println("Interrupted Exception while waiting for new message" + + e.toString()); + } + } + LOG.info("WorkerReceiver is down"); + } + } + } + + class MockFLEMessengerForward extends FastLeaderElection { + + MockFLEMessengerForward(QuorumPeer self, QuorumCnxManager manager){ + super( self, manager ); + } + + void halt() { + super.shutdown(); + } + } + + void populate() + throws Exception { + for (int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", + PortAssignment.unique(), + PortAssignment.unique(), null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + } + + @Test(timeout=20000) + public void testBackwardCompatibility() + throws Exception { + populate(); + + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2); + peer.setPeerState(ServerState.LOOKING); + QuorumCnxManager mng = peer.createCnxnManager(); + + /* + * Check that it generates an internal notification correctly + */ + MockFLEMessengerBackward fle = new MockFLEMessengerBackward(peer, mng); + ByteBuffer buffer = FastLeaderElection.buildMsg(ServerState.LOOKING.ordinal(), 2, 0x1, 1, 1); + fle.manager.recvQueue.add(new Message(buffer, 2)); + Notification n = fle.recvqueue.take(); + Assert.assertTrue("Wrong state", n.state == ServerState.LOOKING); + Assert.assertTrue("Wrong leader", n.leader == 2); + Assert.assertTrue("Wrong zxid", n.zxid == 0x1); + Assert.assertTrue("Wrong epoch", n.electionEpoch == 1); + Assert.assertTrue("Wrong epoch", n.peerEpoch == 1); + + /* + * Check that it sends a notification back to the sender + */ + peer.setPeerState(ServerState.FOLLOWING); + peer.setCurrentVote( new Vote(2, 0x1, 1, 1, ServerState.LOOKING) ); + buffer = FastLeaderElection.buildMsg(ServerState.LOOKING.ordinal(), 1, 0x1, 1, 1); + fle.manager.recvQueue.add(new Message(buffer, 1)); + ToSend m = fle.internalqueue.take(); + Assert.assertTrue("Wrong state", m.state == ServerState.FOLLOWING); + Assert.assertTrue("Wrong sid", m.sid == 1); + Assert.assertTrue("Wrong leader", m.leader == 2); + Assert.assertTrue("Wrong epoch", m.electionEpoch == 1); + Assert.assertTrue("Wrong epoch", m.peerEpoch == 1); + } + + @Test(timeout=20000) + public void testForwardCompatibility() + throws Exception { + populate(); + + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2); + peer.setPeerState(ServerState.LOOKING); + QuorumCnxManager mng = peer.createCnxnManager(); + + /* + * Check that it generates an internal notification correctly + */ + MockFLEMessengerForward fle = new MockFLEMessengerForward(peer, mng); + ByteBuffer notBuffer = FastLeaderElection.buildMsg(ServerState.LOOKING.ordinal(), 2, 0x1, 1, 1); + ByteBuffer buffer = ByteBuffer.allocate( notBuffer.capacity() + 8 ); + notBuffer.flip(); + buffer.put(notBuffer); + buffer.putLong( Long.MAX_VALUE ); + buffer.flip(); + + fle.manager.recvQueue.add(new Message(buffer, 2)); + Notification n = fle.recvqueue.take(); + Assert.assertTrue("Wrong state", n.state == ServerState.LOOKING); + Assert.assertTrue("Wrong leader", n.leader == 2); + Assert.assertTrue("Wrong zxid", n.zxid == 0x1); + Assert.assertTrue("Wrong epoch", n.electionEpoch == 1); + Assert.assertTrue("Wrong epoch", n.peerEpoch == 1); + Assert.assertTrue("Wrong version", n.version == FastLeaderElection.Notification.CURRENTVERSION); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/FLEDontCareTest.java b/src/java/test/org/apache/zookeeper/server/quorum/FLEDontCareTest.java new file mode 100644 index 0000000..ffc7ab1 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/FLEDontCareTest.java @@ -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. + */ + +package org.apache.zookeeper.server.quorum; + +import java.io.File; +import java.net.InetSocketAddress; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.Vote; +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.apache.zookeeper.test.FLETest; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class FLEDontCareTest { + protected static final Logger LOG = LoggerFactory.getLogger(FLEDontCareTest.class); + + class MockFLE extends FastLeaderElection { + MockFLE(QuorumPeer peer, QuorumCnxManager cnxManager) { + super(peer, cnxManager); + } + + public boolean termPredicate(HashMap votes, Vote vote) { + return super.termPredicate(votes, vote); + } + + public boolean checkLeader(HashMap votes, long leader, long electionEpoch) { + return super.checkLeader(votes, leader, electionEpoch); + } + + public boolean ooePredicate(HashMap recv, + HashMap ooe, + FastLeaderElection.Notification n) { + return super.ooePredicate(recv, ooe, n); + + } + } + + HashMap peers; + QuorumPeer peer; + File tmpdir; + + @Before + public void setUp() + throws Exception { + tmpdir = ClientBase.createTmpDir(); + peers = new HashMap(); + for(int i = 0; i < 5; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(Long.valueOf(i), "127.0.0.1", PortAssignment.unique(), 0, null)); + } + peer = new QuorumPeer(peers, + tmpdir, + tmpdir, + PortAssignment.unique(), + 3, 3, 1000, 2, 2); + } + + @After + public void tearDown(){ + tmpdir.delete(); + } + + @Test + public void testDontCare() { + MockFLE fle = new MockFLE(peer, peer.createCnxnManager()); + + HashMap 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)); + + Assert.assertTrue(fle.termPredicate(votes, + new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING))); + } + + @Test + public void testDontCareVersion() { + MockFLE fle = new MockFLE(peer, peer.createCnxnManager()); + + HashMap 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)); + + Assert.assertTrue(fle.termPredicate(votes, + new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING))); + } + + @Test + public void testLookingNormal() { + MockFLE fle = new MockFLE(peer, peer.createCnxnManager()); + + HashMap 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)); + + Assert.assertTrue(fle.termPredicate(votes, + new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING))); + } + + @Test + public void testLookingDiffRounds() { + MockFLE fle = new MockFLE(peer, peer.createCnxnManager()); + + 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)); + + Assert.assertFalse(fle.termPredicate(votes, + new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING))); + } + + + /** + * Helper method to build notifications and populate outofelection. + * + * + * @param version + * @param leader + * @param zxid + * @param electionEpoch + * @param state + * @param sid + * @param peerEpoch + * @param outofelection + * @return + */ + FastLeaderElection.Notification genNotification(int version, + long leader, + long zxid, + long electionEpoch, + ServerState state, + long sid, + long peerEpoch, + HashMap outofelection) { + FastLeaderElection.Notification n = new FastLeaderElection.Notification(); + n.version = version; + n.leader = leader; + n.zxid = zxid; + n.electionEpoch = electionEpoch; + n.state = state; + n.sid = sid; + n.peerEpoch = peerEpoch; + + outofelection.put(n.sid, new Vote(n.version, + n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch, + n.state)); + + return n; + } + + @Test + public void testOutofElection() { + MockFLE fle = new MockFLE(peer, peer.createCnxnManager()); + HashMap outofelection = new HashMap(); + + /* + * Generates notifications emulating servers 1, 2, 4, and 5. + * Server 5 is the elected leader. + */ + + genNotification( 0x0, + 5, + ZxidUtils.makeZxid(15, 0), + 0xa, + ServerState.FOLLOWING, + 1, + 0x17, + outofelection); + + genNotification( 0x0, + 5, + ZxidUtils.makeZxid(15, 0), + 0xa, + ServerState.FOLLOWING, + 2, + 0x17, + outofelection); + + genNotification( 0x1, + 5, + ZxidUtils.makeZxid(15, 0), + 0xa, + ServerState.FOLLOWING, + 4, + 0x18, + outofelection); + + FastLeaderElection.Notification n = genNotification( 0x1, + 5, + ZxidUtils.makeZxid(15, 0), + 0xa, + ServerState.LEADING, + 5, + 0x18, + outofelection); + + /* + * fle represents the FLE instance of server 3.Here we set + * its logical clock to 1. + */ + fle.logicalclock.set(0x1); + + + /* + * Here we test the predicates we use in FLE. + */ + Assert.assertTrue("Termination predicate failed", + fle.termPredicate(outofelection, + new Vote(n.version, + n.leader, + n.zxid, + n.electionEpoch, + n.peerEpoch, + n.state))); + Assert.assertTrue("Leader check failed", + fle.checkLeader(outofelection, + n.leader, + n.electionEpoch)); + + Assert.assertTrue("Out of election predicate failed", + fle.ooePredicate( outofelection, outofelection, n )); + + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/FLELostMessageTest.java b/src/java/test/org/apache/zookeeper/server/quorum/FLELostMessageTest.java new file mode 100644 index 0000000..190785c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/FLELostMessageTest.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.quorum; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumCnxManager; +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.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +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 { + FastLeaderElection le[] = new FastLeaderElection[count]; + + LOG.info("TestLE: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + int clientport = PortAssignment.unique(); + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", clientport, + PortAssignment.unique(), null)); + 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); + peer.startLeaderElection(); + FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 1); + thread.start(); + + /* + * Start mock server 1 + */ + mockServer(); + thread.join(5000); + if (thread.isAlive()) { + Assert.fail("Threads didn't join"); + } + } + + void mockServer() throws InterruptedException, IOException { + /* + * Create an instance of the connection manager + */ + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2); + cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + 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/src/java/test/org/apache/zookeeper/server/quorum/FLETestUtils.java b/src/java/test/org/apache/zookeeper/server/quorum/FLETestUtils.java new file mode 100644 index 0000000..1f0bca3 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/FLETestUtils.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.quorum; + +import java.nio.ByteBuffer; + +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.Vote; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.junit.Assert; + +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; + +public class FLETestUtils { + 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){ + Assert.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()); + + Assert.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); + } + +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/quorum/LeaderBeanTest.java b/src/java/test/org/apache/zookeeper/server/quorum/LeaderBeanTest.java new file mode 100644 index 0000000..0b47952 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/LeaderBeanTest.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.quorum; + +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +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.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; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +public class LeaderBeanTest { + private Leader leader; + private LeaderBean leaderBean; + private FileTxnSnapLog fileTxnSnapLog; + private LeaderZooKeeperServer zks; + private QuorumPeer qp; + + @Before + public void setUp() throws IOException { + qp = new QuorumPeer(); + QuorumVerifier quorumVerifierMock = mock(QuorumVerifier.class); + qp.setQuorumVerifier(quorumVerifierMock); + File tmpDir = ClientBase.createTmpDir(); + fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), + new File(tmpDir, "data_txnlog")); + ZKDatabase zkDb = new ZKDatabase(fileTxnSnapLog); + + zks = new LeaderZooKeeperServer(fileTxnSnapLog, qp, null, 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); + } +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.java b/src/java/test/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.java new file mode 100644 index 0000000..0f6a098 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.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.quorum; + +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; + +import static org.apache.zookeeper.server.quorum.ZabUtils.createLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer; + +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; + observerId = peer.quorumPeers.size(); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + peer.quorumPeers.put(observerId, new QuorumPeer.QuorumServer(observerId, "127.0.0.1", PortAssignment.unique(), + 0, 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); + + Assert.assertEquals("Unexpected vote in connectingFollowers", 0, leader.connectingFollowers.size()); + Assert.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 + } + + Assert.assertEquals("Unexpected vote in connectingFollowers", 1, leader.connectingFollowers.size()); + Assert.assertEquals("Leader shouldn't set new epoch until quorum of participants is in connectingFollowers", + lastAcceptedEpoch, peer.getAcceptedEpoch()); + Assert.assertTrue(leader.waitingForNewEpoch); + try { + // Observer asks for epoch (mocking LearnerHandler behavior) + leader.getEpochToPropose(observerId, lastAcceptedEpoch); + } catch (InterruptedException e) { + // ignore timeout + } + + Assert.assertEquals("Unexpected vote in connectingFollowers", 1, leader.connectingFollowers.size()); + Assert.assertEquals("Leader shouldn't set new epoch after observer asks for epoch", + lastAcceptedEpoch, peer.getAcceptedEpoch()); + Assert.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) { + Assert.fail("Timed out in getEpochToPropose"); + } + + Assert.assertEquals("Unexpected vote in connectingFollowers", 2, leader.connectingFollowers.size()); + Assert.assertEquals("Leader should record next epoch", lastAcceptedEpoch + 1, peer.getAcceptedEpoch()); + Assert.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.readyToStart = true; + leader.leaderStateSummary = new StateSummary(leader.self.getCurrentEpoch(), leader.zk.getLastProcessedZxid()); + + Assert.assertEquals("Unexpected vote in electingFollowers", 0, leader.electingFollowers.size()); + Assert.assertFalse(leader.electionFinished); + try { + // leader calls waitForEpochAck, first add to electingFollowers + leader.waitForEpochAck(peer.getId(), new StateSummary(0, 0)); + } catch (InterruptedException e) { + // ignore timeout + } + + Assert.assertEquals("Unexpected vote in electingFollowers", 1, leader.electingFollowers.size()); + Assert.assertFalse(leader.electionFinished); + try { + // observer calls waitForEpochAck, should fail verifier.containsQuorum + leader.waitForEpochAck(observerId, new StateSummary(0, 0)); + } catch (InterruptedException e) { + // ignore timeout + } + + Assert.assertEquals("Unexpected vote in electingFollowers", 1, leader.electingFollowers.size()); + Assert.assertFalse(leader.electionFinished); + try { + // second add to electingFollowers, verifier.containsQuorum=true, waitForEpochAck returns without exceptions + leader.waitForEpochAck(participantId, new StateSummary(0, 0)); + Assert.assertEquals("Unexpected vote in electingFollowers", 2, leader.electingFollowers.size()); + Assert.assertTrue(leader.electionFinished); + } catch (Exception e) { + Assert.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); + + Assert.assertEquals("Unexpected vote in ackSet", 0, leader.newLeaderProposal.ackSet.size()); + Assert.assertFalse(leader.quorumFormed); + try { + // leader calls waitForNewLeaderAck, first add to ackSet + leader.waitForNewLeaderAck(peer.getId(), zxid); + } catch (InterruptedException e) { + // ignore timeout + } + + Assert.assertEquals("Unexpected vote in ackSet", 1, leader.newLeaderProposal.ackSet.size()); + Assert.assertFalse(leader.quorumFormed); + try { + // observer calls waitForNewLeaderAck, should fail verifier.containsQuorum + leader.waitForNewLeaderAck(observerId, zxid); + } catch (InterruptedException e) { + // ignore timeout + } + + Assert.assertEquals("Unexpected vote in ackSet", 1, leader.newLeaderProposal.ackSet.size()); + Assert.assertFalse(leader.quorumFormed); + try { + // second add to ackSet, verifier.containsQuorum=true, waitForNewLeaderAck returns without exceptions + leader.waitForNewLeaderAck(participantId, zxid); + Assert.assertEquals("Unexpected vote in ackSet", 2, leader.newLeaderProposal.ackSet.size()); + Assert.assertTrue(leader.quorumFormed); + } catch (Exception e) { + Assert.fail("Timed out in waitForEpochAck"); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/LearnerTest.java b/src/java/test/org/apache/zookeeper/server/quorum/LearnerTest.java new file mode 100644 index 0000000..fd08d21 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/LearnerTest.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.quorum; + +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.Socket; +import java.util.ArrayList; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Index; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +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.Learner; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Assert; +import org.junit.Test; + +public class LearnerTest extends ZKTestCase { + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/data")); + + class SimpleLearnerZooKeeperServer extends LearnerZooKeeperServer { + boolean startupCalled; + + public SimpleLearnerZooKeeperServer(FileTxnSnapLog ftsl, QuorumPeer self) throws IOException { + super(ftsl, 2000, 2000, 2000, null, new ZKDatabase(ftsl), self); + } + Learner learner; + @Override + public Learner getLearner() { + return learner; + } + + @Override + public void startup() { + startupCalled = true; + } + } + class SimpleLearner extends Learner { + SimpleLearner(FileTxnSnapLog ftsl) throws IOException { + self = QuorumPeer.testingQuorumPeer(); + zk = new SimpleLearnerZooKeeperServer(ftsl, self); + ((SimpleLearnerZooKeeperServer)zk).learner = this; + } + } + static private void recursiveDelete(File dir) { + if (dir == null || !dir.exists()) { + return; + } + if (!dir.isDirectory()) { + dir.delete(); + } + for(File child: dir.listFiles()) { + recursiveDelete(child); + } + } + @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); + Assert.assertEquals(startZxid, sl.zk.getLastProcessedZxid()); + } finally { + recursiveDelete(tmpFile); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/ProposalStatsTest.java b/src/java/test/org/apache/zookeeper/server/quorum/ProposalStatsTest.java new file mode 100644 index 0000000..1f71979 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/ProposalStatsTest.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.quorum; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ProposalStatsTest { + @Test + public void testSetProposalSizeSetMinMax() { + ProposalStats stats = new ProposalStats(); + assertEquals(-1, stats.getLastProposalSize()); + assertEquals(-1, stats.getMinProposalSize()); + assertEquals(-1, stats.getMaxProposalSize()); + stats.setLastProposalSize(10); + assertEquals(10, stats.getLastProposalSize()); + assertEquals(10, stats.getMinProposalSize()); + assertEquals(10, stats.getMaxProposalSize()); + stats.setLastProposalSize(20); + assertEquals(20, stats.getLastProposalSize()); + assertEquals(10, stats.getMinProposalSize()); + assertEquals(20, stats.getMaxProposalSize()); + stats.setLastProposalSize(5); + assertEquals(5, stats.getLastProposalSize()); + assertEquals(5, stats.getMinProposalSize()); + assertEquals(20, stats.getMaxProposalSize()); + } + + @Test + public void testReset() { + ProposalStats stats = new ProposalStats(); + stats.setLastProposalSize(10); + assertEquals(10, stats.getLastProposalSize()); + assertEquals(10, stats.getMinProposalSize()); + assertEquals(10, stats.getMaxProposalSize()); + stats.reset(); + assertEquals(-1, stats.getLastProposalSize()); + assertEquals(-1, stats.getMinProposalSize()); + assertEquals(-1, stats.getMaxProposalSize()); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java b/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java new file mode 100644 index 0000000..9917cfc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java @@ -0,0 +1,967 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Field; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import javax.security.sasl.SaslException; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +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.QuorumPeer.QuorumServer; +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.test.ClientBase; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; + +public class QuorumCnxManagerTest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(QuorumCnxManagerTest.class); + private int count; + private HashMap peers; + private int peerQuorumPort[]; + private int peerClientPort[]; + private ThreadPoolExecutor executor; + /** + * The maximum number of threads to allow in the connectionExecutors thread + * pool which will be used to initiate quorum server connections. Defaulting to 20. + * TODO: Need to tune this param. + */ + private final int quorumCnxnThreadsSize = 20; + private Set authzHosts; + + private static File saslConfigFile = null; + + @BeforeClass + public static void setupSasl() throws Exception { + String jaasEntries = new String("" + + "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"); + + saslConfigFile = File.createTempFile("jaas.", ".conf"); + FileWriter fwriter = new FileWriter(saslConfigFile); + fwriter.write(jaasEntries); + fwriter.close(); + System.setProperty("java.security.auth.login.config", + saslConfigFile.getAbsolutePath()); + } + + @AfterClass + public static void cleanupSasl() throws Exception { + if (saslConfigFile != null) { + saslConfigFile.delete(); + } + } + + @Before + public void setUp() throws Exception { + this.count = 3; + this.peers = new HashMap(count); + peerQuorumPort = new int[count]; + peerClientPort = new int[count]; + authzHosts = new HashSet(); + for(int i = 0; i < count; i++) { + peerQuorumPort[i] = PortAssignment.unique(); + peerClientPort[i] = PortAssignment.unique(); + QuorumServer qs = new QuorumServer(i, "0.0.0.0", + peerQuorumPort[i], PortAssignment.unique(), null); + peers.put(Long.valueOf(i), qs); + authzHosts.add(qs.hostname); + } + executor = new ThreadPoolExecutor(3, 10, + 60, TimeUnit.SECONDS, new SynchronousQueue()); + } + + @After + public void tearDown() throws Exception { + if (executor != null) { + executor.shutdownNow(); + } + } + + @Test(timeout = 30000) + public void testNoAuthConnection() throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0); + QuorumCnxManager peer1 = createAndStartManager(1); + + peer0.connectOne(1); + assertEventuallyConnected(peer0, 1); + } + + @Test(timeout = 30000) + public void testAuthConnection() throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearner", true, true); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearner", true, true); + peer0.connectOne(1); + assertEventuallyConnected(peer0, 1); + } + + /** + * Peer0 has no auth configured, Peer1 has auth configured. + * Peer1 connects to peer0, because null auth server sees an auth packet and connection succeeds. + * Peer0 connects to peer1, but connection isn't initiated because + * peer0's sid is lower than peer1's + */ + @Test(timeout = 30000) + public void testClientAuthAgainstNoAuthServerWithLowerSid() + throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearner", false, false); + peer1.connectOne(0); + peer0.connectOne(1); + assertEventuallyConnected(peer0, 1); + } + + /** + * Peer0 has auth configured, Peer1 has no auth configured. + * Peer0 connects to peer1, but is disconnected, because peer1's sid is + * higher than peer0. + * Peer1 connects to peer0, but is disconnected, because peer1 cannot + * handle auth. + */ + @Test(timeout = 30000) + public void testClientAuthAgainstNoAuthServerWithHigherSid() + throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearner", false, false); + QuorumCnxManager peer1 = createAndStartManager(1); + peer0.connectOne(1); + peer1.connectOne(0); + assertEventuallyConnected(peer0, 1); + } + + /** + * No auth learner connects to a server that requires auth, when the server + * has a higher sid. + * The connection should fail in both directions. + */ + @Test(timeout = 30000) + public void testNoAuthLearnerConnectToAuthRequiredServerWithLowerSid() + throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearner", true, true); + QuorumCnxManager peer1 = createAndStartManager(1); + peer0.connectOne(1); + peer1.connectOne(0); + assertEventuallyNotConnected(peer0, 1); + } + + /** + * No auth learner connects to a server that requires auth, when the server + * has a higher sid. + * The connection should fail in both directions. + * + * peer0 should attempt to connect to peer1, but disconnect as its sid is lower + * peer1 should attempt to connect to peer0, peer0 will accept and add an entry to + * the senderWorkerMap but peer1 will disconnect because peer1 will start speaking SASL + * and peer0 will consider this invalid. + * + * Due to the unique behavior of peer0 creating an entry + * in senderWorkerMap for peer1 and then deleting it we use mockito spies to track + * this behavior. + */ + @Test(timeout = 30000) + public void testNoAuthLearnerConnectToAuthRequiredServerWithHigherSid() + throws Exception { + ConcurrentHashMap senderWorkerMap0 = + spy(new ConcurrentHashMap()); + ConcurrentHashMap senderWorkerMap1 = + spy(new ConcurrentHashMap()); + + QuorumCnxManager peer0 = createAndStartManager(0, senderWorkerMap0); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", "QuorumLearner", + true, true, senderWorkerMap1); + peer0.connectOne(1); + peer1.connectOne(0); + + verify(senderWorkerMap0, timeout(10000)).put(eq(1L), any(QuorumCnxManager.SendWorker.class)); + verify(senderWorkerMap0, timeout(10000)).remove(eq(1L), any(QuorumCnxManager.SendWorker.class)); + + verify(senderWorkerMap1, never()).put(anyLong(), any(QuorumCnxManager.SendWorker.class)); + } + + /** + * An auth learner connects to a auth server, but the credentials are bad. + * The peer with the higher sid has the bad credentials. + * The connection will be denied. + */ + @Test(timeout = 30000) + public void testAuthLearnerBadCredToAuthRequiredServerWithLowerSid() + throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearner", true, true); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearnerInvalid", true, true); + peer0.connectOne(1); + peer1.connectOne(0); + + assertEventuallyNotConnected(peer0, 1); + } + + /** + * An auth learner connects to a auth server, but the credentials are bad. + * The peer with the lower sid has the bad credentials. + * The connection will work, because peer1 is connecting to peer0. + */ + @Test(timeout = 30000) + public void testAuthLearnerBadCredToAuthRequiredServerWithHigherSid() + throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearnerInvalid", true, true); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearner", true, true); + peer0.connectOne(1); + peer1.connectOne(0); + assertEventuallyConnected(peer0, 1); + assertEventuallyConnected(peer1, 0); + } + + /** + * An auth learner connects to a auth server, but the credentials are bad. + * The connection should fail in both directions. + */ + @Test(timeout = 30000) + public void testAuthLearnerBadCredToNoAuthServerWithHigherSid() throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearner", false, false); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearnerInvalid", true, true); + peer1.connectOne(0); + assertEventuallyNotConnected(peer1, 0); + } + + /** + * An auth learner connects to a auth server, but the credentials are bad. + * The peer with the lower sid has the bad credentials. + * The connection will work, because peer0 is connecting to peer1 and peer1 + * server doesn't require sasl + */ + @Test(timeout = 30000) + public void testAuthLearnerBadCredToNoAuthServerWithLowerSid() throws Exception { + QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer", + "QuorumLearnerInvalid", true, true); + QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer", + "QuorumLearner", false, true); + peer0.connectOne(1); + assertEventuallyConnected(peer0, 1); + assertEventuallyConnected(peer1, 0); + } + + /** + * Test verifies that the LearnerHandler should authenticate the connecting + * quorumpeer. Here its simulating authentication failure and it should throw + * SaslException + */ + @Test(timeout = 30000) + public void testLearnerHandlerAuthFailed() throws Exception { + File testData = ClientBase.createTmpDir(); + Socket leaderSocket = getSocketPair(); + File tmpDir = File.createTempFile("test", ".dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, true, false, true, + "QuorumLearner", "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + + // authentication failed as qpserver didn't get auth packet from qpclient. + try { + new LearnerHandler(leaderSocket, + new BufferedInputStream(leaderSocket.getInputStream()), leader); + Assert.fail("Must throw exception as there is an authentication failure"); + } catch (SaslException e){ + Assert.assertEquals("Mistakely added to learners", 0, + leader.getLearners().size()); + } + ClientBase.recursiveDelete(testData); + } + + /** + * Test verifies that the Leader should authenticate the connecting learner + * quorumpeer. After the successful authentication it should add this + * learner to the learnerHandler list. + */ + @Test(timeout = 30000) + public void testAuthLearnerConnectsToServerWithAuthRequired() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true, + "QuorumLearner", "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, true, true, true, "QuorumLearner", + "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + // wait till leader socket soTimeout period + Assert.assertTrue("Leader should accept the auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000, + TimeUnit.MILLISECONDS)); + Assert.assertEquals("Failed to added the learner", 1, + leader.getLearners().size()); + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + private String getLeaderHostname(QuorumPeer peer) { + String hostname = null; + for (QuorumServer p : peer.getView().values()) { + if (p.id == peer.getId()) { + hostname = p.hostname; + break; + } + } + Assert.assertNotNull("Didn't find leader", hostname); + return hostname; + } + + /** + * Test verifies that the Leader should authenticate the connecting learner + * quorumpeer. After the successful authentication it should add this + * learner to the learnerHandler list. + */ + @Test(timeout = 30000) + public void testAuthLearnerConnectsToServerWithAuthNotRequired() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true, + "QuorumLearner", "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, true, true, false, "QuorumLearner", + "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + // wait till leader socket soTimeout period + Assert.assertTrue("Leader should accept the auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000, + TimeUnit.MILLISECONDS)); + Assert.assertEquals("Failed to added the learner", 1, + leader.getLearners().size()); + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + private void startLearnerCnxAcceptorThread(Leader leader) + throws InterruptedException { + final CountDownLatch cnxAcceptorWatcher = new CountDownLatch(1); + leader.cnxAcceptor = leader.new LearnerCnxAcceptor(){ + @Override + public void run() { + cnxAcceptorWatcher.countDown(); + super.run(); + } + }; + leader.cnxAcceptor.start(); + // waiting to start the thread + Assert.assertTrue("Failed to start leader.cnxAcceptor thread!", + cnxAcceptorWatcher.await(15, TimeUnit.SECONDS)); + LOG.info("Started leader.cnxAcceptor:{} thread, state:{}", + leader.cnxAcceptor.getName(), leader.cnxAcceptor.getState()); + } + + /** + * Test verifies that the Auth enabled Learner is connecting to a Null Auth + * Leader server. Learner is failing to get an auth response from Null Auth + * Leader and fails the connection establishment. + */ + @Test(timeout = 30000) + public void testAuthLearnerConnectsToNullAuthServer() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true, + "QuorumLearner", "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, false, false, false, + "QuorumLearner", "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + + try { + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + Assert.fail("Must throw exception as server doesn't supports authentication"); + } catch (IOException e) { + // expected + Assert.assertTrue("Leader should accept the auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 500, + TimeUnit.MILLISECONDS)); + } + + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + /** + * Test verifies that the No Auth enabled Learner is connecting to a No Auth + * Leader server. Learner should be able to establish a connection with + * Leader as auth is not required. + */ + @Test(timeout = 30000) + public void testNoAuthLearnerConnectsToServerWithAuthNotRequired() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false, + "QuorumLearner", "QuorumServer", ""); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, true, false, false, "QuorumLearner", + "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + + Assert.assertTrue("Leader should accept no auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000, + TimeUnit.MILLISECONDS)); + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + /** + * Test verifies that the No Auth enabled Learner is connecting to a No Auth + * Leader server. Learner shouldn't be able to establish a connection with + * Leader as auth as auth is required. + */ + @Test(timeout = 30000) + public void testNoAuthLearnerConnectsToServerWithAuthRequired() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false, + "QuorumLearner", "QuorumServer", ""); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, true, true, true, "QuorumLearner", + "QuorumServer", + QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + Assert.assertFalse("Leader shouldn't accept no auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000, + TimeUnit.MILLISECONDS)); + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + /** + * Test verifies that the No Auth enabled Learner is connecting to a No Auth + * Leader server. Learner should be able to establish a connection with + * Leader as auth is not required. + */ + @Test(timeout = 30000) + public void testNoAuthLearnerConnectsToNullAuthServer() + throws Exception { + File testDataLearner = ClientBase.createTmpDir(); + File tmpDir = File.createTempFile("test", ".dir", testDataLearner); + tmpDir.delete(); + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir); + QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false, + "QuorumLearner", "QuorumServer", ""); + SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer); + + File testDataLeader = ClientBase.createTmpDir(); + tmpDir = File.createTempFile("test", ".dir", testDataLeader); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + QuorumPeer peer = createQuorumPeer(tmpDir, false, false, false, "", "", + ""); + CountDownLatch learnerLatch = new CountDownLatch(1); + leader = createSimpleLeader(tmpDir, peer, learnerLatch); + peer.leader = leader; + + startLearnerCnxAcceptorThread(leader); + LOG.info("Start establishing a connection with the Leader"); + String hostname = getLeaderHostname(peer); + sl.connectToLeader(peer.getQuorumAddress(), hostname); + + Assert.assertTrue("Leader should accept no auth learner connection", + learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000, + TimeUnit.MILLISECONDS)); + ClientBase.recursiveDelete(testDataLearner); + ClientBase.recursiveDelete(testDataLeader); + } + + /** + * SaslQuorumAuthServer throws exception on receiving an invalid quorum + * auth packet. + */ + @Test(timeout = 30000) + public void testSaslQuorumAuthServerWithInvalidQuorumAuthPacket() + throws Exception { + Socket socket = getSocketPair(); + DataOutputStream dout = new DataOutputStream(socket.getOutputStream()); + BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout); + BinaryOutputArchive boa = BinaryOutputArchive + .getArchive(bufferedOutput); + QuorumAuthPacket authPacket = QuorumAuth + .createPacket(QuorumAuth.Status.IN_PROGRESS, null); + authPacket.setMagic(Long.MAX_VALUE); // invalid magic number + boa.writeRecord(authPacket, null); + bufferedOutput.flush(); + QuorumAuthServer authServer = new SaslQuorumAuthServer(true, + "QuorumServer", authzHosts); + BufferedInputStream is = new BufferedInputStream( + socket.getInputStream()); + try { + authServer.authenticate(socket, new DataInputStream(is)); + Assert.fail("Must throw exception as QuorumAuthPacket is invalid"); + } catch (SaslException e) { + // expected + } + } + + /** + * NullQuorumAuthServer should return true when no auth quorum packet + * received and timed out. + */ + @Test(timeout = 30000) + public void testNullQuorumAuthServerShouldReturnTrue() + throws Exception { + Socket socket = getSocketPair(); + QuorumAuthServer authServer = new NullQuorumAuthServer(); + BufferedInputStream is = new BufferedInputStream( + socket.getInputStream()); + // It will throw exception and fail the + // test if any unexpected error. Not adding any extra assertion. + authServer.authenticate(socket, new DataInputStream(is)); + } + + /** + * NullQuorumAuthServer should return true on receiving a valid quorum auth + * packet. + */ + @Test(timeout = 30000) + public void testNullQuorumAuthServerWithValidQuorumAuthPacket() + throws Exception { + Socket socket = getSocketPair(); + DataOutputStream dout = new DataOutputStream(socket.getOutputStream()); + BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout); + BinaryOutputArchive boa = BinaryOutputArchive + .getArchive(bufferedOutput); + QuorumAuthPacket authPacket = QuorumAuth + .createPacket(QuorumAuth.Status.IN_PROGRESS, null); + boa.writeRecord(authPacket, null); + bufferedOutput.flush(); + QuorumAuthServer authServer = new NullQuorumAuthServer(); + BufferedInputStream is = new BufferedInputStream( + socket.getInputStream()); + // It will throw exception and fail the + // test if any unexpected error. Not adding any extra assertion. + authServer.authenticate(socket, new DataInputStream(is)); + } + + private QuorumCnxManager createAndStartManager(long sid) { + return createAndStartManager(sid, new ConcurrentHashMap()); + } + + private QuorumCnxManager createAndStartManager(long sid, ConcurrentHashMap senderWorkerMap) { + QuorumCnxManager peer = new QuorumCnxManager(sid, peers, + new NullQuorumAuthServer(), new NullQuorumAuthLearner(), 10000, + false, quorumCnxnThreadsSize, false, + senderWorkerMap); + executor.submit(peer.listener); + InetSocketAddress electionAddr = peer.view.get(sid).electionAddr; + waitForElectionAddrBinding(electionAddr, 15); + return peer; + } + + private QuorumCnxManager createAndStartManager(long sid, + String serverLoginContext, + String learnerLoginContext, + boolean serverRequireSasl, + boolean learnerRequireSasl) throws Exception { + return createAndStartManager(sid, serverLoginContext, learnerLoginContext, serverRequireSasl, learnerRequireSasl, new ConcurrentHashMap()); + + } + + private QuorumCnxManager createAndStartManager(long sid, + String serverLoginContext, + String learnerLoginContext, + boolean serverRequireSasl, + boolean learnerRequireSasl, + ConcurrentHashMap senderWorkerMap) + throws Exception { + QuorumAuthLearner authClient = new SaslQuorumAuthLearner(learnerRequireSasl, + "NOT_USING_KRB_PRINCIPAL", learnerLoginContext); + QuorumAuthServer authServer = new SaslQuorumAuthServer(serverRequireSasl, + serverLoginContext, authzHosts); + QuorumCnxManager peer = new QuorumCnxManager(sid, peers, + authServer, authClient, 10000, false, quorumCnxnThreadsSize, true, senderWorkerMap); + executor.submit(peer.listener); + InetSocketAddress electionAddr = peer.view.get(sid).electionAddr; + waitForElectionAddrBinding(electionAddr, 15); + return peer; + } + + private void waitForElectionAddrBinding(InetSocketAddress electionAddr, + int retries) { + boolean success = false; + while (retries > 0) { + Socket sock = new Socket(); + try { + sock.setTcpNoDelay(true); + sock.setSoTimeout(5000); + sock.connect(electionAddr, 5000); + success = true; + } catch (IOException e) { + LOG.error("IOException while checking election addr", e); + } finally { + cleanup(sock); + } + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // ignore + } + retries--; + } + Assert.assertTrue("Did not connect to election port", success); + } + + private void cleanup(Socket sock) { + try { + sock.close(); + } catch (IOException ie) { + LOG.error("Exception while closing socket", ie); + } + } + + private void assertEventuallyConnected(QuorumCnxManager peer, long sid) + throws Exception { + for (int i = 0; i < 20 && !peer.connectedToPeer(sid); i++) { + Thread.sleep(1000); + } + Assert.assertTrue("Not connected to peer", peer.connectedToPeer(sid)); + } + + private void assertEventuallyNotConnected(QuorumCnxManager peer, long sid) + throws Exception { + for (int i = 0; i < 3 && !peer.connectedToPeer(sid); i++) { + Thread.sleep(1000); + } + Assert.assertFalse("Connected to peer (shouldn't be)", + peer.connectedToPeer(sid)); + } + + private QuorumPeer createQuorumPeer(File tmpDir, + boolean isQuorumAuthEnabled, boolean isQuorumLearnerAuthRequired, + boolean isQuorumServerAuthRequired, String quorumLearnerLoginContext, + String quorumServerLoginContext, String quorumServicePrincipal) + throws IOException, FileNotFoundException { + QuorumPeer peer = QuorumPeer.testingQuorumPeer(); + peer.syncLimit = 2; + peer.initLimit = 2; + peer.tickTime = 2000; + peer.quorumPeers = new HashMap(); + peer.quorumPeers.put(0L, + new QuorumServer(0, "0.0.0.0", PortAssignment.unique(), null, null)); + peer.quorumPeers.put(1L, + new QuorumServer(1, "0.0.0.0", PortAssignment.unique(), null, null)); + peer.setQuorumVerifier(new QuorumMaj(3)); + peer.setCnxnFactory(new NullServerCnxnFactory()); + // auth + if (isQuorumAuthEnabled) { + peer.authServer = new SaslQuorumAuthServer( + isQuorumServerAuthRequired, quorumServerLoginContext, authzHosts); + peer.authLearner = new SaslQuorumAuthLearner( + isQuorumLearnerAuthRequired, quorumServicePrincipal, + quorumLearnerLoginContext); + } + File version2 = new File(tmpDir, "version-2"); + version2.mkdir(); + FileOutputStream fos; + 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; + } + + private static final class NullServerCnxnFactory extends ServerCnxnFactory { + public void startup(ZooKeeperServer zkServer) + 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 getLocalPort() { + return 0; + } + + public InetSocketAddress getLocalAddress() { + return null; + } + + public Iterable getConnections() { + return null; + } + + public void configure(InetSocketAddress addr, int maxClientCnxns) + throws IOException { + } + + public void closeSession(long sessionId) { + } + + public void closeAll() { + } + + @Override + public int getNumAliveConnections() { + return 0; + } + } + + private static Socket getSocketPair() throws IOException { + ServerSocket ss = new ServerSocket(); + ss.bind(null); + InetSocketAddress endPoint = (InetSocketAddress) ss + .getLocalSocketAddress(); + Socket s = new Socket(endPoint.getAddress(), endPoint.getPort()); + s.setSoTimeout(5000); + return s; + } + + private Leader createLeader(File tmpDir, QuorumPeer peer) throws IOException, + NoSuchFieldException, IllegalAccessException { + LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer); + return new Leader(peer, zk); + } + + private Leader createSimpleLeader(File tmpDir, QuorumPeer peer, + CountDownLatch learnerLatch) throws IOException, + NoSuchFieldException, IllegalAccessException { + LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer); + return new SimpleLeader(peer, zk, learnerLatch); + } + + class SimpleLeader extends Leader { + final CountDownLatch learnerLatch; + + SimpleLeader(QuorumPeer self, LeaderZooKeeperServer zk, + CountDownLatch latch) throws IOException { + super(self, zk); + this.learnerLatch = latch; + } + + @Override + void addLearnerHandler(LearnerHandler learner) { + super.addLearnerHandler(learner); + learnerLatch.countDown(); + } + } + + private LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) + throws IOException, NoSuchFieldException, IllegalAccessException { + FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); + peer.setTxnFactory(logFactory); + Field addrField = peer.getClass().getDeclaredField("myQuorumAddr"); + addrField.setAccessible(true); + addrField.set(peer, new InetSocketAddress(PortAssignment.unique())); + ZKDatabase zkDb = new ZKDatabase(logFactory); + LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, + new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); + return zk; + } + + class SimpleLearnerZooKeeperServer extends LearnerZooKeeperServer { + boolean startupCalled; + + public SimpleLearnerZooKeeperServer(FileTxnSnapLog ftsl, + QuorumPeer self) throws IOException { + super(ftsl, 2000, 2000, 2000, null, new ZKDatabase(ftsl), self); + } + + Learner learner; + + @Override + public Learner getLearner() { + return learner; + } + + @Override + public void startup() { + startupCalled = true; + } + } + + class SimpleLearner extends Learner { + SimpleLearner(FileTxnSnapLog ftsl, QuorumPeer learner) + throws IOException { + self = learner; + zk = new SimpleLearnerZooKeeperServer(ftsl, self); + ((SimpleLearnerZooKeeperServer) zk).learner = this; + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java new file mode 100644 index 0000000..0099c93 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java @@ -0,0 +1,1361 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.mockito.Matchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; +import java.io.LineNumberReader; +import java.io.OutputStreamWriter; +import java.io.StringReader; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.regex.Pattern; + +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.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + + +/** + * Test stand-alone server. + * + */ +public class QuorumPeerMainTest extends QuorumPeerTestBase { + protected static final Logger LOG = + Logger.getLogger(QuorumPeerMainTest.class); + + private Servers servers; + private 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(); + } + } + } + + /** + * Verify the ability to start a cluster. + */ + @Test + public void testQuorum() 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(); + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, + CONNECTION_TIMEOUT)); + + + 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(); + + zk = new ZooKeeper("127.0.0.1:" + 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(); + + Assert.assertTrue("waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, + ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * 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()+"\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(); + } + + 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); + Assert.fail("create /zk" + leader + " should have failed"); + } catch(KeeperException e) {} + + // just make sure that we actually did get it in process at the + // leader + Assert.assertTrue(outstanding.size() == 1); + Assert.assertTrue(((Proposal)outstanding.values().iterator().next()).request.hdr.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) { + 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) { + Assert.assertTrue((j==leader?("Leader ("+leader+")"):("Follower "+j))+" should not have /zk" + i, zk[j].exists("/zk"+i, false) == null); + } else { + Assert.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 + Assert.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(); + Assert.assertTrue("There should be a leader", trueLeader >= 0); + + // find a follower + int falseLeader = (trueLeader + 1) % numServers; + Assert.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 + // and close the connection to the leader + 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 + Assert.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); + } + + Assert.assertTrue("falseLeader never attempts to become leader", foundLeading); + Assert.assertTrue("falseLeader never gives up on leadership", foundLooking); + Assert.assertTrue("falseLeader never rejoins the quorum", foundFollowing); + } + + private void waitForOne(ZooKeeper zk, 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); + } + } + + private void waitForAll(Servers servers, States state) throws InterruptedException { + waitForAll(servers.zk, state); + } + + private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException { + int iterations = ClientBase.CONNECTION_TIMEOUT / 1000; + boolean someoneNotConnected = true; + while (someoneNotConnected) { + if (iterations-- == 0) { + ClientBase.logAllStackTraces(); + throw new RuntimeException("Waiting too long"); + } + + someoneNotConnected = false; + for (ZooKeeper zk : zks) { + if (zk.getState() != state) { + someoneNotConnected = true; + break; + } + } + Thread.sleep(1000); + } + } + + // This class holds the servers and clients for those servers + private 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 { + 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 { + 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) { + return i; + } + } + return -1; + } + } + + private Servers LaunchServers(int numServers) throws IOException, InterruptedException { + return LaunchServers(numServers, null); + } + + /** + * This is a helper function for launching a set of servers + * + * @param numServers the number of servers + * @param tickTime A ticktime to pass to MainThread + * @return + * @throws IOException + * @throws InterruptedException + */ + private Servers LaunchServers(int numServers, Integer tickTime) throws IOException, InterruptedException { + int SERVER_COUNT = numServers; + Servers svrs = new Servers(); + svrs.clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < SERVER_COUNT; i++) { + svrs.clientPorts[i] = PortAssignment.unique(); + sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n"); + } + 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, States.CONNECTED); + + return svrs; + } + + + /** + * 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() + + "\nserver.2=fee.fii.foo.fum:" + PortAssignment.unique() + + ":" + PortAssignment.unique(); + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + 30000); + + Assert.assertFalse("Server never came up", isup); + + q1.shutdown(); + + Assert.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; + } + } + Assert.assertTrue("complains about host", found); + } + + @Test + public void testValidIpv6AddressInQuorum() throws Exception { + assumeIPv6Available(); + + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.INFO); + appender.setImmediateFlush(true); + 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() + + "\nserver.2=[0:0:0:0:0:0:0:1]:" + PortAssignment.unique() + + ":" + PortAssignment.unique(); + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + + q1.start(); + q2.start(); + + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + + Assert.assertTrue("waiting for server 2 being up", + ClientBase.waitForServerUp("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + + Assert.assertTrue("waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + + Assert.assertTrue("waiting for server 2 down", + ClientBase.waitForServerDown("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + os.close(); + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = + Pattern.compile(".*Resolved hostname: 0:0:0:0:0:0:0:1.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + Assert.assertTrue("IPv6 address resolved", found); + } + + @Test + public void testInvalidIpv6AddressInQuorum() throws Exception { + assumeIPv6Available(); + + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.INFO); + appender.setImmediateFlush(true); + 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() + + "\nserver.2=[0:0:0:0:0:0:0:1:" + PortAssignment.unique() + + ":" + PortAssignment.unique(); + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + 30000); + + Assert.assertFalse("Server never came up", isup); + + q1.shutdown(); + + Assert.assertTrue("waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + os.close(); + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = + Pattern.compile(".*QuorumPeerConfig\\$ConfigException.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + Assert.assertTrue("complains about configuration", 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() + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + + ":" + PortAssignment.unique() + + "\nserver.3=127.0.0.1:" + PortAssignment.unique() + + ":" + PortAssignment.unique() + ":observer"; + + 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(); + + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP3, + CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + Assert.assertTrue("waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, + ClientBase.CONNECTION_TIMEOUT)); + Assert.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; + } + } + Assert.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 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + + ":" + electionPort2; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + Assert.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() + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + + ":" + PortAssignment.unique(); + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, + CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + + Assert.assertTrue("waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + Assert.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; + } + } + Assert.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() + + "\nserver.2=127.0.0.1:" + 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) { + Assert.fail("QuorumPeer took " + (end -start) + + " to shutdown, expected " + maxwait); + } + } + + static long readLongFromFile(File file) throws IOException { + 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(); + } + } + + static void writeLongToFile(File file, long value) throws IOException { + AtomicFileOutputStream out = new AtomicFileOutputStream(file); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); + try { + bw.write(Long.toString(value)); + bw.flush(); + out.flush(); + out.close(); + } catch (IOException e) { + LOG.error("Failed to write new file " + file, e); + out.abort(); + throw e; + } + } + + /** + * ZOOKEEPER-1653 Make sure the server starts if the current epoch is less + * than the epoch from last logged zxid and updatingEpoch file exists. + */ + @Test + public void testUpdatingEpoch() throws Exception { + // Create a cluster and restart them multiple times to bump the epoch. + numServers = 3; + servers = LaunchServers(numServers); + File currentEpochFile; + for (int i = 0; i < 10; i++) { + for (int j = 0; j < numServers; j++) { + servers.mt[j].shutdown(); + } + waitForAll(servers.zk, States.CONNECTING); + for (int j = 0; j < numServers; j++) { + servers.mt[j].start(); + } + waitForAll(servers.zk, States.CONNECTED); + } + + // Current epoch is 11 now. + for (int i = 0; i < numServers; i++) { + currentEpochFile = new File( + new File(servers.mt[i].dataDir, "version-2"), + QuorumPeer.CURRENT_EPOCH_FILENAME); + LOG.info("Validating current epoch: " + servers.mt[i].dataDir); + assertEquals("Current epoch should be 11.", 11, + readLongFromFile(currentEpochFile)); + } + + // Find a follower and get epoch from the last logged zxid. + int followerIndex = -1; + for (int i = 0; i < numServers; i++) { + if (servers.mt[i].main.quorumPeer.leader == null) { + followerIndex = i; + break; + } + } + Assert.assertTrue("Found a valid follower", + followerIndex >= 0 && followerIndex < numServers); + MainThread follower = servers.mt[followerIndex]; + long zxid = follower.main.quorumPeer.getLastLoggedZxid(); + long epochFromZxid = ZxidUtils.getEpochFromZxid(zxid); + + // Shutdown the cluster + for (int i = 0; i < numServers; i++) { + servers.mt[i].shutdown(); + } + waitForAll(servers.zk, States.CONNECTING); + + // Make current epoch less than epoch from the last logged zxid. + // The server should fail to start. + File followerDataDir = new File(follower.dataDir, "version-2"); + currentEpochFile = new File(followerDataDir, + QuorumPeer.CURRENT_EPOCH_FILENAME); + writeLongToFile(currentEpochFile, epochFromZxid - 1); + follower.start(); + Assert.assertTrue(follower.mainFailed.await(10, TimeUnit.SECONDS)); + + // Touch the updateEpoch file. Now the server should start. + File updatingEpochFile = new File(followerDataDir, + QuorumPeer.UPDATING_EPOCH_FILENAME); + updatingEpochFile.createNewFile(); + for (int i = 0; i < numServers; i++) { + servers.mt[i].start(); + } + waitForAll(servers.zk, States.CONNECTED); + Assert.assertNotNull("Make sure the server started with acceptEpoch", + follower.main.quorumPeer.getActiveServer()); + Assert.assertFalse("updatingEpoch file should get deleted", + updatingEpochFile.exists()); + } + + @Test + public void testNewFollowerRestartAfterNewEpoch() throws Exception { + numServers = 3; + + servers = LaunchServers(numServers); + waitForAll(servers.zk, States.CONNECTED); + String inputString = "test"; + byte[] input = inputString.getBytes(); + byte[] output; + String path = "/newepochzxidtest"; + + // Create a couple of nodes + servers.zk[0].create(path, input, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + servers.zk[0].setData(path, input, -1); + + // make sure the updates indeed committed. If it is not + // the following statement will throw. + output = servers.zk[1].getData(path, false, null); + + // Shutdown every one + for (int i=0; i < numServers; i++) { + servers.mt[i].shutdown(); + } + + LOG.info("resetting follower"); + MainThread follower = servers.mt[0]; + // delete followers information + File followerDataDir = new File(follower.dataDir, "version-2"); + for(File file: followerDataDir.listFiles()) { + LOG.info("deleting " + file.getName()); + file.delete(); + } + + // Startup everyone except follower, wait for election. + for (int i=1; i < numServers; i++) { + servers.mt[i].start(); + } + for (int i=1; i < numServers; i++) { + waitForOne(servers.zk[i], States.CONNECTED); + } + + follower.start(); + waitForAll(servers.zk, States.CONNECTED); // snapshot should be recieved + + follower.shutdown(); + follower.start(); + + Assert.assertFalse(follower.mainFailed.await(10, TimeUnit.SECONDS)); + waitForAll(servers.zk, States.CONNECTED); + } + + @Test + public void testDataDirAndDataLogDir() throws Exception { + // Arrange + File dataDir = createEmptyTestDir(); + File dataLogDir = createEmptyTestDir(); + try { + QuorumPeerConfig configMock = mock(QuorumPeerConfig.class); + when(configMock.getDataDir()).thenReturn(dataDir.getAbsolutePath()); + when(configMock.getDataLogDir()).thenReturn(dataLogDir.getAbsolutePath()); + + 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(dataLogDir.getAbsolutePath() + "/version-2", txnFactory.getDataDir().getAbsolutePath()); + assertEquals(dataDir.getAbsolutePath() + "/version-2", 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; + } + } + + @Test + public void testFailedTxnAsPartOfQuorumLoss() throws Exception { + final int LEADER_TIMEOUT_MS = 10000; + // 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 + servers.mt[leader].main.quorumPeer.tickTime = LEADER_TIMEOUT_MS; + 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); + Assert.fail("create /zk" + leader + " should have failed"); + } catch (KeeperException e) { + } + + // just make sure that we actually did get it in process at the + // leader + Assert.assertEquals(1, outstanding.size()); + Proposal p = outstanding.values().iterator().next(); + Assert.assertEquals(OpCode.create, p.request.hdr.getType()); + + // make sure it has a chance to write it to disk + int sleepTime = 0; + Long longLeader = new Long(leader); + while (!p.ackSet.contains(longLeader)) { + if (sleepTime > 2000) { + Assert.fail("Transaction not synced to disk within 1 second " + p.ackSet + + " 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 " + leader + " to timeout followers"); + sleepTime = 0; + Follower f = servers.mt[leader].main.quorumPeer.follower; + while (f == null || !f.isRunning()) { + if (sleepTime > LEADER_TIMEOUT_MS * 2) { + Assert.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 + Assert.assertTrue(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(); + 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++) { + Assert.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); + Assert.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); + Assert.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++) { + Assert.assertTrue("waiting for server to start", + ClientBase.waitForServerUp("127.0.0.1:" + svrs.clientPorts[i], CONNECTION_TIMEOUT)); + } + + Assert.assertTrue(svrs.mt[0].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LOOKING); + Assert.assertTrue(svrs.mt[highestServerIndex].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LEADING); + for (int i = 1; i < highestServerIndex; i++) { + Assert.assertTrue(svrs.mt[i].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.FOLLOWING); + } + + // 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(); + } + + } finally { + qlogger.removeAppender(appender); + } + + Assert.assertFalse("Corrupt peer should never become leader", foundLeading); + Assert.assertFalse("Corrupt peer should not attempt connection to out of view leader", foundFollowing); + } + + 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 + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java new file mode 100644 index 0000000..31dbc41 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java @@ -0,0 +1,225 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumBase; +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; + + 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; + volatile TestQPMain main; + final File dataDir; + CountDownLatch mainFailed; + File baseDir; + private final int myid; + private final int clientPort; + private final String quorumCfgSection; + private final 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 = " + myid + " tmpDir = " + baseDir + " clientPort = " + + 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"); + + dataDir = new File(baseDir, "data"); + if (!dataDir.mkdir()) { + throw new IOException("Unable to mkdir " + dataDir); + } + + // Convert windows path to UNIX to avoid problems with "\" + String dir = dataDir.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(dataDir, "myid"); + fwriter = new FileWriter(myidFile); + fwriter.write(Integer.toString(myid)); + 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; + + synchronized public void start() { + main = getTestQPMain(); + currentThread = new Thread(this); + currentThread.start(); + mainFailed = new CountDownLatch(1); + } + + 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); + main.shutdown(); + mainFailed.countDown(); + } 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 clean() { + ClientBase.recursiveDelete(main.quorumPeer.getTxnFactory() + .getDataDir()); + } + + 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; + } + + public TestQPMain getTestQPMain() { + return new TestQPMain(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/WatchLeakTest.java b/src/java/test/org/apache/zookeeper/server/quorum/WatchLeakTest.java new file mode 100644 index 0000000..7471381 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/WatchLeakTest.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 static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.junit.Assert.*; + +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.Collections; +import java.util.List; +import java.util.Random; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.MockPacket; +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.NIOServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.junit.Test; +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 + */ +public class WatchLeakTest { + + protected static final Logger LOG = LoggerFactory + .getLogger(WatchLeakTest.class); + + final long SESSION_ID = 0xBABEL; + + /** + * ZOOKEEPR-1382 test class + */ + @Test + public void testWatchesWithClientSessionTimeout() throws Exception { + + NIOServerCnxnFactory serverCnxnFactory = new NIOServerCnxnFactory(); + + 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("/tmp")); + when(logfactory.getSnapDir()).thenReturn(new File("/tmp")); + FollowerZooKeeperServer fzks = null; + try { + fzks = new FollowerZooKeeperServer(logfactory, quorumPeer, null, + database); + fzks.startup(); + fzks.setServerCnxnFactory(serverCnxnFactory); + quorumPeer.follower = new MyFollower(quorumPeer, fzks); + final SelectionKey sk = new FakeSK(); + // 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); + // Send the connection request as a client do + nioCnxn.doIO(sk); + // Send the invalid session packet to the follower + QuorumPacket qp = createInvalidSessionPacket(); + quorumPeer.follower.processPacket(qp); + // OK, now the follower knows that the session is invalid, let's try + // to + // send it the watches + nioCnxn.doIO(sk); + // wait for the the request processor to do his job + Thread.sleep(1000L); + // Session has not been re-validated ! + // If session has not been validated, there must be NO watches + int watchCount = database.getDataTree().getWatchCount(); + LOG.info("watches = " + watchCount); + assertEquals(0, 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() { + return ops; + } + + } + + /** + * Create a watches message with a single watch on / + * + * @return + */ + 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(-8); + 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. + */ + static final private long superSecret = 0XB3415C00L; + + /** + * Create a connection request + * + * @return + */ + 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 + * + * @throws Exception + */ + private QuorumPacket createInvalidSessionPacket() throws Exception { + QuorumPacket qp = createValidateSessionQuorumPacket(); + 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(false); + qp.setData(bos.toByteArray()); + return qp; + } + + /** + * Forge an validate session packet as a LEARNER do + * + * @return + * @throws Exception + */ + private QuorumPacket createValidateSessionQuorumPacket() 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); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "To validate session 0x" + Long.toHexString(2L)); + } + return qp; + } + +} \ No newline at end of file diff --git a/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java b/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java new file mode 100644 index 0000000..4f831e8 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java @@ -0,0 +1,1376 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.createQuorumPeer; +import static org.apache.zookeeper.server.quorum.ZabUtils.createMockLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.MockLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createLeader; + +import static org.junit.Assert.assertEquals; +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.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.EOFException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.List; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +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.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.ByteBufferOutputStream; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperServer.DataTreeBuilder; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.ZxidUtils; +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.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Zab1_0Test { + private static final Logger LOG = LoggerFactory.getLogger(Zab1_0Test.class); + + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/data")); + + 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()); + Assert.assertEquals("leader got wrong epoch from getEpochToPropose", 6, epoch); + } catch (Exception e){ + Assert.fail("leader timed out in getEpochToPropose"); + } + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + recursiveDelete(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. + * + * {@link 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); + Assert.assertEquals("New proposed epoch is wrong", 7, epoch); + } catch (Exception e){ + Assert.fail("Timed out in getEpochToPropose"); + } + + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + if (leadThread != null) { + leadThread.interrupt(); + leadThread.join(); + } + recursiveDelete(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.readyToStart = true; + 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 + Assert.assertTrue(f1.msg + " without waiting for leader", f1.msg == null); + Assert.assertTrue(f2.msg + " without waiting for leader", f2.msg == null); + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + recursiveDelete(tmpDir); + } + } + + static Socket[] getSocketPair() throws IOException { + ServerSocket ss = new ServerSocket(); + ss.bind(null); + 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; + } + } + } + + static public interface LeaderConversation { + void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws Exception; + } + + static public interface PopulatedLeaderConversation { + void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l, long zxid) throws Exception; + } + + static public interface FollowerConversation { + void converseWithFollower(InputArchive ia, OutputArchive oa, Follower f) throws Exception; + } + + static 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.readyToStart) { + 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(); + } + recursiveDelete(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); + + Assert.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)); + Stat stat = new Stat(); + Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null))); + } + Assert.assertTrue(zxid > ZxidUtils.makeZxid(1, 0)); + + // Generate snapshot and close files. + snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts()); + 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(); + } + recursiveDelete(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(); + ss.bind(null); + 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(); + } + recursiveDelete(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(); + ss.bind(null); + 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(); + } + recursiveDelete(tmpDir); + } + } + + @Test + public void testUnnecessarySnap() throws Exception { + testPopulatedLeaderConversation(new PopulatedLeaderConversation() { + @Override + public void converseWithLeader(InputArchive ia, OutputArchive oa, + Leader l, long zxid) throws Exception { + + Assert.assertEquals(1, l.self.getAcceptedEpoch()); + Assert.assertEquals(1, l.self.getCurrentEpoch()); + + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000); + byte liBytes[] = new byte[12]; + ByteBufferOutputStream.record2ByteBuffer(li, + ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 1, + liBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.LEADERINFO, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(2, 0), qp.getZxid()); + Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), + 0x10000); + Assert.assertEquals(2, l.self.getAcceptedEpoch()); + Assert.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); + Assert.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(); + } + } + } + synchronized public 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 { + Assert.assertEquals(0, f.self.getAcceptedEpoch()); + Assert.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)); + Stat stat = new Stat(); + Assert.assertEquals("data1", new String(zkDb.getData("/foo", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.FOLLOWERINFO, qp.getType()); + Assert.assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(qp.getData()), learnInfo); + Assert.assertEquals(learnInfo.getProtocolVersion(), 0x10000); + Assert.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); + Assert.assertEquals(Leader.ACKEPOCH, qp.getType()); + Assert.assertEquals(0, qp.getZxid()); + Assert.assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer.wrap(qp.getData()).getInt()); + Assert.assertEquals(1, f.self.getAcceptedEpoch()); + Assert.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); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(1, f.self.getAcceptedEpoch()); + Assert.assertEquals(1, f.self.getCurrentEpoch()); + //Make sure that we did take the snapshot now + verify(f.zk).takeSnapshot(); + Assert.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(); + Assert.assertEquals("data1", new String(zkDb2.getData("/foo", stat, null))); + Assert.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 + Assert.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); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(proposalZxid, qp.getZxid()); + + watcher.waitForChange(); + Assert.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(); + Assert.assertEquals("data2", new String(zkDb2.getData("/foo", stat, null))); + Assert.assertEquals(proposalZxid, lastZxid); + } finally { + recursiveDelete(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 { + Assert.assertEquals(0, f.self.getAcceptedEpoch()); + Assert.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)); + Stat stat = new Stat(); + Assert.assertEquals("data1", new String(zkDb.getData("/foo", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.FOLLOWERINFO, qp.getType()); + Assert.assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(qp.getData()), learnInfo); + Assert.assertEquals(learnInfo.getProtocolVersion(), 0x10000); + Assert.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); + Assert.assertEquals(Leader.ACKEPOCH, qp.getType()); + Assert.assertEquals(0, qp.getZxid()); + Assert.assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer.wrap(qp.getData()).getInt()); + Assert.assertEquals(1, f.self.getAcceptedEpoch()); + Assert.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)); + oa.writeRecord(qp, null); + qp.setType(Leader.UPTODATE); + qp.setZxid(0); + oa.writeRecord(qp, null); + + // Read the uptodate ack + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + + // Get the ack of the new leader + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(1, f.self.getAcceptedEpoch()); + Assert.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); + } + + Assert.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()); + Assert.assertNotNull(zkDb2.getSessionWithTimeOuts().get(4L)); + //Snapshot was never taken during very simple sync + verify(f.zk, never()).takeSnapshot(); + } finally { + recursiveDelete(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 { + Assert.assertEquals(0, l.self.getAcceptedEpoch()); + Assert.assertEquals(0, l.self.getCurrentEpoch()); + + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000); + byte liBytes[] = new byte[12]; + ByteBufferOutputStream.record2ByteBuffer(li, + ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0, + liBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.LEADERINFO, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), + 0x10000); + Assert.assertEquals(1, l.self.getAcceptedEpoch()); + Assert.assertEquals(0, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.DIFF, qp.getType()); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.NEWLEADER, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(1, l.self.getAcceptedEpoch()); + Assert.assertEquals(1, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.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 { + Assert.assertEquals(0, l.self.getAcceptedEpoch()); + Assert.assertEquals(0, l.self.getCurrentEpoch()); + + LearnerInfo li = new LearnerInfo(1, 0x10000); + 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); + Assert.assertEquals(Leader.LEADERINFO, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), + 0x10000); + Assert.assertEquals(1, l.self.getAcceptedEpoch()); + Assert.assertEquals(0, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.DIFF, qp.getType()); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.NEWLEADER, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(1, l.self.getAcceptedEpoch()); + Assert.assertEquals(1, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.UPTODATE, qp.getType()); + + l.propose(createNodeRequest(l.zk.getZxid())); + + readPacketSkippingPing(ia, qp); + Assert.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; + } + } + + Assert.fail("Connection hasn't been closed by leader after transaction times out."); + } + + private Request createNodeRequest(long zxid) throws IOException { + TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.create); + CreateTxn ct = new CreateTxn("/foo", "data".getBytes(), null, true, 0); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeRecord(hdr, "header"); + boa.writeRecord(ct, "txn"); + baos.close(); + Request rq = new Request(null, 1, 1, ZooDefs.OpCode.create, ByteBuffer.wrap(baos.toByteArray()), null); + rq.zxid = zxid; + rq.hdr = hdr; + rq.txn = ct; + return rq; + } + }); + } + + 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 { + Assert.assertEquals(0, o.self.getAcceptedEpoch()); + Assert.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)); + zkDb.processTxn(new TxnHeader(13, 1313, foo2Zxid, 33, + ZooDefs.OpCode.create), new CreateTxn("/foo2", + "data1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + false, 1)); + Stat stat = new Stat(); + Assert.assertEquals("data1", + new String(zkDb.getData("/foo1", stat, null))); + Assert.assertEquals("data1", + new String(zkDb.getData("/foo2", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.OBSERVERINFO, qp.getType()); + Assert.assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record( + ByteBuffer.wrap(qp.getData()), learnInfo); + Assert.assertEquals(learnInfo.getProtocolVersion(), 0x10000); + Assert.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); + Assert.assertEquals(Leader.ACKEPOCH, qp.getType()); + Assert.assertEquals(0, qp.getZxid()); + Assert.assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer + .wrap(qp.getData()).getInt()); + Assert.assertEquals(1, o.self.getAcceptedEpoch()); + Assert.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); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.assertEquals(1, o.self.getAcceptedEpoch()); + Assert.assertEquals(1, o.self.getCurrentEpoch()); + + Assert.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(); + Assert.assertEquals("data1", + new String(zkDb2.getData("/foo1", stat, null))); + Assert.assertEquals(foo2Zxid, lastZxid); + + // Register watch + TrackerWatcher watcher = new TrackerWatcher(); + Assert.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); + Assert.assertEquals(Leader.ACK, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + // Data should get updated + watcher.waitForChange(); + Assert.assertEquals("data2", new String(o.zk + .getZKDatabase().getData("/foo1", stat, null))); + Assert.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(); + Assert.assertEquals("data2", new String(zkDb2.getData("/foo1", stat, null))); + Assert.assertEquals("data2", new String(zkDb2.getData("/foo2", stat, null))); + Assert.assertEquals(informZxid, lastZxid); + } finally { + recursiveDelete(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); + byte liBytes[] = new byte[12]; + 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); + Assert.assertEquals(Leader.LEADERINFO, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid()); + Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), + 0x10000); + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.DIFF, qp.getType()); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.NEWLEADER, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid()); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + Assert.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); + byte liBytes[] = new byte[12]; + ByteBufferOutputStream.record2ByteBuffer(li, + ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0, + liBytes, null); + oa.writeRecord(qp, null); + readPacketSkippingPing(ia, qp); + Assert.assertEquals(Leader.LEADERINFO, qp.getType()); + Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + Assert.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 + Assert.assertEquals(0, l.self.getCurrentEpoch()); + } + }); + } + + /** + * verify that a peer with dirty snapshot joining an established cluster + * does not go into an inconsistent state. + * + * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1558} + */ + @Test + public void testDirtySnapshot() + throws IOException, + InterruptedException, + KeeperException, + NoSuchFieldException, + IllegalAccessException { + Socket pair[] = getSocketPair(); + Socket leaderSocket = pair[0]; + Socket followerSocket = pair[1]; + File tmpDir = File.createTempFile("test", "dir"); + 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); + + long zxid = ZxidUtils.makeZxid(0, 1); + String path = "/foo"; + zkDb.processTxn(new TxnHeader(13,1000,zxid,30,ZooDefs.OpCode.create), + new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1)); + Stat stat = new Stat(); + Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null))); + + // Close files + 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(0); + peer.setCurrentEpoch(0); + + leadThread = new LeadThread(leader); + leadThread.start(); + + while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) { + Thread.sleep(20); + } + + leader.shutdown("Shutting down the leader"); + + // Check if there is a valid snapshot (we better not have it) + File snapDir = new File (tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + List files = Util.sortDataDir(snapDir.listFiles(),"snapshot", false); + + for (File f : files) { + try { + Assert.assertFalse("Found a valid snapshot", Util.isValidSnapshot(f)); + } catch (IOException e) { + LOG.info("invalid snapshot " + f, e); + } + } + + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + if (leadThread != null) { + leadThread.interrupt(); + leadThread.join(); + } + recursiveDelete(tmpDir); + } + } + + private void recursiveDelete(File file) { + if (file.isFile()) { + file.delete(); + } else { + // might return null if deleted out from under us... + File[] files = file.listFiles(); + if (files != null) { + for(File c: files) { + recursiveDelete(c); + } + } + file.delete(); + } + } + + 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, new ZooKeeperServer.BasicDataTreeBuilder(), 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); + DataTreeBuilder treeBuilder = new ZooKeeperServer.BasicDataTreeBuilder(); + ZKDatabase zkDb = new ZKDatabase(logFactory); + ObserverZooKeeperServer zk = new ObserverZooKeeperServer(logFactory, peer, treeBuilder, 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(); + long zxid = ZxidUtils.makeZxid(3, 3); + + TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error); + ErrorTxn txn = new ErrorTxn(1); + byte[] buf = Util.marshallTxnEntry(hdr, txn); + Request req = new Request(null, 1, 1, ZooDefs.OpCode.error, + ByteBuffer.wrap(buf), null); + req.hdr = hdr; + req.txn = txn; + logFactory.append(req); + logFactory.commit(); + ZKDatabase zkDb = new ZKDatabase(logFactory); + QuorumPeer peer = QuorumPeer.testingQuorumPeer(); + peer.setZKDatabase(zkDb); + peer.setTxnFactory(logFactory); + peer.getLastLoggedZxid(); + Assert.assertEquals(3, peer.getAcceptedEpoch()); + Assert.assertEquals(3, peer.getCurrentEpoch()); + Assert.assertEquals(3, Integer + .parseInt(readContentsOfFile(new File(version2, + QuorumPeer.CURRENT_EPOCH_FILENAME)))); + Assert.assertEquals(3, Integer + .parseInt(readContentsOfFile(new File(version2, + QuorumPeer.ACCEPTED_EPOCH_FILENAME)))); + } finally { + recursiveDelete(tmpDir); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/ZabUtils.java b/src/java/test/org/apache/zookeeper/server/quorum/ZabUtils.java new file mode 100644 index 0000000..a84a332 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/ZabUtils.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.quorum; + +import org.apache.zookeeper.PortAssignment; +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 java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Field; +import java.net.InetSocketAddress; +import java.util.HashMap; + +public class ZabUtils { + + private ZabUtils() {} + + public static final int SYNC_LIMIT = 2; + + public static QuorumPeer createQuorumPeer(File tmpDir) throws IOException{ + QuorumPeer peer = new QuorumPeer(); + peer.syncLimit = 2; + peer.initLimit = 2; + peer.tickTime = 2000; + peer.quorumPeers = new HashMap(); + peer.quorumPeers.put(0L, new QuorumPeer.QuorumServer(0, "127.0.0.1", PortAssignment.unique(), 0, null)); + peer.quorumPeers.put(1L, new QuorumPeer.QuorumServer(1, "127.0.0.1", PortAssignment.unique(), 0, null)); + peer.quorumPeers.put(2L, new QuorumPeer.QuorumServer(2, "127.0.0.1", PortAssignment.unique(), 0, null)); + peer.setQuorumVerifier(new QuorumMaj(peer.quorumPeers.size())); + peer.setCnxnFactory(new NullServerCnxnFactory()); + File version2 = new File(tmpDir, "version-2"); + version2.mkdir(); + 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{ + LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer); + return new Leader(peer, zk); + } + + public static MockLeader createMockLeader(File tmpDir, QuorumPeer peer) + throws IOException, NoSuchFieldException, IllegalAccessException{ + 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); + Field addrField = peer.getClass().getDeclaredField("myQuorumAddr"); + addrField.setAccessible(true); + addrField.set(peer, new InetSocketAddress(PortAssignment.unique())); + ZKDatabase zkDb = new ZKDatabase(logFactory); + return new LeaderZooKeeperServer(logFactory, peer, new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); + } + + private static final class NullServerCnxnFactory extends ServerCnxnFactory { + public void startup(ZooKeeperServer zkServer) 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 getLocalPort() { + return 0; + } + public InetSocketAddress getLocalAddress() { + return null; + } + public Iterable getConnections() { + return null; + } + public void configure(InetSocketAddress addr, int maxClientCnxns) + throws IOException { + } + public void closeSession(long sessionId) { + } + public void closeAll() { + } + @Override + public int getNumAliveConnections() { + return 0; + } + } + + public static final class MockLeader extends Leader { + + MockLeader(QuorumPeer qp, LeaderZooKeeperServer zk) + throws IOException { + 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/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java new file mode 100644 index 0000000..9617c70 --- /dev/null +++ b/src/java/test/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 org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; + +import java.io.File; +import java.io.IOException; +import java.util.Properties; + +/* + * 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 + Assert.assertFalse(tmpDir.exists()); + Assert.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/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java new file mode 100644 index 0000000..41609fa --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.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.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 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/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java new file mode 100644 index 0000000..ebe541d --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java @@ -0,0 +1,574 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 org.apache.commons.io.Charsets; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.text.StrSubstitutor; +import org.apache.directory.api.ldap.model.schema.SchemaManager; +import org.apache.directory.api.ldap.schemaextractor.SchemaLdifExtractor; +import org.apache.directory.api.ldap.schemaextractor.impl.DefaultSchemaLdifExtractor; +import org.apache.directory.api.ldap.schemaloader.LdifSchemaLoader; +import org.apache.directory.api.ldap.schemamanager.impl.DefaultSchemaManager; +import org.apache.directory.server.constants.ServerDNConstants; +import org.apache.directory.server.core.DefaultDirectoryService; +import org.apache.directory.server.core.api.CacheService; +import org.apache.directory.server.core.api.DirectoryService; +import org.apache.directory.server.core.api.InstanceLayout; +import org.apache.directory.server.core.api.schema.SchemaPartition; +import org.apache.directory.server.core.kerberos.KeyDerivationInterceptor; +import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex; +import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition; +import org.apache.directory.server.core.partition.ldif.LdifPartition; +import org.apache.directory.server.kerberos.KerberosConfig; +import org.apache.directory.server.kerberos.kdc.KdcServer; +import org.apache.directory.server.kerberos.shared.crypto.encryption.KerberosKeyFactory; +import org.apache.directory.server.kerberos.shared.keytab.Keytab; +import org.apache.directory.server.kerberos.shared.keytab.KeytabEntry; +import org.apache.directory.server.protocol.shared.transport.TcpTransport; +import org.apache.directory.server.protocol.shared.transport.UdpTransport; +import org.apache.directory.server.xdbm.Index; +import org.apache.directory.shared.kerberos.KerberosTime; +import org.apache.directory.shared.kerberos.codec.types.EncryptionType; +import org.apache.directory.shared.kerberos.components.EncryptionKey; +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.apache.directory.api.ldap.model.ldif.LdifEntry; +import org.apache.directory.api.ldap.model.ldif.LdifReader; +import org.apache.directory.api.ldap.model.name.Dn; +import org.apache.directory.api.ldap.model.schema.registries.SchemaLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.lang.reflect.Method; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.UUID; + +/** + * 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 2 System properties when started and un-sets them when stopped: + *

      + *
    • java.security.krb5.conf: set to the MiniKDC real/host/port
    • + *
    • 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: 42e3a805117ff7cb054c2442f7b0e0cc54be63ad + */ +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"; + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/data")); + + public static void main(String[] args) throws Exception { + if (args.length < 4) { + System.out.println("Arguments: " + + " []+"); + System.exit(1); + } + 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), Charsets.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, "true"); + } + + /** + * 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 DirectoryService ds; + private KdcServer kdc; + private int port; + private String realm; + private File workDir; + private File krb5conf; + + /** + * 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)); + if (port == 0) { + ServerSocket ss = new ServerSocket(0, 1, InetAddress.getByName(conf.getProperty(KDC_BIND_ADDRESS))); + port = ss.getLocalPort(); + ss.close(); + } + 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() { + return krb5conf; + } + + /** + * Starts the MiniKdc. + * + * @throws Exception thrown if the MiniKdc could not be started. + */ + public synchronized void start() throws Exception { + if (kdc != null) { + throw new RuntimeException("Already started"); + } + initDirectoryService(); + initKDCServer(); + } + + private void initDirectoryService() throws Exception { + ds = new DefaultDirectoryService(); + ds.setInstanceLayout(new InstanceLayout(workDir)); + + CacheService cacheService = new CacheService(); + ds.setCacheService(cacheService); + + // first load the schema + InstanceLayout instanceLayout = ds.getInstanceLayout(); + File schemaPartitionDirectory = new File(instanceLayout.getPartitionsDirectory(), "schema"); + SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(instanceLayout.getPartitionsDirectory()); + extractor.extractOrCopy(); + + SchemaLoader loader = new LdifSchemaLoader(schemaPartitionDirectory); + SchemaManager schemaManager = new DefaultSchemaManager(loader); + schemaManager.loadAllEnabled(); + ds.setSchemaManager(schemaManager); + // Init the LdifPartition with schema + LdifPartition schemaLdifPartition = new LdifPartition(schemaManager); + schemaLdifPartition.setPartitionPath(schemaPartitionDirectory.toURI()); + + // The schema partition + SchemaPartition schemaPartition = new SchemaPartition(schemaManager); + schemaPartition.setWrappedPartition(schemaLdifPartition); + ds.setSchemaPartition(schemaPartition); + + JdbmPartition systemPartition = new JdbmPartition(ds.getSchemaManager()); + systemPartition.setId("system"); + systemPartition.setPartitionPath( + new File(ds.getInstanceLayout().getPartitionsDirectory(), systemPartition.getId()).toURI()); + systemPartition.setSuffixDn(new Dn(ServerDNConstants.SYSTEM_DN)); + systemPartition.setSchemaManager(ds.getSchemaManager()); + ds.setSystemPartition(systemPartition); + + ds.getChangeLog().setEnabled(false); + ds.setDenormalizeOpAttrsEnabled(true); + ds.addLast(new KeyDerivationInterceptor()); + + // create one partition + String orgName = conf.getProperty(ORG_NAME).toLowerCase(Locale.ENGLISH); + String orgDomain = conf.getProperty(ORG_DOMAIN).toLowerCase(Locale.ENGLISH); + + JdbmPartition partition = new JdbmPartition(ds.getSchemaManager()); + partition.setId(orgName); + partition.setPartitionPath(new File(ds.getInstanceLayout().getPartitionsDirectory(), orgName).toURI()); + partition.setSuffixDn(new Dn("dc=" + orgName + ",dc=" + orgDomain)); + ds.addPartition(partition); + // indexes + Set> indexedAttributes = new HashSet>(); + indexedAttributes.add(new JdbmIndex("objectClass", false)); + indexedAttributes.add(new JdbmIndex("dc", false)); + indexedAttributes.add(new JdbmIndex("ou", false)); + partition.setIndexedAttributes(indexedAttributes); + + // And start the ds + ds.setInstanceId(conf.getProperty(INSTANCE)); + ds.startup(); + // context entry, after ds.startup() + Dn dn = new Dn("dc=" + orgName + ",dc=" + orgDomain); + Entry entry = ds.newEntry(dn); + entry.add("objectClass", "top", "domain"); + entry.add("dc", orgName); + ds.getAdminSession().add(entry); + } + + private void initKDCServer() throws Exception { + String orgName = conf.getProperty(ORG_NAME); + String orgDomain = conf.getProperty(ORG_DOMAIN); + String bindAddress = conf.getProperty(KDC_BIND_ADDRESS); + final Map map = new HashMap(); + map.put("0", orgName.toLowerCase(Locale.ENGLISH)); + map.put("1", orgDomain.toLowerCase(Locale.ENGLISH)); + map.put("2", orgName.toUpperCase(Locale.ENGLISH)); + map.put("3", orgDomain.toUpperCase(Locale.ENGLISH)); + map.put("4", bindAddress); + + InputStream is1 = getMinikdcResourceAsStream("minikdc.ldiff"); + + SchemaManager schemaManager = ds.getSchemaManager(); + LdifReader reader = null; + + try { + final String content = StrSubstitutor.replace(IOUtils.toString(is1), map); + reader = new LdifReader(new StringReader(content)); + + for (LdifEntry ldifEntry : reader) { + ds.getAdminSession().add(new DefaultEntry(schemaManager, ldifEntry.getEntry())); + } + } finally { + IOUtils.closeQuietly(reader); + IOUtils.closeQuietly(is1); + } + + KerberosConfig kerberosConfig = new KerberosConfig(); + kerberosConfig.setMaximumRenewableLifetime(Long.parseLong(conf.getProperty(MAX_RENEWABLE_LIFETIME))); + kerberosConfig.setMaximumTicketLifetime(Long.parseLong(conf.getProperty(MAX_TICKET_LIFETIME))); + kerberosConfig.setSearchBaseDn(String.format("dc=%s,dc=%s", orgName, orgDomain)); + kerberosConfig.setPaEncTimestampRequired(false); + kdc = new KdcServer(kerberosConfig); + kdc.setDirectoryService(ds); + + // transport + String transport = conf.getProperty(TRANSPORT); + if (transport.trim().equals("TCP")) { + kdc.addTransports(new TcpTransport(bindAddress, port, 3, 50)); + } else if (transport.trim().equals("UDP")) { + kdc.addTransports(new UdpTransport(port)); + } else { + throw new IllegalArgumentException("Invalid transport: " + transport); + } + kdc.setServiceName(conf.getProperty(INSTANCE)); + kdc.start(); + + StringBuilder sb = new StringBuilder(); + InputStream is2 = getMinikdcResourceAsStream("minikdc-krb5.conf"); + + BufferedReader r = null; + + try { + r = new BufferedReader(new InputStreamReader(is2, Charsets.UTF_8)); + String line = r.readLine(); + + while (line != null) { + sb.append(line).append("{3}"); + line = r.readLine(); + } + } finally { + IOUtils.closeQuietly(r); + IOUtils.closeQuietly(is2); + } + + krb5conf = new File(workDir, "krb5.conf").getAbsoluteFile(); + FileUtils.writeStringToFile(krb5conf, MessageFormat.format(sb.toString(), getRealm(), getHost(), + Integer.toString(getPort()), System.getProperty("line.separator"))); + System.setProperty(JAVA_SECURITY_KRB5_CONF, krb5conf.getAbsolutePath()); + + System.setProperty(SUN_SECURITY_KRB5_DEBUG, conf.getProperty(DEBUG, "false")); + + // refresh the config + Class classRef; + if (System.getProperty("java.vendor").contains("IBM")) { + classRef = Class.forName("com.ibm.security.krb5.internal.Config"); + } else { + classRef = Class.forName("sun.security.krb5.Config"); + } + Method refreshMethod = classRef.getMethod("refresh", new Class[0]); + refreshMethod.invoke(classRef, new Object[0]); + + LOG.info("MiniKdc listening at port: {}", getPort()); + LOG.info("MiniKdc setting JVM krb5.conf to: {}", krb5conf.getAbsolutePath()); + } + + private InputStream getMinikdcResourceAsStream(String resourceName) + throws FileNotFoundException { + File kdcResourceFile = new File(testData, "/kerberos/" + resourceName); + return new FileInputStream(kdcResourceFile); + } + + /** + * Stops the MiniKdc + */ + public synchronized void stop() { + if (kdc != null) { + System.getProperties().remove(JAVA_SECURITY_KRB5_CONF); + System.getProperties().remove(SUN_SECURITY_KRB5_DEBUG); + kdc.stop(); + try { + ds.shutdown(); + } catch (Exception ex) { + LOG.error("Could not shutdown ApacheDS properly: {}", ex.toString(), ex); + } + } + delete(workDir); + } + + 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 { + String orgName = conf.getProperty(ORG_NAME); + String orgDomain = conf.getProperty(ORG_DOMAIN); + String baseDn = "ou=users,dc=" + orgName.toLowerCase(Locale.ENGLISH) + ",dc=" + + orgDomain.toLowerCase(Locale.ENGLISH); + String content = "dn: uid=" + principal + "," + baseDn + "\n" + "objectClass: top\n" + "objectClass: person\n" + + "objectClass: inetOrgPerson\n" + "objectClass: krb5principal\n" + "objectClass: krb5kdcentry\n" + + "cn: " + principal + "\n" + "sn: " + principal + "\n" + "uid: " + principal + "\n" + "userPassword: " + + password + "\n" + "krb5PrincipalName: " + principal + "@" + getRealm() + "\n" + + "krb5KeyVersionNumber: 0"; + + for (LdifEntry ldifEntry : new LdifReader(new StringReader(content))) { + ds.getAdminSession().add(new DefaultEntry(ds.getSchemaManager(), ldifEntry.getEntry())); + } + } + + /** + * 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 { + String generatedPassword = UUID.randomUUID().toString(); + Keytab keytab = new Keytab(); + List entries = new ArrayList(); + for (String principal : principals) { + createPrincipal(principal, generatedPassword); + principal = principal + "@" + getRealm(); + KerberosTime timestamp = new KerberosTime(); + for (Map.Entry entry : KerberosKeyFactory + .getKerberosKeys(principal, generatedPassword).entrySet()) { + EncryptionKey ekey = entry.getValue(); + byte keyVersion = (byte) ekey.getKeyVersion(); + entries.add(new KeytabEntry(principal, 1L, timestamp, keyVersion, ekey)); + } + } + keytab.setEntries(entries); + keytab.write(keytabFile); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java new file mode 100644 index 0000000..f5317f7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java @@ -0,0 +1,185 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 org.apache.directory.server.kerberos.shared.keytab.Keytab; +import org.apache.directory.server.kerberos.shared.keytab.KeytabEntry; +import org.junit.Assert; +import org.junit.Test; + +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 java.io.File; +import java.security.Principal; +import java.util.Set; +import java.util.Map; +import java.util.HashSet; +import java.util.HashMap; +import java.util.Arrays; + +/* + * 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(); + Assert.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"); + Keytab kt = Keytab.read(new File(workDir, "keytab")); + + Set principals = new HashSet(); + for (KeytabEntry entry : kt.getEntries()) { + principals.add(entry.getPrincipalName()); + } + //here principals use \ instead of / + //because org.apache.directory.server.kerberos.shared.keytab.KeytabDecoder + // .getPrincipalName(IoBuffer buffer) use \\ when generates principal + Assert.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(); + Assert.assertEquals(1, subject.getPrincipals().size()); + Assert.assertEquals(KerberosPrincipal.class, + subject.getPrincipals().iterator().next().getClass()); + Assert.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(); + Assert.assertEquals(1, subject.getPrincipals().size()); + Assert.assertEquals(KerberosPrincipal.class, + subject.getPrincipals().iterator().next().getClass()); + Assert.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/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java new file mode 100644 index 0000000..219d5bc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.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.quorum.auth; + +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 org.apache.commons.io.FileUtils; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Assert; +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. + } + } + + public static void cleanupJaasConfig() { + if (jaasConfigDir != null) { + FileUtils.deleteQuietly(jaasConfigDir); + } + } + + protected String startQuorum(final int serverCount, + Map authConfigs, int authServerCount, + boolean delayedServerStartup) throws IOException { + StringBuilder connectStr = new StringBuilder(); + final int[] clientPorts = startQuorum(serverCount, 0, connectStr, + authConfigs, authServerCount, delayedServerStartup); + for (int i = 0; i < serverCount; i++) { + Assert.assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], + ClientBase.CONNECTION_TIMEOUT)); + } + return connectStr.toString(); + } + + /** + * Starts the given number of quorum servers and will wait for the quorum + * formation. + * + * @param serverCount + * total server count includes participants + observers + * @param observerCount + * number of observers + * @param authConfigs + * configuration parameters for authentication + * @param authServerCount + * number of auth enabled servers + * @return client port for the respective servers + * @throws IOException + */ + protected String startQuorum(final int serverCount, int observerCount, + Map authConfigs, int authServerCount) + throws IOException { + StringBuilder connectStr = new StringBuilder(); + final int[] clientPorts = startQuorum(serverCount, observerCount, + connectStr, authConfigs, authServerCount, false); + for (int i = 0; i < serverCount; i++) { + Assert.assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], + ClientBase.CONNECTION_TIMEOUT)); + } + return connectStr.toString(); + } + + /** + * Starts the given number of quorum servers and won't wait for the quorum + * formation. + * + * @param serverCount + * total server count includes participants + observers + * @param observerCount + * number of observers + * @param connectStr + * connection string where clients can used for connection + * establishment + * @param authConfigs + * configuration parameters for authentication + * @param authServerCount + * number of auth enabled servers + * @param delayedServerStartup + * true flag value to add delay between server's startup, false otherwise. + * @return client port for the respective servers + * @throws IOException + */ + protected int[] startQuorum(final int serverCount, int observerCount, + StringBuilder connectStr, Map authConfigs, + int authServerCount, boolean delayedServerStartup) + throws IOException { + final int clientPorts[] = new int[serverCount]; + StringBuilder sb = new StringBuilder(); + + // If there are any Observers then the Observer server details will be + // placed first in the configuration section. + for (int i = 0; i < serverCount; i++) { + clientPorts[i] = PortAssignment.unique(); + String server = ""; + if (observerCount > 0 && i < observerCount) { + // add observer learner type + server = String.format("server.%d=localhost:%d:%d:observer", + i, PortAssignment.unique(), PortAssignment.unique()); + } else { + // add participant learner type + server = String.format("server.%d=localhost:%d:%d:participant", + i, PortAssignment.unique(), PortAssignment.unique()); + } + sb.append(server + "\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++) { + if (observerCount > 0 && i < observerCount) { + String obsCfgSection = quorumCfg + "\npeerType=observer"; + quorumCfg = obsCfgSection; + } + startServer(authConfigs, clientPorts[i], quorumCfg, i, delayedServerStartup); + } + // servers without any authentication configured + for (int j = 0; j < serverCount - authServerCount; j++, i++) { + if (observerCount > 0 && i < observerCount) { + String obsCfgSection = quorumCfg + "\npeerType=observer"; + quorumCfg = obsCfgSection; + } + startServer(null, clientPorts[i], quorumCfg, i, delayedServerStartup); + } + return clientPorts; + } + + private void startServer(Map authConfigs, + final int clientPort, String quorumCfg, int i, + boolean delayedServerStartup) throws IOException { + MainThread mthread; + if (authConfigs != null) { + mthread = new MainThread(i, clientPort, quorumCfg, authConfigs); + } else { + mthread = new MainThread(i, clientPort, quorumCfg); + } + mt.add(mthread); + mthread.start(); + + if (delayedServerStartup) { + addDelayBeforeStartingNextServer(mthread); + } + } + + private void addDelayBeforeStartingNextServer(MainThread mThread) { + // Refer https://issues.apache.org/jira/browse/ZOOKEEPER-2712 + LOG.info("Waiting to finish login context init(Krb login), " + + "as there are potential concurrency issues in ApacheDS " + + "if multiple servers starts together!"); + int retries = 60; // 15secs delay + while (retries > 0) { + if (mThread.getQuorumPeer() != null + && mThread.getQuorumPeer().hasAuthInitialized()) { + try { + Thread.sleep(1000); // adding 1sec grace period. + } catch (InterruptedException e) { + LOG.info("Ignore InterruptedException"); + } + break; + } + // moving to next retry cycle + retries--; + try { + Thread.sleep(250); + } catch (InterruptedException e) { + LOG.info("Ignore InterruptedException"); + } + } + } + + 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) { + } finally { + mt.remove(index); + } + mainThread.deleteBaseDir(); + return mainThread; + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.java new file mode 100644 index 0000000..4eeccf3 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.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.quorum.auth; + +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.ClientTest; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +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 = new String("" + "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 + public void tearDown() throws Exception { + shutdownAll(); + } + + @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, false); + 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, false); + 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, false); + 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, false); + 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, false); + 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); + Assert.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); + Assert.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/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.java new file mode 100644 index 0000000..c2f4cc3 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.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.server.quorum.auth; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +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.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +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.Assert; +import org.junit.Test; + +public class QuorumDigestAuthTest extends QuorumAuthTestBase { + + private ZooKeeper zk; + static { + String jaasEntries = new String("" + + "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 + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + if (zk != null) { + zk.close(); + } + } + + @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, false); + CountdownWatcher watcher = new CountdownWatcher(); + 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); + } + } + + /** + * 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, false); + CountdownWatcher watcher = new CountdownWatcher(); + 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); + } + } + + /** + * 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, 0, + new StringBuilder(), authConfigs, serverCount, false); + for (int i = 0; i < serverCount; i++) { + boolean waitForServerUp = ClientBase.waitForServerUp( + "127.0.0.1:" + clientPorts[i], QuorumPeerTestBase.TIMEOUT); + Assert.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 { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + Assert.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 { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + Assert.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 { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + Assert.fail("Must throw exception as quorum sasl is not enabled!"); + } catch (ConfigException e) { + // expected + } + } + + /** + * Test to verify that Observer server is able to join quorum. + */ + @Test(timeout = 30000) + public void testObserverWithValidCredentials() 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"); + + // Starting auth enabled 5-node cluster. 3-Participants and 2-Observers. + int totalServerCount = 5; + int observerCount = 2; + String connectStr = startQuorum(totalServerCount, observerCount, + authConfigs, totalServerCount); + CountdownWatcher watcher = new CountdownWatcher(); + zk = new ZooKeeper(connectStr.toString(), ClientBase.CONNECTION_TIMEOUT, + watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + + /** + * Test to verify that non-auth enabled Observer server should be rejected + * by the auth enabled quorum servers. + */ + @Test(timeout = 30000) + public void testNonAuthEnabledObserverJoiningAuthEnabledQuorum() + 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"); + + // Starting auth enabled 3-node cluster. + int totalServerCount = 3; + String connectStr = startQuorum(totalServerCount, authConfigs, + totalServerCount, false); + + CountdownWatcher watcher = new CountdownWatcher(); + zk = new ZooKeeper(connectStr.toString(), ClientBase.CONNECTION_TIMEOUT, + watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + + // Adding a non-auth enabled Observer to the 3-node auth cluster. + String quorumCfgSection = mt.get(0).getQuorumCfgSection(); + int observerMyid = totalServerCount + 1; + StringBuilder newObsCfgSection = new StringBuilder(quorumCfgSection); + newObsCfgSection.append("\n"); + newObsCfgSection.append(String.format( + "server.%d=localhost:%d:%d:observer", observerMyid, + PortAssignment.unique(), PortAssignment.unique())); + newObsCfgSection.append("\npeerType=observer"); + newObsCfgSection.append("\n"); + int clientPort = PortAssignment.unique(); + newObsCfgSection.append("127.0.0.1:" + clientPort); + MainThread mthread = new MainThread(observerMyid, clientPort, + newObsCfgSection.toString()); + mt.add(mthread); + mthread.start(); + + boolean waitForServerUp = ClientBase.waitForServerUp( + "127.0.0.1:" + clientPort, QuorumPeerTestBase.TIMEOUT); + Assert.assertFalse( + "Non-auth enabled Observer shouldn't be able join auth-enabled quorum", + waitForServerUp); + + // quorum shouldn't be disturbed due to rejection. + zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + } + + /** + * Test to verify that server is able to reform quorum if the Leader goes + * down. + */ + @Test(timeout = 30000) + public void testRelectionWithValidCredentials() 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, false); + CountdownWatcher watcher = new CountdownWatcher(); + zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + watcher.reset(); + + // Shutdown Leader to trigger re-election + QuorumPeer leaderQP = getLeaderQuorumPeer(mt); + LOG.info("Shutdown Leader sid:{} to trigger quorum leader-election", + leaderQP.getId()); + shutdownQP(leaderQP); + + // Wait for quorum formation + QuorumPeer newLeaderQP = waitForLeader(); + assertNotNull("New leader must have been elected by now", newLeaderQP); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + } + + private QuorumPeer waitForLeader() throws InterruptedException { + int retryCnt = 0; + QuorumPeer newLeaderQP = null; + while (retryCnt < 30) { + newLeaderQP = getLeaderQuorumPeer(mt); + if (newLeaderQP != null) { + LOG.info("Number of retries:{} to findout new Leader", + retryCnt); + break; + } + retryCnt--; + Thread.sleep(500); + } + return newLeaderQP; + } + + private void shutdownQP(QuorumPeer qp) throws InterruptedException { + assertNotNull("QuorumPeer doesn't exist!", qp); + qp.shutdown(); + + int retryCnt = 30; + while (retryCnt > 0) { + if (qp.getPeerState() == ServerState.LOOKING) { + LOG.info("Number of retries:{} to change the server state to {}", + retryCnt, ServerState.LOOKING); + break; + } + Thread.sleep(500); + retryCnt--; + } + Assert.assertEquals( + "After shutdown, QuorumPeer should change its state to LOOKING", + ServerState.LOOKING, qp.getPeerState()); + } + + private QuorumPeer getLeaderQuorumPeer(List mtList) { + for (MainThread mt : mtList) { + QuorumPeer quorumPeer = mt.getQuorumPeer(); + if (null != quorumPeer + && ServerState.LEADING == quorumPeer.getPeerState()) { + return quorumPeer; + } + } + return null; + } +} diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java new file mode 100644 index 0000000..e3eddf7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.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.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.Before; +import org.junit.Test; + +public class QuorumKerberosAuthTest extends KerberosSecurityTestcase { + private static File keytabFile; + static { + String keytabFilePath = FilenameUtils.normalize(KerberosTestUtils.getKeytabFile(), true); + String jaasEntries = new String("" + + "QuorumServer {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + keytabFilePath + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=true\n" + + " doNotPrompt=true\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=true\n" + + " doNotPrompt=true\n" + + " refreshKrb5Config=true\n" + + " isInitiator=true\n" + + " principal=\"" + KerberosTestUtils.getLearnerPrincipal() + "\";\n" + "};\n"); + setupJaasConfig(jaasEntries); + } + + @Before + public 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 + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + } + + @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, true); + 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/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java new file mode 100644 index 0000000..55deefb --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java @@ -0,0 +1,192 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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 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; + +import junit.framework.Assert; + +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); + String jaasEntries = new String("" + + "QuorumServer {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + keytabFilePath + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=true\n" + + " doNotPrompt=true\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=true\n" + + " doNotPrompt=true\n" + + " refreshKrb5Config=true\n" + + " isInitiator=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=true\n" + + " doNotPrompt=true\n" + + " refreshKrb5Config=true\n" + + " isInitiator=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 + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + } + + @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, true); + 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, true); + 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); + Assert.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/src/java/test/org/apache/zookeeper/server/util/PortForwarder.java b/src/java/test/org/apache/zookeeper/server/util/PortForwarder.java new file mode 100644 index 0000000..acbad80 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/util/PortForwarder.java @@ -0,0 +1,256 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY 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.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 pfs = startForwarding(); + * .... + * // simulate a network interruption for server 1 + * stopForwarding(pfs); + * .... + * // restore connection + * pfs = startForwarding(); + * + * + * private List startForwarding() throws IOException { + * List res = new ArrayList(); + * 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 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; + + PortForwardWorker(Socket toClose, Socket toClose2, InputStream in, + OutputStream out) throws IOException { + 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); + try { + toClose.close(); + } catch (IOException ex) { + // ignore + } + try { + toClose2.close(); + } catch (IOException ex) { + // ignore + } + break; + } + } + } catch (SocketTimeoutException e) { + LOG.error("socket timeout", e); + } + Thread.sleep(1); + } + } catch (InterruptedException e) { + LOG.warn("Interrupted", e); + try { + toClose.close(); + } catch (IOException ex) { + // ignore + } + try { + toClose2.close(); + } catch (IOException ex) { + // ignore silently + } + } catch (SocketException e) { + if (!"Socket closed".equals(e.getMessage())) { + LOG.error("Unexpected exception", e); + } + } catch (IOException e) { + LOG.error("Unexpected exception", e); + } + LOG.info("Shutting down forward for " + toClose); + } + + } + + private volatile boolean stopped = false; + private ExecutorService workers = Executors.newCachedThreadPool(); + 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:" + + serverSocket.getLocalPort() + " to:" + to); + sock = serverSocket.accept(); + LOG.info("accepted: local:" + sock.getLocalPort() + + " from:" + sock.getPort() + + " to:" + 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(" + retry + + "): local:" + sock.getLocalPort() + + " from:" + sock.getPort() + + " to:" + to, e); + } + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + retry--; + } + LOG.info("connected: local:" + sock.getLocalPort() + + " from:" + sock.getPort() + + " to:" + to); + sock.setSoTimeout(30000); + target.setSoTimeout(30000); + this.workers.execute(new PortForwardWorker(sock, target, + sock.getInputStream(), target.getOutputStream())); + this.workers.execute(new PortForwardWorker(target, sock, + target.getInputStream(), sock.getOutputStream())); + } catch (SocketTimeoutException e) { + LOG.warn("socket timed out local:" + + (sock != null ? sock.getLocalPort(): "") + + " from:" + (sock != null ? sock.getPort(): "") + + " to:" + to, e); + } catch (ConnectException e) { + LOG.warn("connection exception local:" + + (sock != null ? sock.getLocalPort(): "") + + " from:" + (sock != null ? sock.getPort(): "") + + " to:" + to, e); + sock.close(); + } catch (IOException e) { + if (!"Socket closed".equals(e.getMessage())) { + LOG.warn("unexpected exception local:" + + (sock != null ? sock.getLocalPort(): "") + + " from:" + (sock != null ? sock.getPort(): "") + + " to:" + 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.workers.shutdownNow(); + try { + if (!this.workers.awaitTermination(5, TimeUnit.SECONDS)) { + throw new Exception( + "Failed to stop forwarding within 5 seconds"); + } + } catch (InterruptedException e) { + throw new Exception("Failed to stop forwarding"); + } + this.join(); + } +} diff --git a/src/java/test/org/apache/zookeeper/server/util/SerializeUtilsTest.java b/src/java/test/org/apache/zookeeper/server/util/SerializeUtilsTest.java new file mode 100644 index 0000000..61893f7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/server/util/SerializeUtilsTest.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.server.util; + +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; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.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; + +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/src/java/test/org/apache/zookeeper/test/ACLCountTest.java b/src/java/test/org/apache/zookeeper/test/ACLCountTest.java new file mode 100644 index 0000000..88b8869 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ACLCountTest.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.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.util.ArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +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.Assert; +import org.junit.Test; + +public class ACLCountTest 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; + + /** + * + * 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"); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + + String path = "/path"; + + try { + Assert.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()); + Assert.assertEquals(2,acls.size()); + } + catch (Exception e) { + // test failed somehow. + Assert.assertTrue(false); + } + + f.shutdown(); + zks.shutdown(); + } + + + /* + * (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); + } + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/ACLRootTest.java b/src/java/test/org/apache/zookeeper/test/ACLRootTest.java new file mode 100644 index 0000000..7307cbf --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ACLRootTest.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.test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Assert; +import org.junit.Test; + +public class ACLRootTest extends ClientBase { + private static final Logger LOG = LoggerFactory.getLogger(ACLRootTest.class); + + @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); + Assert.fail("validate auth"); + } catch (KeeperException.NoAuthException e) { + // expected + } + try { + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, + CreateMode.PERSISTENT); + Assert.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); + Assert.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); + Assert.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/src/java/test/org/apache/zookeeper/test/ACLTest.java b/src/java/test/org/apache/zookeeper/test/ACLTest.java new file mode 100644 index 0000000..793265c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ACLTest.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.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.util.ArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeper; +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.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +import org.junit.Test; + +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 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"); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + try { + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + } finally { + zk.close(); + } + } finally { + f.shutdown(); + zks.shutdown(); + Assert.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"); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + 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); + } + Assert.assertTrue("size of the acl map ", (1 == 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); + ArrayList list = new ArrayList(); + list.add(acl); + zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT); + } + Assert.assertTrue("size of the acl map ", (101 == zks.getZKDatabase().getAclSize())); + } finally { + // now shutdown the server and restart it + f.shutdown(); + zks.shutdown(); + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } + startSignal = new CountDownLatch(1); + + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + f = ServerCnxnFactory.createFactory(PORT, -1); + + f.startup(zks); + try { + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + startSignal.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + Assert.assertTrue("count == 0", startSignal.getCount() == 0); + + Assert.assertTrue("acl map ", (101 == 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); + } + Assert.assertTrue("acl map ", (106 == zks.getZKDatabase().getAclSize())); + + zk.close(); + } finally { + f.shutdown(); + zks.shutdown(); + Assert.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); + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java b/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java new file mode 100644 index 0000000..ebfc963 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.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.test.ClientBase.CONNECTION_TIMEOUT; +import static org.apache.zookeeper.test.ClientBase.verifyThreadTerminated; + +import java.util.LinkedList; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Assert; +import org.junit.Test; + +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 Assert.failed!!!"); // sanity check + return; + } + } catch (Exception e) { + LOG.error("Client create operation Assert.failed", e); + return; + } finally { + if (zk != null) { + try { + zk.close(); + if (!zk.testableWaitForShutdown(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--; + Assert.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 Assert.failed for 0x" + + Long.toHexString(zk.getSessionId()) + + "with rc:" + rc + " path:" + path); + } + decOutstanding(); + return; + } + try { + decOutstanding(); + zk.delete(name, -1, this, null); + } catch (Exception e) { + if (bang) { + failed = true; + LOG.error("Client delete Assert.failed", e); + } + } + } + + public void processResult(int rc, String path, Object ctx) { + if (rc != KeeperException.Code.OK.intValue()) { + if (bang) { + failed = true; + LOG.error("Delete Assert.failed for 0x" + + Long.toHexString(zk.getSessionId()) + + "with rc:" + rc + " path:" + 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); + Assert.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/src/java/test/org/apache/zookeeper/test/AsyncOps.java b/src/java/test/org/apache/zookeeper/test/AsyncOps.java new file mode 100644 index 0000000..2dbe644 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/AsyncOps.java @@ -0,0 +1,655 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.ACLCallback; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.DataCallback; +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.ZooDefs.Ids; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; + +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 static abstract 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: where 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) { + Assert.fail("unexpected interrupt"); + } + // on the lookout for timeout + Assert.assertSame(0L, latch.getCount()); + + String actual = toString(); + + Assert.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() + "::"); + } + 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 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; + } + } + + +} diff --git a/src/java/test/org/apache/zookeeper/test/AsyncOpsTest.java b/src/java/test/org/apache/zookeeper/test/AsyncOpsTest.java new file mode 100644 index 0000000..c807d72 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/AsyncOpsTest.java @@ -0,0 +1,227 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.DataCB; +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; + +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 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 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(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/AsyncTest.java b/src/java/test/org/apache/zookeeper/test/AsyncTest.java new file mode 100644 index 0000000..2182032 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/AsyncTest.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.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +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(); + } + + public void restart() throws Exception { + JMXEnv.setUp(); + qb.startServers(); + } + + @After + public void tearDown() throws Exception { + LOG.info("Test clients shutting down"); + qb.tearDown(); + } + + 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 ZooKeeper createClient() throws IOException,InterruptedException { + return createClient(qb.hostPort); + } + + private ZooKeeper createClient(String hp) + throws IOException, InterruptedException + { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(hp, CONNECTION_TIMEOUT, watcher); + if(!watcher.clientConnected.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS)) + { + Assert.fail("Unable to connect to server"); + } + return zk; + } + + LinkedList results = new LinkedList(); + @Test + public void testAsync() + throws IOException, InterruptedException, KeeperException + { + 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(); + } + } + Assert.assertEquals(0, (int) results.get(0)); + Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1))); + Assert.assertEquals(0, (int) results.get(2)); + Assert.assertEquals(0, (int) results.get(3)); + Assert.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()); + Assert.fail("Should have received a permission error"); + } catch (KeeperException e) { + Assert.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/src/java/test/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java b/src/java/test/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java new file mode 100644 index 0000000..cbd2b77 --- /dev/null +++ b/src/java/test/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/src/java/test/org/apache/zookeeper/test/AuthTest.java b/src/java/test/org/apache/zookeeper/test/AuthTest.java new file mode 100644 index 0000000..60e3453 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/AuthTest.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 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.Assert; +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); + Assert.fail("Should get auth state error"); + } catch(KeeperException.AuthFailedException e) { + if(!authFailed.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS)) + { + Assert.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); + Assert.fail("Should get auth state error"); + } catch(KeeperException.AuthFailedException e) { + if(!authFailed.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS)) + { + Assert.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); + Assert.fail("auth verification"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify bad pass Assert.fails + zk = createClient(); + zk.addAuthInfo("digest", "pat:pass2".getBytes()); + try { + zk.getData("/path1", false, null); + Assert.fail("auth verification"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify super with bad pass Assert.fails + zk = createClient(); + zk.addAuthInfo("digest", "super:test2".getBytes()); + try { + zk.getData("/path1", false, null); + Assert.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/src/java/test/org/apache/zookeeper/test/BufferSizeTest.java b/src/java/test/org/apache/zookeeper/test/BufferSizeTest.java new file mode 100644 index 0000000..6d74e54 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/BufferSizeTest.java @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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", "build/test/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 + tmpDir = testDir; + try { + startServer(); + fail(failureMsg); + } catch (IOException e) { + LOG.info("Successfully caught IOException: " + e); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ChrootAsyncTest.java b/src/java/test/org/apache/zookeeper/test/ChrootAsyncTest.java new file mode 100644 index 0000000..e60ea04 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ChrootAsyncTest.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.test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; + +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/src/java/test/org/apache/zookeeper/test/ChrootClientTest.java b/src/java/test/org/apache/zookeeper/test/ChrootClientTest.java new file mode 100644 index 0000000..e0286e1 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ChrootClientTest.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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Test; + +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/src/java/test/org/apache/zookeeper/test/ChrootTest.java b/src/java/test/org/apache/zookeeper/test/ChrootTest.java new file mode 100644 index 0000000..3df068b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ChrootTest.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 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.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Assert; +import org.junit.Test; + +public class ChrootTest extends ClientBase { + private 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)) { + Assert.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 { + Assert.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"); + Assert.assertNotNull(zk1.exists("/ch1", w1)); + MyWatcher w2 = new MyWatcher("/ch1/ch2"); + Assert.assertNotNull(zk1.exists("/ch1/ch2", w2)); + + MyWatcher w3 = new MyWatcher("/ch2"); + Assert.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 + Assert.assertTrue(w1.matches()); + Assert.assertTrue(w2.matches()); + Assert.assertTrue(w3.matches()); + + // check exceptions + try { + zk2.setData("/ch3", "3".getBytes(), -1); + } catch (KeeperException.NoNodeException e) { + Assert.assertEquals("/ch3", e.getPath()); + } + + Assert.assertTrue(Arrays.equals("1".getBytes(), + zk1.getData("/ch1", false, null))); + Assert.assertTrue(Arrays.equals("2".getBytes(), + zk1.getData("/ch1/ch2", false, null))); + Assert.assertTrue(Arrays.equals("2".getBytes(), + zk2.getData("/ch2", false, null))); + + // check delete + zk2.delete("/ch2", -1); + Assert.assertTrue(w4.matches()); + Assert.assertTrue(w5.matches()); + + zk1.delete("/ch1", -1); + Assert.assertNull(zk1.exists("/ch1", false)); + Assert.assertNull(zk1.exists("/ch1/ch2", false)); + Assert.assertNull(zk2.exists("/ch2", false)); + } finally { + if(zk1 != null) + zk1.close(); + if(zk2 != null) + zk2.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ClientBase.java b/src/java/test/org/apache/zookeeper/test/ClientBase.java new file mode 100644 index 0000000..7de4f3c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ClientBase.java @@ -0,0 +1,757 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +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 junit.framework.TestCase; + +import org.apache.zookeeper.common.Time; +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.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxnFactoryAccessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FilePadding; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.After; +import org.junit.Assert; +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; + static final File BASETEST = + new File(System.getProperty("build.test.dir", "build")); + + protected int port = PortAssignment.unique(); + protected String hostPort = "127.0.0.1:" + port; + protected String ipv6HostPort = "[0:0:0:0:0:0:0:1]:" + port; + protected int maxCnxns = 0; + protected ServerCnxnFactory serverFactory = null; + protected File tmpDir = null; + + long initialFdCount; + + public ClientBase() { + super(); + } + + /** + * In general don't use this. Only use in the special case that you + * want to ignore results (for whatever reason) in your test. Don't + * use empty watchers in real code! + * + */ + protected class NullWatcher implements Watcher { + public void process(WatchedEvent event) { /* nada */ } + } + + public static class CountdownWatcher implements Watcher { + // XXX 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(); + } + synchronized public void reset() { + clientConnected = new CountDownLatch(1); + connected = false; + syncConnected = false; + readOnlyConnected = false; + } + synchronized public 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(); + } + } + synchronized public boolean isConnected() { + return connected; + } + synchronized public 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("Did not connect"); + + } + } + synchronized public void waitForSyncConnected(long timeout) + throws InterruptedException, TimeoutException + { + long expire = System.currentTimeMillis() + timeout; + long left = timeout; + while(!syncConnected && left > 0) { + wait(left); + left = expire - System.currentTimeMillis(); + } + if (!syncConnected) { + throw new TimeoutException("Failed to connect to read-write ZooKeeper server."); + } + } + synchronized public 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."); + } + } + synchronized public 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 LinkedList 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)) + { + Assert.fail("Unable to connect to server"); + } + synchronized(this) { + if (!allClientsSetup) { + LOG.error("allClients never setup"); + Assert.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) { + 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"); + if (result.startsWith("Zookeeper version:") && + !result.contains("READ-ONLY")) { + return true; + } + } catch (IOException e) { + // ignore as this is expected + LOG.info("server " + hp + " not up " + 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) { + long start = Time.currentElapsedTime(); + while (true) { + try { + HostPort hpobj = parseHostPortList(hp).get(0); + send4LetterWord(hpobj.host, hpobj.port, "stat"); + } catch (IOException e) { + return true; + } + + if (Time.currentElapsedTime() > start + timeout) { + break; + } + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + } + return false; + } + + public static boolean waitForServerState(QuorumPeer qp, int timeout, + String serverState) { + long start = Time.currentElapsedTime(); + while (true) { + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + if (qp.getServerState().equals(serverState)) + 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())); + Assert.assertFalse("thread " + thread.getName() + + " still alive after join", true); + } + } + + public static File createEmptyTestDir() throws IOException { + return createTmpDir(BASETEST, false); + } + + public static File createTmpDir() throws IOException { + return createTmpDir(BASETEST, 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"); + Assert.assertFalse(tmpDir.exists()); // never true if tmpfile does it's job + Assert.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()) { + Assert.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) 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); + factory.startup(zks); + Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( + "127.0.0.1:" + port, CONNECTION_TIMEOUT)); + } + + /** + * This method instantiates a new server. Starting of the server + * instance has been moved to a separate method + * {@link ClientBase#startServerInstance(File, ServerCnxnFactory, String)}. + * 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 = getServer(factory); + 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); + + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + PORT, + CONNECTION_TIMEOUT)); + } + } + + /** + * 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 Assert.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 { + /* 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() == true) { + initialFdCount = osMbean.getOpenFileDescriptorCount(); + LOG.info("Initial fdcount is: " + + initialFdCount); + } + + setupTestEnv(); + + JMXEnv.setUp(); + + setUpAll(); + + tmpDir = createTmpDir(BASETEST, true); + + startServer(); + + LOG.info("Client test setup finished"); + } + + protected void startServer() throws Exception { + LOG.info("STARTING server"); + serverFactory = createNewServerInstance(serverFactory, hostPort, + maxCnxns); + startServerInstance(tmpDir, serverFactory, hostPort); + // 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:" + zkc.getSessionId() + + " client bean:" + clientBean.toString()); + childItr.remove(); + } + } + } + } + for (ObjectName bean : children) { + LOG.info("unexpected:" + bean.toString()); + } + TestCase.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 static ZooKeeperServer getServer(ServerCnxnFactory fac) { + ZooKeeperServer zs = ServerCnxnFactoryAccessor.getZkServer(fac); + + return zs; + } + + 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) { + Assert.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() == true) { + 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); + } + } + } + + public static MBeanServerConnection jmxConn() throws IOException { + return JMXEnv.conn(); + } + + public static boolean recursiveDelete(File d) { + if (d.isDirectory()) { + File children[] = d.listFiles(); + for (File f : children) { + Assert.assertTrue("delete " + f.toString(), recursiveDelete(f)); + } + } + return d.delete(); + } + + 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 Assert.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.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, Integer.valueOf(counts[i-1]), Integer.valueOf(counts[i])); + } else { + LOG.info(logmsg, Integer.valueOf(counts[i-1]), Integer.valueOf(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 { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(cxnString, sessionTimeout, watcher); + try { + watcher.waitForConnected(CONNECTION_TIMEOUT); + } catch (InterruptedException e) { + Assert.fail("ZooKeeper client can not connect to " + cxnString); + } + catch (TimeoutException e) { + Assert.fail("ZooKeeper client can not connect to " + cxnString); + } + return zk; + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ClientHammerTest.java b/src/java/test/org/apache/zookeeper/test/ClientHammerTest.java new file mode 100644 index 0000000..025ccbe --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ClientHammerTest.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 java.io.IOException; +import java.util.Date; +import java.util.List; + +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Assert; +import org.junit.Test; + +public class ClientHammerTest extends ClientBase { + protected static final Logger LOG = LoggerFactory.getLogger(ClientHammerTest.class); + + private static final long HAMMERTHREAD_LATENCY = 5; + + private static abstract 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 Assert.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 Assert.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 Assert.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 Assert.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(h.getName() + " never finished creation, current:" + + h.current); + } + } else { + LOG.info("Hammer threads completed creation operations"); + } + + for (HammerThread h : threads) { + final int safetyFactor = 3; + verifyThreadTerminated(h, + threads.length * childCount + * HAMMERTHREAD_LATENCY * safetyFactor); + } + LOG.info(new Date() + " Total time " + + (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); + Assert.assertEquals(childCount, children.size()); + children = zk.getChildren("/test-" + i, false, null); + Assert.assertEquals(childCount, children.size()); + } + for (int i = 0; i < threads.length; i++) { + List children = + zk.getChildren("/test-" + i, false); + Assert.assertEquals(childCount, children.size()); + children = zk.getChildren("/test-" + i, false, null); + Assert.assertEquals(childCount, children.size()); + } + } finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ClientPortBindTest.java b/src/java/test/org/apache/zookeeper/test/ClientPortBindTest.java new file mode 100644 index 0000000..95c55e2 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ClientPortBindTest.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.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +import org.junit.Test; + +public class ClientPortBindTest extends ZKTestCase implements Watcher { + protected static final Logger LOG = + LoggerFactory.getLogger(ClientPortBindTest.class); + + private volatile CountDownLatch startSignal; + + /** + * 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(); + break; + } + } + } + } catch (SocketException se) { + LOG.warn("Couldn't find loopback interface: " + se.getMessage()); + } + } + if (bindAddress == null) { + LOG.warn("Unable to determine loop back address, skipping test"); + return; + } + final int PORT = PortAssignment.unique(); + + LOG.info("Using " + bindAddress + " as the bind address"); + final String HOSTPORT = bindAddress + ":" + PORT; + LOG.info("Using " + HOSTPORT + " as the host/port"); + + + 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"); + + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(HOSTPORT, + CONNECTION_TIMEOUT)); + + startSignal = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + try { + boolean isZero = startSignal.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + Assert.assertTrue("count == " + startSignal.getCount(), isZero); + zk.close(); + } finally { + f.shutdown(); + zks.shutdown(); + + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(HOSTPORT, + CONNECTION_TIMEOUT)); + } + } + + 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/src/java/test/org/apache/zookeeper/test/ClientRetry.java b/src/java/test/org/apache/zookeeper/test/ClientRetry.java new file mode 100644 index 0000000..d0b6b43 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ClientRetry.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 java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.junit.Assert; +import org.junit.Test; + +public class ClientRetry 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(); + Assert.assertSame(s1,States.CONNECTED); + Assert.assertSame(s2,States.CONNECTING); + cdw1.reset(); + cdw1.waitForDisconnected(CONNECTION_TIMEOUT); + cdw2.waitForConnected(CONNECTION_TIMEOUT); + Assert.assertSame(zk2.getState(),States.CONNECTED); + } finally { + zk2.close(); + } + } finally { + zk.close(); + } + } +} + diff --git a/src/java/test/org/apache/zookeeper/test/ClientTest.java b/src/java/test/org/apache/zookeeper/test/ClientTest.java new file mode 100644 index 0000000..9dc4861 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ClientTest.java @@ -0,0 +1,805 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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 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.Assert; +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); + + /** 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, hostPort); + } + + @Test + public void testClientWithWatcherObj() throws IOException, + InterruptedException, KeeperException + { + performClientTest(true, hostPort); + } + + @Test + public void testClientWithIPv6Address() throws IOException, + InterruptedException, KeeperException + { + assumeIPv6Available(); + performClientTest(true, ipv6HostPort); + } + + /** 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(); + zk.testableWaitForShutdown(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); + Assert.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); + Assert.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()); + zk.create("/acltest", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.close(); + zk = createClient(); + zk.addAuthInfo("digest", "ben:passwd2".getBytes()); + try { + zk.getData("/acltest", false, new Stat()); + Assert.fail("Should have received a permission error"); + } catch (KeeperException e) { + Assert.assertEquals(Code.NOAUTH, e.code()); + } + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + zk.getData("/acltest", false, new Stat()); + zk.setACL("/acltest", Ids.OPEN_ACL_UNSAFE, -1); + zk.close(); + zk = createClient(); + zk.getData("/acltest", false, new Stat()); + List acls = zk.getACL("/acltest", new Stat()); + Assert.assertEquals(1, acls.size()); + Assert.assertEquals(Ids.OPEN_ACL_UNSAFE, acls); + + // The stat parameter should be optional. + acls = zk.getACL("/acltest", null); + Assert.assertEquals(1, acls.size()); + Assert.assertEquals(Ids.OPEN_ACL_UNSAFE, acls); + + zk.close(); + } 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++) { + Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + } + for (int i = 0; i < watchers.length; i++) { + Assert.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); + Assert.assertEquals("/foo-" + i, event.getPath()); + Assert.assertEquals(EventType.NodeDataChanged, event.getType()); + Assert.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 + Assert.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++) { + Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + Assert.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); + Assert.assertEquals("/foo-" + i, event.getPath()); + Assert.assertEquals(EventType.NodeDataChanged, event.getType()); + Assert.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 + Assert.assertEquals(0, watchers[i].events.size()); + } + + // + // test get/exists with two sets of watchers + // + for (int i = 0; i < watchers.length; i++) { + Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + Assert.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); + Assert.assertEquals("/foo-" + i, event.getPath()); + Assert.assertEquals(EventType.NodeDataChanged, event.getType()); + Assert.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 + Assert.assertEquals(0, watchers[i].events.size()); + + // watchers2 + WatchedEvent event2 = + watchers2[i].events.poll(10, TimeUnit.SECONDS); + Assert.assertEquals("/foo-" + i, event2.getPath()); + Assert.assertEquals(EventType.NodeDataChanged, event2.getType()); + Assert.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 + Assert.assertEquals(0, watchers2[i].events.size()); + } + + } finally { + if (zk != null) { + zk.close(); + } + } + } + + private void performClientTest(boolean withWatcherObj, String hostPort) + 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); + Assert.fail("Should have gotten BadVersion exception"); + } catch(KeeperException.BadVersionException e) { + // expected that + } catch (KeeperException e) { + Assert.fail("Should have gotten BadVersion exception"); + } + LOG.info("Before delete /benwashere"); + zk.delete("/benwashere", 0); + LOG.info("After delete /benwashere"); + zk.close(); + //LOG.info("Closed client: " + zk.describeCNXN()); + Thread.sleep(2000); + + zk = createClient(watcher, hostPort); + //LOG.info("Created a new client: " + zk.describeCNXN()); + LOG.info("Before delete /"); + + try { + zk.delete("/", -1); + Assert.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); + Assert.assertEquals(1, children.size()); + Assert.assertEquals("ben", children.get(0)); + List children2 = zk.getChildren("/pat", false, null); + Assert.assertEquals(children, children2); + String value = new String(zk.getData("/pat/ben", false, stat)); + Assert.assertEquals("Ben was here", value); + // Test stat and watch of non existent node + + try { + if (withWatcherObj) { + Assert.assertEquals(null, zk.exists("/frog", watcher)); + } else { + Assert.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); + Assert.assertEquals("/frog", event.getPath()); + Assert.assertEquals(EventType.NodeCreated, event.getType()); + Assert.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); + Assert.assertEquals(10, children.size()); + for (int i = 0; i < 10; i++) { + final String name = children.get(i); + Assert.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); + } + Assert.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); + Assert.assertEquals("/pat/ben", event.getPath()); + Assert.assertEquals(EventType.NodeChildrenChanged, event.getType()); + Assert.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); + Assert.assertEquals("/pat/ben/" + name, event.getPath()); + Assert.assertEquals(EventType.NodeDataChanged, event.getType()); + Assert.assertEquals(KeeperState.SyncConnected, event.getState()); + event = watcher.events.poll(10, TimeUnit.SECONDS); + Assert.assertEquals("/pat/ben/" + name, event.getPath()); + Assert.assertEquals(EventType.NodeDeleted, event.getType()); + Assert.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); + Assert.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); + Assert.assertEquals(1, children.size()); + Assert.assertEquals(file + "0000000000", children.get(0)); + + zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + children = zk.getChildren(path, false); + Assert.assertEquals(2, children.size()); + Assert.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); + Assert.assertEquals(3, children.size()); + Assert.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); + Assert.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); + Assert.assertEquals(children.size(), 2); + String child1 = children.get(0); + String child2 = children.get(1); + int compareResult = child1.compareTo(child2); + Assert.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)); + Assert.assertEquals(child1data, "0"); + Assert.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; + } + Assert.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 Assert.fails if not sequential + try { + zk.create("/createseqpar/", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + Assert.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); + Assert.assertTrue(false); + } catch(IllegalArgumentException be) { + // catch this. + } + try { + zk.create("/createseqpar/./", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + Assert.assertTrue(false); + } catch(IllegalArgumentException be) { + // catch this. + } + try { + zk.create("/createseqpar/../", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + Assert.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); + Assert.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); + } + +// private void notestConnections() +// throws IOException, InterruptedException, KeeperException +// { +// ZooKeeper zk; +// for(int i = 0; i < 2000; i++) { +// if (i % 100 == 0) { +// LOG.info("Testing " + i + " connections"); +// } +// // We want to make sure socket descriptors are going away +// zk = new ZooKeeper(hostPort, 30000, this); +// zk.getData("/", false, new Stat()); +// zk.close(); +// } +// } + + @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); + Assert.fail("Should have received a not equals message"); + } catch (KeeperException e) { + Assert.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(); + zk.close(); + // 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 Assert.failure + zk.testableWaitForShutdown(CONNECTION_TIMEOUT); + } + } catch (Throwable t) { + LOG.error("test Assert.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() == false) { + 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); + Assert.assertTrue(threads[i].current == threads[i].count); + } + + // if this Assert.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,Long.valueOf(currentCount),Long.valueOf(initialFdCount)); + } else { + LOG.info(logmsg,Long.valueOf(currentCount),Long.valueOf(initialFdCount)); + } + } + + + /** + * We create a perfectly valid 'exists' request, except that the opcode is wrong. + * @return + * @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); + + Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue()); + + // Sending a nonexisting opcode should cause the server to disconnect + Assert.assertTrue("failed to disconnect", + clientDisconnected.await(5000, TimeUnit.MILLISECONDS)); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java new file mode 100644 index 0000000..a169b7b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java @@ -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. + */ + +package org.apache.zookeeper.test; + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.client.ConnectStringParser; +import org.junit.Assert; +import org.junit.Test; + +public class ConnectStringParserTest extends ZKTestCase{ + + @Test + public void testSingleServerChrootPath(){ + String chrootPath = "/hallo/welt"; + String servers = "10.10.10.1"; + 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)); + } + + @Test + public void testParseServersWithoutPort(){ + String servers = "10.10.10.1,10.10.10.2"; + ConnectStringParser parser = new ConnectStringParser(servers); + + Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostName()); + Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostName()); + } + + @Test + public void testParseServersWithPort(){ + String servers = "10.10.10.1:112,10.10.10.2:110"; + ConnectStringParser parser = new ConnectStringParser(servers); + + Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostName()); + Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostName()); + + Assert.assertEquals(112, parser.getServerAddresses().get(0).getPort()); + Assert.assertEquals(110, parser.getServerAddresses().get(1).getPort()); + } + + private void assertChrootPath(String expected, ConnectStringParser parser){ + Assert.assertEquals(expected, parser.getChrootPath()); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/CreateModeTest.java b/src/java/test/org/apache/zookeeper/test/CreateModeTest.java new file mode 100644 index 0000000..a3b838a --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/CreateModeTest.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.test; + +import java.util.EnumSet; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.KeeperException.Code; +import org.junit.Assert; +import org.junit.Test; + +public class CreateModeTest extends ZKTestCase { + + @Test + public void testBasicCreateMode() { + CreateMode cm = CreateMode.PERSISTENT; + Assert.assertEquals(cm.toFlag(), 0); + Assert.assertFalse(cm.isEphemeral()); + Assert.assertFalse(cm.isSequential()); + + cm = CreateMode.EPHEMERAL; + Assert.assertEquals(cm.toFlag(), 1); + Assert.assertTrue(cm.isEphemeral()); + Assert.assertFalse(cm.isSequential()); + + cm = CreateMode.PERSISTENT_SEQUENTIAL; + Assert.assertEquals(cm.toFlag(), 2); + Assert.assertFalse(cm.isEphemeral()); + Assert.assertTrue(cm.isSequential()); + + cm = CreateMode.EPHEMERAL_SEQUENTIAL; + Assert.assertEquals(cm.toFlag(), 3); + Assert.assertTrue(cm.isEphemeral()); + Assert.assertTrue(cm.isSequential()); + } + + @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) { + Assert.assertEquals(cm, CreateMode.fromFlag( cm.toFlag() ) ); + } + } + + @Test + public void testInvalidFlagConversion() throws KeeperException { + try { + CreateMode cm = CreateMode.fromFlag(99); + Assert.fail("Shouldn't be able to convert 99 to a CreateMode."); + } catch(KeeperException ke) { + Assert.assertEquals(Code.BADARGUMENTS, ke.code()); + } + + try { + CreateMode cm = CreateMode.fromFlag(-1); + Assert.fail("Shouldn't be able to convert -1 to a CreateMode."); + } catch(KeeperException ke) { + Assert.assertEquals(Code.BADARGUMENTS, ke.code()); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/DisconnectableZooKeeper.java b/src/java/test/org/apache/zookeeper/test/DisconnectableZooKeeper.java new file mode 100644 index 0000000..619bdc6 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/DisconnectableZooKeeper.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 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/src/java/test/org/apache/zookeeper/test/DisconnectedWatcherTest.java b/src/java/test/org/apache/zookeeper/test/DisconnectedWatcherTest.java new file mode 100644 index 0000000..763ad9b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/DisconnectedWatcherTest.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.test; + +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Assert; +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"); + } + } + } + } + + // @see jira issue ZOOKEEPER-961 + + @Test + public void testChildWatcherAutoResetWithChroot() throws Exception { + ZooKeeper zk1 = createClient(); + + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk2 = createClient(watcher, 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 = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/", e.getPath()); + + MyWatcher childWatcher = new MyWatcher(); + zk2.getChildren("/", childWatcher); + + stopServer(); + watcher.waitForDisconnected(3000); + startServer(); + watcher.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); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/", e.getPath()); + } + + @Test + public void testDefaultWatcherAutoResetWithChroot() throws Exception { + ZooKeeper zk1 = createClient(); + + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk2 = createClient(watcher, 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 = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/", e.getPath()); + + zk2.getChildren("/", true ); + + stopServer(); + watcher.waitForDisconnected(3000); + startServer(); + watcher.waitForConnected(3000); + + // this should trigger the watch + zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/", e.getPath()); + } + + @Test + public void testDeepChildWatcherAutoResetWithChroot() throws Exception { + ZooKeeper zk1 = createClient(); + + 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); + + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk2 = createClient(watcher, 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 = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/are", e.getPath()); + + MyWatcher childWatcher = new MyWatcher(); + zk2.getChildren("/are", childWatcher); + + stopServer(); + watcher.waitForDisconnected(3000); + startServer(); + watcher.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); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.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 = 600000) + public void testManyChildWatchersAutoReset() throws Exception { + ZooKeeper zk1 = createClient(); + + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk2 = createClient(watcher); + + // 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(); + watcher.waitForDisconnected(30000); + startServer(); + watcher.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); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.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); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeCreated, e.getType()); + Assert.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); + Assert.assertNotNull(e); + Assert.assertEquals(EventType.NodeDataChanged, e.getType()); + Assert.assertEquals(path, e.getPath()); + } + + i++; + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/EventTypeTest.java b/src/java/test/org/apache/zookeeper/test/EventTypeTest.java new file mode 100644 index 0000000..e6a9826 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/EventTypeTest.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.test; + +import java.util.EnumSet; + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.junit.Assert; +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) { + Assert.assertEquals(et, EventType.fromInt( et.getIntValue() ) ); + } + } + + @Test + public void testInvalidIntConversion() { + try { + EventType et = EventType.fromInt(324242); + Assert.fail("Was able to create an invalid EventType via an integer"); + } catch(RuntimeException re) { + // we're good. + } + + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FLENewEpochTest.java b/src/java/test/org/apache/zookeeper/test/FLENewEpochTest.java new file mode 100644 index 0000000..1731e6e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FLENewEpochTest.java @@ -0,0 +1,192 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.concurrent.Semaphore; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +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++) { + ((FastLeaderElection) 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){ + Assert.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 { + + FastLeaderElection le[] = new FastLeaderElection[count]; + + LOG.info("TestLE: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + + for(int i = 1; i < le.length; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2); + peer.startLeaderElection(); + LEThread thread = new LEThread(peer, i); + thread.start(); + threads.add(thread); + } + if(!start0.tryAcquire(4000, java.util.concurrent.TimeUnit.MILLISECONDS)) + Assert.fail("First leader election failed"); + + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 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()) { + Assert.fail("Threads didn't join"); + } + + } + } + } diff --git a/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java b/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.java new file mode 100644 index 0000000..0ecac6e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FLEPredicateTest.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 java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; + +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumCnxManager; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.junit.Assert; +import org.junit.Test; + +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, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + } + + /* + * Creating peer. + */ + try{ + File tmpDir = ClientBase.createTmpDir(); + QuorumPeer peer = new QuorumPeer(peers, tmpDir, tmpDir, + PortAssignment.unique(), 3, 0, 1000, 2, 2); + + MockFLE mock = new MockFLE(peer); + + /* + * Lower epoch must return false + */ + + Assert.assertFalse (mock.predicate(4L, 0L, 0L, 3L, 0L, 2L)); + + /* + * Later epoch + */ + Assert.assertTrue (mock.predicate(0L, 0L, 1L, 1L, 0L, 0L)); + + /* + * Higher zxid + */ + Assert.assertTrue(mock.predicate(0L, 1L, 0L, 1L, 0L, 0L)); + + /* + * Higher id + */ + Assert.assertTrue(mock.predicate(1L, 1L, 0L, 0L, 1L, 0L)); + } catch (IOException e) { + LOG.error("Exception while creating quorum peer", e); + Assert.fail("Exception while creating quorum peer"); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FLERestartTest.java b/src/java/test/org/apache/zookeeper/test/FLERestartTest.java new file mode 100644 index 0000000..a7cecf6 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FLERestartTest.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.test; + +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Random; +import java.util.concurrent.Semaphore; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class FLERestartTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(FLETest.class); + + 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; + // int baseport; + // int baseLEport; + HashMap peers; + ArrayList restartThreads; + HashMap > voteMap; + File tmpdir[]; + int port[]; + int successCount; + Semaphore finish; + + volatile Vote votes[]; + volatile boolean leaderDies; + volatile long leader = -1; + //volatile int round = 1; + Random rand = new Random(); + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap(count); + restartThreads = new ArrayList(count); + voteMap = new HashMap >(); + votes = new Vote[count]; + tmpdir = new File[count]; + port = new int[count]; + successCount = 0; + finish = new Semaphore(0); + } + + @After + public void tearDown() throws Exception { + for(int i = 0; i < restartThreads.size(); i++) { + ((FastLeaderElection) 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 " + i + " got a null vote"); + 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); + ((FastLeaderElection) restartThreads.get(i).peer.getElectionAlg()).shutdown(); + + peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 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 { + + FastLeaderElection le[] = new FastLeaderElection[count]; + leaderDies = true; + boolean allowOneBadLeader = leaderDies; + + LOG.info("TestLE: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + 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); + 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()) { + Assert.fail("Threads didn't join"); + } + + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FLETest.java b/src/java/test/org/apache/zookeeper/test/FLETest.java new file mode 100644 index 0000000..6281a57 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FLETest.java @@ -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. + */ + +package org.apache.zookeeper.test; + +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class FLETest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(FLETest.class); + 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; + HashMap peers; + ArrayList threads; + HashMap > voteMap; + File tmpdir[]; + int port[]; + int successCount; + Object finalObj; + + volatile Vote votes[]; + volatile boolean leaderDies; + 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; + finalObj = new Object(); + 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); + } + } + + class LEThread extends Thread { + int i; + QuorumPeer peer; + //int peerRound = 1; + + LEThread(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 " + i + " got a null vote"); + 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; + + /* + * Get the current value of the logical clock for this peer. + */ + int lc = (int) ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock(); + + if (v.getId() == i) { + /* + * A leader executes this part of the code. If it is the first leader to be + * elected, then it Assert.fails right after. Otherwise, it waits until it has enough + * followers supporting it. + */ + LOG.info("I'm the leader: " + i); + synchronized(FLETest.this) { + if (leaderDies) { + LOG.info("Leader " + i + " dying"); + leaderDies = false; + ((FastLeaderElection) peer.getElectionAlg()).shutdown(); + leader = -1; + LOG.info("Leader " + i + " dead"); + + //round++; + FLETest.this.notifyAll(); + + break; + + } else { + synchronized(voteMap){ + if(voteMap.get(lc) == null) + voteMap.put(lc, new HashSet()); + HashSet hs = voteMap.get(lc); + hs.add(new TestVote(i, v.getId())); + + if(countVotes(hs, v.getId()) > (count/2)){ + leader = i; + LOG.info("Got majority: " + i); + } else { + voteMap.wait(3000); + LOG.info("Notified or expired: " + i); + hs = voteMap.get(lc); + if(countVotes(hs, v.getId()) > (count/2)){ + leader = i; + LOG.info("Got majority: " + i); + } else { + //round++; + } + } + } + FLETest.this.notifyAll(); + + if(leader == i){ + synchronized(finalObj){ + successCount++; + joinedThreads.add((long)i); + if(successCount > (count/2)) finalObj.notify(); + } + + break; + } + } + } + } else { + /* + * Followers execute this part. They first add their vote to voteMap, and then + * they wait for bounded amount of time. A leader notifies followers through the + * FLETest.this object. + * + * Note that I can get FLETest.this, and then voteMap before adding the vote of + * a follower, otherwise a follower would be blocked out until the leader notifies + * or leaves the synchronized block on FLEtest.this. + */ + + + LOG.info("Logical clock " + ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock()); + synchronized(voteMap){ + LOG.info("Voting on " + votes[i].getId() + ", round " + ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock()); + if(voteMap.get(lc) == null) + voteMap.put(lc, new HashSet()); + HashSet hs = voteMap.get(lc); + hs.add(new TestVote(i, votes[i].getId())); + if(countVotes(hs, votes[i].getId()) > (count/2)){ + LOG.info("Logical clock: " + lc + ", " + votes[i].getId()); + voteMap.notify(); + } + } + + /* + * In this part a follower waits until the leader notifies it, and remove its + * vote if the leader takes too long to respond. + */ + synchronized(FLETest.this){ + if (leader != votes[i].getId()) FLETest.this.wait(3000); + + LOG.info("The leader: " + leader + " and my vote " + votes[i].getId()); + synchronized(voteMap){ + if (leader == votes[i].getId()) { + synchronized(finalObj){ + successCount++; + joinedThreads.add((long)i); + if(successCount > (count/2)) finalObj.notify(); + } + break; + } else { + HashSet hs = voteMap.get(lc); + TestVote toRemove = null; + for(TestVote tv : hs){ + if(v.getId() == i){ + toRemove = tv; + break; + } + } + hs.remove(toRemove); + } + } + } + } + /* + * Add some randomness to the execution. + */ + Thread.sleep(rand.nextInt(500)); + peer.setCurrentVote(new Vote(peer.getId(), 0)); + } + LOG.debug("Thread " + i + " votes " + v); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Test + public void testLE() throws Exception { + + FastLeaderElection le[] = new FastLeaderElection[count]; + leaderDies = true; + boolean allowOneBadLeader = leaderDies; + + LOG.info("TestLE: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + + for(int i = 0; i < le.length; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], + port[i], 3, i, 1000, 2, 2); + peer.startLeaderElection(); + LEThread thread = new LEThread(peer, i); + thread.start(); + threads.add(thread); + } + LOG.info("Started threads " + getTestName()); + + + int waitCounter = 0; + synchronized(finalObj){ + while((successCount <= count/2) && (waitCounter < 50)){ + finalObj.wait(2000); + waitCounter++; + } + } + + /* + * 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){ + Assert.fail("Fewer than a a majority has joined"); + } + + synchronized(finalObj){ + if(!joinedThreads.contains(leader)){ + Assert.fail("Leader hasn't joined: " + leader); + } + } + } + + /* + * Class to verify of the thread has become a follower + */ + class VerifyState extends Thread { + volatile private boolean success = false; + 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++) { + peers.put(Long.valueOf(sid), + new QuorumServer(sid, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + 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); + 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); + Assert.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); + LOG.info("Starting peer " + peer.getId()); + peer.start(); + peerList.add(sid, peer); + v1 = new VerifyState(peer); + v1.start(); + v1.join(waitTime); + if (v1.isAlive()) { + Assert.fail("Peer " + peer.getId() + " failed to join the cluster " + + "within " + waitTime + " ms"); + } else if (!v1.isSuccess()) { + Assert.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, "0.0.0.0", PortAssignment.unique(), + PortAssignment.unique(), null)); + 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); + 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); + Assert.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); + LOG.info("Starting peer " + peer.getId()); + peer.start(); + peerList.add(sid, peer); + v1 = new VerifyState(peer); + v1.start(); + v1.join(waitTime); + if (v1.isAlive()) { + Assert.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(); + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FLEZeroWeightTest.java b/src/java/test/org/apache/zookeeper/test/FLEZeroWeightTest.java new file mode 100644 index 0000000..e8a8cf7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FLEZeroWeightTest.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.test; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Properties; +import java.util.Random; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class FLEZeroWeightTest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(HierarchicalQuorumTest.class); + + Properties qp; + + int count; + HashMap peers; + ArrayList threads; + File tmpdir[]; + int port[]; + Object finalObj; + + volatile Vote votes[]; + volatile boolean leaderDies; + volatile long leader = -1; + Random rand = new Random(); + + + @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]; + finalObj = new Object(); + + 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 " + i + " got a null vote"); + 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 " + i + " votes " + v); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Test + public void testZeroWeightQuorum() throws Exception { + FastLeaderElection le[] = new FastLeaderElection[count]; + + LOG.info("TestZeroWeightQuorum: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "0.0.0.0", PortAssignment.unique(), PortAssignment.unique(), null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + + for(int i = 0; i < le.length; i++) { + QuorumHierarchical hq = new QuorumHierarchical(qp); + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 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()) { + Assert.fail("Threads didn't join"); + } else { + if(threads.get(i).fail) + Assert.fail("Elected zero-weight server"); + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java b/src/java/test/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java new file mode 100644 index 0000000..acd05d6 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java @@ -0,0 +1,607 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.HashSet; +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 org.apache.log4j.Logger; +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.Assert; +import org.junit.Test; + + +public class FollowerResyncConcurrencyTest extends ZKTestCase { + private static final Logger LOG = Logger.getLogger(FollowerResyncConcurrencyTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + private volatile int counter = 0; + private volatile int errors = 0; + + /** + * 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); + Assert.assertTrue("Waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + Assert.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); + Assert.assertTrue("Waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("Waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.start(1); + qu.start(2); + Assert.assertTrue("Waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("Waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.start(3); + Assert.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 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); + + 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); + Thread mytestfooThread = new Thread(new Runnable() { + + @Override + public void run() { + for(int i = 0; i < 3000; i++) { + 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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter == 16200){ + sem.release(); + } + } + }, null); + if(i%10==0){ + try { + Thread.sleep(100); + } catch (Exception e) { + + } + } + } + + } + }); + + for(int i = 0; i < 13000; i++) { + 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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter == 16200){ + sem.release(); + } + } + }, null); + + if(i == 5000){ + qu.shutdown(index); + LOG.info("Shutting down s1"); + } + if(i == 12000){ + //Restart off of snap, then get some txns for a log, then shut down + mytestfooThread.start(); + qu.restart(index); + Thread.sleep(300); + qu.shutdown(index); + Thread.sleep(300); + 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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter == 16200){ + sem.release(); + } + } + }, null); + } + } + + // 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"); + } + Thread.sleep(1000); + + 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 <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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter > 7300){ + sem.release(); + } + } + }, null); + + 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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter > 7300){ + sem.release(); + } + } + }, null); + + 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) { + counter++; + if (rc != 0) { + errors++; + } + if(counter > 7300){ + sem.release(); + } + } + }, null); + } + 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"); + } + + Thread.sleep(1000); + // 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; + } + + 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) { + 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) { + assertTrue("Should have same set of sessions in both servers, did not expect: " + l, sessionsNotRestarted.contains(l)); + HashSet ephemerals = restarted.getEphemerals(l); + HashSet cleanEphemerals = clean.getEphemerals(l); + for(Object o : cleanEphemerals) { + if(!ephemerals.contains(o)) { + LOG.info("Restarted follower doesn't contain ephemeral " + o); + } + } + HashSet leadEphemerals = lead.getEphemerals(l); + for(Object o : leadEphemerals) { + if(!cleanEphemerals.contains(o)) { + LOG.info("Follower doesn't contain ephemeral from leader " + o); + } + } + 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(); + } + + 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(); + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/FollowerTest.java b/src/java/test/org/apache/zookeeper/test/FollowerTest.java new file mode 100644 index 0000000..fa6b4a3 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FollowerTest.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.test; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +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.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FollowerTest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(FollowerTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + volatile int counter = 0; + volatile int errors = 0; + + /** + * See ZOOKEEPER-790 for details + * */ + @Test + public void testFollowersStartAfterLeader() throws Exception { + QuorumUtil 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); + + // break the quorum + qu.shutdown(index); + + // Wait until we disconnect to proceed + watcher.waitForDisconnected(CONNECTION_TIMEOUT); + + // try to reestablish the quorum + qu.start(index); + + try{ + watcher.waitForConnected(30000); + } catch(TimeoutException e) { + Assert.fail("client could not connect to reestablished quorum: giving up after 30+ seconds."); + } + + zk.close(); + + qu.tearDown(); + } + + // 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 { + QuorumUtil 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); + + List results = new ArrayList(); + + results = 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(); + + qu.tearDown(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java b/src/java/test/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java new file mode 100644 index 0000000..49d90f7 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FourLetterWordsQuorumTest.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 java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.TestableZooKeeper; +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import org.junit.Assert; +import org.junit.Test; + +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); + + 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); + + 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, "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 + { + for(HostPort hpobj: parseHostPortList(hp)) { + String resp = send4LetterWord(hpobj.host, hpobj.port, cmd); + LOG.info("cmd " + cmd + " expected " + expected + " got " + resp); + if (cmd.equals("dump")) { + Assert.assertTrue(resp.contains(expected) + || resp.contains("Sessions with Ephemerals")); + } else { + Assert.assertTrue(resp.contains(expected)); + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/FourLetterWordsTest.java b/src/java/test/org/apache/zookeeper/test/FourLetterWordsTest.java new file mode 100644 index 0000000..bc8d071 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FourLetterWordsTest.java @@ -0,0 +1,238 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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 static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import org.junit.Assert; +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 = new Timeout(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"); + + TestableZooKeeper zk = createClient(); + String sid = getHexSessionId(zk.getSessionId()); + + verify("stat", "queued"); + verify("srvr", "Outstanding"); + verify("cons", sid); + verify("dump", sid); + + 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); + 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("mntr", "fsync_threshold_exceed_count"); + verify("stat", "Connections"); + verify("srvr", "Connections"); + } + + private String sendRequest(String cmd) throws IOException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd); + } + private String sendRequest(String cmd, int timeout) throws IOException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd, timeout); + } + + private void verify(String cmd, String expected) throws IOException { + String resp = sendRequest(cmd); + LOG.info("cmd " + cmd + " expected " + expected + " got " + resp); + Assert.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(); + Assert.assertTrue(Pattern.matches("^.*\\s\\d+\\.\\d+\\.\\d+-.*$", line)); + Assert.assertTrue(Pattern.matches("^Clients:$", in.readLine())); + + int count = 0; + while ((line = in.readLine()).length() > 0) { + count++; + Assert.assertTrue(Pattern.matches("^ /.*:\\d+\\[\\d+\\]\\(queued=\\d+,recved=\\d+,sent=\\d+\\)$", line)); + } + // ensure at least the two clients we created are accounted for + Assert.assertTrue(count >= 2); + + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Latency min/avg/max: \\d+/\\d+/\\d+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Received: \\d+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Sent: \\d+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Connections: \\d+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Outstanding: \\d+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Zxid: 0x[\\da-fA-F]+$", line)); + line = in.readLine(); + Assert.assertTrue(Pattern.matches("^Mode: .*$", line)); + line = in.readLine(); + Assert.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++; + Assert.assertTrue(line, Pattern.matches("^ /.*:\\d+\\[\\d+\\]\\(queued=\\d+,recved=\\d+,sent=\\d+.*\\)$", line)); + } + // ensure at least the two clients we created are accounted for + Assert.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); + Assert.assertTrue(resp.contains("rw")); + } + + @Test + public void testSetTraceMask() throws Exception { + String gtmkResp = sendRequest("gtmk"); + Assert.assertNotNull(gtmkResp); + gtmkResp = gtmkResp.trim(); + Assert.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/src/java/test/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java b/src/java/test/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java new file mode 100644 index 0000000..515cbdc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java @@ -0,0 +1,253 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.TestableZooKeeper; +import org.apache.zookeeper.server.ServerCnxn; +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FourLetterWordsWhiteListTest extends ClientBase { + protected static final Logger LOG = + LoggerFactory.getLogger(FourLetterWordsTest.class); + + @Rule + public Timeout timeout = new Timeout(30000); + + /* + * 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(); + ServerCnxn.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(); + ServerCnxn.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("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(); + ServerCnxn.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(); + ServerCnxn.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(); + ServerCnxn.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "*"); + startServer(); + verifyAllCommandsSuccess(); + } + + @Test(timeout=30000) + public void testFourLetterWordsEnableAllCommandsThroughExplicitList() throws Exception { + stopServer(); + ServerCnxn.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", + "ruok, envi, conf, stat, srvr, cons, dump," + + "wchs, wchp, wchc, srst, crst, " + + "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); + + 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); + 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("mntr", "fsync_threshold_exceed_count"); + verifyFuzzyMatch("stat", "Connections"); + verifyFuzzyMatch("srvr", "Connections"); + } + + private void verifyAllCommandsFail() throws Exception { + verifyExactMatch("ruok", generateExpectedMessage("ruok")); + verifyExactMatch("conf", generateExpectedMessage("conf")); + verifyExactMatch("cons", generateExpectedMessage("cons")); + verifyExactMatch("crst", generateExpectedMessage("crst")); + 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 void verifyFuzzyMatch(String cmd, String expected) throws IOException { + String resp = sendRequest(cmd); + LOG.info("cmd " + cmd + " expected " + expected + " got " + resp); + Assert.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 { + String resp = sendRequest(cmd); + LOG.info("cmd " + cmd + " expected an exact match of " + expected + "; got " + resp); + Assert.assertTrue(resp.trim().equals(expected)); + } + + private String sendRequest(String cmd) throws IOException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd); + } + + private String sendRequest(String cmd, int timeout) throws IOException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd, timeout); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/GetChildren2Test.java b/src/java/test/org/apache/zookeeper/test/GetChildren2Test.java new file mode 100644 index 0000000..e41fffe --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/GetChildren2Test.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 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.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid() + 1, stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(1, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.assertEquals(1, stat.getNumChildren()); + Assert.assertEquals(s.size(), stat.getNumChildren()); + + s = zk.getChildren(childname, false, stat); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(0, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); + Assert.assertEquals(childname.length(), stat.getDataLength()); + Assert.assertEquals(0, stat.getNumChildren()); + Assert.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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid() + i + 1, stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(i + 1, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.assertEquals(i + 1, stat.getNumChildren()); + Assert.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); + Assert.assertEquals(c_a.size(), 10); + Assert.assertEquals(c_a, c_b); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/HierarchicalQuorumTest.java b/src/java/test/org/apache/zookeeper/test/HierarchicalQuorumTest.java new file mode 100644 index 0000000..051da13 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/HierarchicalQuorumTest.java @@ -0,0 +1,309 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.jmx.CommonNames; +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.quorum.flexible.QuorumHierarchical; +import org.junit.Assert; +import org.junit.Test; + +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; + + 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(); + + hostPort = "127.0.0.1:" + port1 + + ",127.0.0.1:" + port2 + + ",127.0.0.1:" + port3 + + ",127.0.0.1:" + port4 + + ",127.0.0.1:" + port5; + 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"; + + 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; + HashMap peers = new HashMap(); + peers.put(Long.valueOf(1), new QuorumServer(1, "127.0.0.1", port1 + 1000, leport1 + 1000, null)); + peers.put(Long.valueOf(2), new QuorumServer(2, "127.0.0.1", port2 + 1000, leport2 + 1000, null)); + peers.put(Long.valueOf(3), new QuorumServer(3, "127.0.0.1", port3 + 1000, leport3 + 1000, null)); + peers.put(Long.valueOf(4), new QuorumServer(4, "127.0.0.1", port4 + 1000, leport4 + 1000, + withObservers ? QuorumPeer.LearnerType.OBSERVER + : QuorumPeer.LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, "127.0.0.1", port5 + 1000, leport5 + 1000, + withObservers ? QuorumPeer.LearnerType.OBSERVER + : QuorumPeer.LearnerType.PARTICIPANT)); + + LOG.info("creating QuorumPeer 1 port " + port1); + List qps = new ArrayList(); + QuorumHierarchical hq1 = new QuorumHierarchical(qp); + s1 = new QuorumPeer(peers, s1dir, s1dir, port1, 3, 1, tickTime, initLimit, syncLimit, hq1); + qps.add(s1); + Assert.assertEquals(port1, s1.getClientPort()); + + LOG.info("creating QuorumPeer 2 port " + port2); + QuorumHierarchical hq2 = new QuorumHierarchical(qp); + s2 = new QuorumPeer(peers, s2dir, s2dir, port2, 3, 2, tickTime, initLimit, syncLimit, hq2); + qps.add(s2); + Assert.assertEquals(port2, s2.getClientPort()); + + LOG.info("creating QuorumPeer 3 port " + port3); + QuorumHierarchical hq3 = new QuorumHierarchical(qp); + s3 = new QuorumPeer(peers, s3dir, s3dir, port3, 3, 3, tickTime, initLimit, syncLimit, hq3); + qps.add(s3); + Assert.assertEquals(port3, s3.getClientPort()); + + LOG.info("creating QuorumPeer 4 port " + port4); + QuorumHierarchical hq4 = new QuorumHierarchical(qp); + s4 = new QuorumPeer(peers, s4dir, s4dir, port4, 3, 4, tickTime, initLimit, syncLimit, hq4); + qps.add(s4); + if (withObservers) { + s4.setLearnerType(QuorumPeer.LearnerType.OBSERVER); + } + Assert.assertEquals(port4, s4.getClientPort()); + + LOG.info("creating QuorumPeer 5 port " + port5); + QuorumHierarchical hq5 = new QuorumHierarchical(qp); + s5 = new QuorumPeer(peers, s5dir, s5dir, port5, 3, 5, tickTime, initLimit, syncLimit, hq5); + qps.add(s5); + if (withObservers) { + s5.setLearnerType(QuorumPeer.LearnerType.OBSERVER); + } + Assert.assertEquals(port5, s5.getClientPort()); + + // Observers are currently only compatible with LeaderElection + if (withObservers) { + s1.setElectionType(0); + s2.setElectionType(0); + s3.setElectionType(0); + s4.setElectionType(0); + s5.setElectionType(0); + } + + 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(",")) { + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(hp, + CONNECTION_TIMEOUT)); + LOG.info(hp + " is accepting client connections"); + } + + // 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()])); + verifyElectionTimeTakenJMXAttribute(qps); + } + + @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(",")) { + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(hp, + ClientBase.CONNECTION_TIMEOUT)); + LOG.info(hp + " is no longer accepting client connections"); + } + + 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); + } + + private void verifyElectionTimeTakenJMXAttribute(List peers) + throws Exception { + LOG.info("Verify QuorumPeer#electionTimeTaken jmx bean attribute"); + + for (int i = 1; i <= peers.size(); i++) { + QuorumPeer qp = peers.get(i - 1); + if (qp.getLearnerType() == LearnerType.OBSERVER) { + continue; // Observer don't have electionTimeTaken attribute. + } + Long electionTimeTaken = -1L; + String bean = ""; + if (qp.getPeerState() == ServerState.FOLLOWING) { + bean = String.format( + "%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Follower", + CommonNames.DOMAIN, i, i); + } else if (qp.getPeerState() == ServerState.LEADING) { + bean = String.format( + "%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Leader", + CommonNames.DOMAIN, i, i); + } + electionTimeTaken = (Long) JMXEnv.ensureBeanAttribute(bean, + "ElectionTimeTaken"); + Assert.assertTrue("Wrong electionTimeTaken value!", + electionTimeTaken >= 0); + } + } + + @Test + public void testHierarchicalQuorum() throws Throwable { + cht.runHammer(5, 10); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/IntegrityCheck.java b/src/java/test/org/apache/zookeeper/test/IntegrityCheck.java new file mode 100644 index 0000000..9a01a65 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/IntegrityCheck.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.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.io.IOException; +import java.util.Date; +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.data.Stat; + +public class IntegrityCheck implements Watcher, StatCallback, DataCallback { + private static final Logger LOG = LoggerFactory.getLogger(IntegrityCheck.class); + + ZooKeeper zk; + + HashMap 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 + IOException { + zk = new ZooKeeper(hostPort, 30000, this); + 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(); + } + } + + // watcher callback + public void process(WatchedEvent event) { + if(event.getState()==KeeperState.SyncConnected){ + synchronized(this){ + notifyAll(); + } + } + } + + 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(1); + } + + 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(2); + } + } + + 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 " + string + " expected >= " + + lastString); + errorCount++; + } + lastValue.put(path, (byte[]) ctx); + } + decOutstanding(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java b/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.java new file mode 100644 index 0000000..c5a7b4f --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/InvalidSnapshotTest.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.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.util.concurrent.CountDownLatch; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.LogFormatter; +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.Assert; +import org.junit.Test; + +public class InvalidSnapshotTest extends ZKTestCase implements Watcher { + private final static Logger LOG = LoggerFactory.getLogger(UpgradeTest.class); + private static final String HOSTPORT = + "127.0.0.1:" + PortAssignment.unique(); + + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/data")); + private CountDownLatch startSignal; + + /** + * Verify the LogFormatter by running it on a known file. + */ + @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()}; + 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); + } + + /** + * test the snapshot + * @throws Exception an exception could be expected + */ + @Test + public void testSnapshot() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + 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"); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = new ZooKeeper(HOSTPORT, 20000, this); + try { + // we know this from the data files + // this node is the last node in the snapshot + + Assert.assertTrue(zk.exists("/9/9/8", false) != null); + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(HOSTPORT, + ClientBase.CONNECTION_TIMEOUT)); + + } + + 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/src/java/test/org/apache/zookeeper/test/JMXEnv.java b/src/java/test/org/apache/zookeeper/test/JMXEnv.java new file mode 100644 index 0000000..f67c67c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/JMXEnv.java @@ -0,0 +1,325 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.Arrays; +import java.util.HashSet; +import java.util.Set; + +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 junit.framework.TestCase; + +import org.apache.zookeeper.jmx.CommonNames; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.junit.Assert; +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://"); + cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); + cs.start(); + + JMXServiceURL addr = cs.getAddress(); + + 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 MalformedObjectNameException + */ + 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(CommonNames.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)); + TestCase.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 MalformedObjectNameException + */ + 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()); + } + TestCase.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(CommonNames.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()); + } + TestCase.fail(unexpectedName); + } + } + + public static void dump() throws IOException { + LOG.info("JMXEnv.dump() follows"); + Set beans; + try { + beans = conn().queryNames( + new ObjectName(CommonNames.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. + * + * {@link 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(CommonNames.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); + TestCase.assertEquals("expected " + Arrays.toString(expectedNames), + expectedNames.length, found.size()); + return beans; + } + + /** + * 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("="); + if (names.length > 0 && names[names.length - 1].contains(name)) { + return true; + } + return false; + } + + /** + * 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:{}", new Object[] { expectedName, + expectedAttribute }); + + Set beans; + int nTry = 0; + do { + if (nTry++ > 0) { + Thread.sleep(500); + } + try { + beans = conn().queryNames( + new ObjectName(CommonNames.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:{} {}", new Object[] { expectedName, bean }); + return conn().getAttribute(bean, expectedAttribute); + } + } + } while (nTry < 120); + Assert.fail("Failed to find bean:" + expectedName + ", attribute:" + + expectedAttribute); + return value; + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/KeeperStateTest.java b/src/java/test/org/apache/zookeeper/test/KeeperStateTest.java new file mode 100644 index 0000000..55dc852 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/KeeperStateTest.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 java.util.EnumSet; + +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.junit.Assert; +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) { + Assert.assertEquals(as, KeeperState.fromInt( as.getIntValue() ) ); + } + } + + @Test + public void testInvalidIntConversion() { + try { + KeeperState ks = KeeperState.fromInt(324142); + Assert.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: + Assert.assertTrue(true); + break; + } + } + + /** Verify the enum works (paranoid) */ + @Test + public void testCodeOKInSwitch() { + Code test = Code.OK; + switch (test) { + case OK: + Assert.assertTrue(true); + break; + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/LENonTerminateTest.java b/src/java/test/org/apache/zookeeper/test/LENonTerminateTest.java new file mode 100644 index 0000000..de26698 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/LENonTerminateTest.java @@ -0,0 +1,378 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.File; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.quorum.Election; +import org.apache.zookeeper.server.quorum.FLELostMessageTest; +import org.apache.zookeeper.server.quorum.LeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.Vote; +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.quorum.flexible.QuorumMaj; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LENonTerminateTest extends ZKTestCase { + public class MockLeaderElection extends LeaderElection { + public MockLeaderElection(QuorumPeer self) { + super(self); + } + + /** + * Temporary for 3.3.0 - we want to ensure that a round of voting happens + * before any of the peers update their votes. The easiest way to do that + * is to add a latch that all wait on after counting their votes. + * + * In 3.4.0 we intend to make this class more testable, and therefore + * there should be much less duplicated code. + * + * JMX bean method calls are removed to reduce noise. + */ + public Vote lookForLeader() throws InterruptedException { + self.setCurrentVote(new Vote(self.getId(), + self.getLastLoggedZxid())); + // We are going to look for a leader by casting a vote for ourself + byte requestBytes[] = new byte[4]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + byte responseBytes[] = new byte[28]; + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + /* The current vote for the leader. Initially me! */ + DatagramSocket s = null; + try { + s = new DatagramSocket(); + s.setSoTimeout(200); + } catch (SocketException e1) { + LOG.error("Socket exception when creating socket for leader election", e1); + System.exit(4); + } + DatagramPacket requestPacket = new DatagramPacket(requestBytes, + requestBytes.length); + DatagramPacket responsePacket = new DatagramPacket(responseBytes, + responseBytes.length); + int xid = epochGen.nextInt(); + while (self.isRunning()) { + HashMap votes = + new HashMap(self.getVotingView().size()); + + requestBuffer.clear(); + requestBuffer.putInt(xid); + requestPacket.setLength(4); + HashSet heardFrom = new HashSet(); + for (QuorumServer server : + self.getVotingView().values()) + { + LOG.info("Server address: " + server.addr); + try { + requestPacket.setSocketAddress(server.addr); + } catch (IllegalArgumentException e) { + // Sun doesn't include the address that causes this + // exception to be thrown, so we wrap the exception + // in order to capture this critical detail. + throw new IllegalArgumentException( + "Unable to set socket address on packet, msg:" + + e.getMessage() + " with addr:" + server.addr, + e); + } + + try { + s.send(requestPacket); + responsePacket.setLength(responseBytes.length); + s.receive(responsePacket); + if (responsePacket.getLength() != responseBytes.length) { + LOG.error("Got a short response: " + + responsePacket.getLength()); + continue; + } + responseBuffer.clear(); + int recvedXid = responseBuffer.getInt(); + if (recvedXid != xid) { + LOG.error("Got bad xid: expected " + xid + + " got " + recvedXid); + continue; + } + long peerId = responseBuffer.getLong(); + heardFrom.add(peerId); + //if(server.id != peerId){ + Vote vote = new Vote(responseBuffer.getLong(), + responseBuffer.getLong()); + InetSocketAddress addr = + (InetSocketAddress) responsePacket + .getSocketAddress(); + votes.put(addr, vote); + //} + } catch (IOException e) { + LOG.warn("Ignoring exception while looking for leader", + e); + // Errors are okay, since hosts may be + // down + } + } + + ElectionResult result = countVotes(votes, heardFrom); + + /** + * This is the only difference from LeaderElection - wait for + * this latch on the first time through this method. This ensures + * that the first round of voting happens before setCurrentVote + * is called below. + */ + LOG.info("Waiting for first round of voting to complete"); + latch.countDown(); + Assert.assertTrue("Thread timed out waiting for latch", + latch.await(10000, TimeUnit.MILLISECONDS)); + + // ZOOKEEPER-569: + // If no votes are received for live peers, reset to voting + // for ourselves as otherwise we may hang on to a vote + // for a dead peer + if (result.numValidVotes == 0) { + self.setCurrentVote(new Vote(self.getId(), + self.getLastLoggedZxid())); + } else { + if (result.winner.getId() >= 0) { + self.setCurrentVote(result.vote); + // To do: this doesn't use a quorum verifier + if (result.winningCount > (self.getVotingView().size() / 2)) { + self.setCurrentVote(result.winner); + s.close(); + Vote current = self.getCurrentVote(); + LOG.info("Found leader: my type is: " + self.getLearnerType()); + /* + * We want to make sure we implement the state machine + * correctly. If we are a PARTICIPANT, once a leader + * is elected we can move either to LEADING or + * FOLLOWING. However if we are an OBSERVER, it is an + * error to be elected as a Leader. + */ + if (self.getLearnerType() == LearnerType.OBSERVER) { + if (current.getId() == self.getId()) { + // This should never happen! + LOG.error("OBSERVER elected as leader!"); + Thread.sleep(100); + } + else { + self.setPeerState(ServerState.OBSERVING); + Thread.sleep(100); + return current; + } + } else { + self.setPeerState((current.getId() == self.getId()) + ? ServerState.LEADING: ServerState.FOLLOWING); + if (self.getPeerState() == ServerState.FOLLOWING) { + Thread.sleep(100); + } + return current; + } + } + } + } + Thread.sleep(1000); + } + return null; + } + } + + public class MockQuorumPeer extends QuorumPeer { + public MockQuorumPeer(Map quorumPeers, File snapDir, + File logDir, int clientPort, int electionAlg, + long myid, int tickTime, int initLimit, int syncLimit) + throws IOException + { + super(quorumPeers, snapDir, logDir, electionAlg, + myid,tickTime, initLimit,syncLimit, false, + ServerCnxnFactory.createFactory(clientPort, -1), + new QuorumMaj(countParticipants(quorumPeers))); + } + + protected Election createElectionAlgorithm(int electionAlgorithm){ + LOG.info("Returning mocked leader election"); + return new MockLeaderElection(this); + } + } + + + protected static final Logger LOG = LoggerFactory.getLogger(FLELostMessageTest.class); + + int count; + HashMap peers; + File tmpdir[]; + int port[]; + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap(count); + tmpdir = new File[count]; + port = new int[count]; + } + + static final CountDownLatch latch = new CountDownLatch(2); + static final CountDownLatch mockLatch = new CountDownLatch(1); + + 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(){ + try{ + Vote v = null; + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election: " + i); + v = peer.getElectionAlg().lookForLeader(); + + if (v == null){ + Assert.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()); + } catch (Exception e) { + e.printStackTrace(); + } + LOG.info("Joining"); + } + } + + /** + * This tests ZK-569. + * With three peers A, B and C, the following could happen: + * 1. Round 1, A,B and C all vote for themselves + * 2. Round 2, C dies, A and B vote for C + * 3. Because C has died, votes for it are ignored, but A and B never + * reset their votes. Hence LE never terminates. ZK-569 fixes this by + * resetting votes to themselves if the set of votes for live peers is null. + */ + @Test + public void testNonTermination() throws Exception { + LOG.info("TestNonTermination: " + getTestName()+ ", " + count); + for(int i = 0; i < count; i++) { + int clientport = PortAssignment.unique(); + peers.put(Long.valueOf(i), + new QuorumServer(i, "127.0.0.1", clientport, + PortAssignment.unique(), null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = clientport; + } + + /* + * peer1 and peer2 are A and B in the above example. + */ + QuorumPeer peer1 = new MockQuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 0, 0, 2, 2, 2); + peer1.startLeaderElection(); + LEThread thread1 = new LEThread(peer1, 0); + + QuorumPeer peer2 = new MockQuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 0, 1, 2, 2, 2); + peer2.startLeaderElection(); + LEThread thread2 = new LEThread(peer2, 1); + + /* + * Start mock server. + */ + Thread thread3 = new Thread() { + public void run() { + try { + mockServer(); + } catch (Exception e) { + LOG.error("exception", e); + Assert.fail("Exception when running mocked server " + e); + } + } + }; + + thread3.start(); + Assert.assertTrue("mockServer did not start in 5s", + mockLatch.await(5000, TimeUnit.MILLISECONDS)); + thread1.start(); + thread2.start(); + /* + * Occasionally seen false negatives with a 5s timeout. + */ + thread1.join(15000); + thread2.join(15000); + thread3.join(15000); + if (thread1.isAlive() || thread2.isAlive() || thread3.isAlive()) { + Assert.fail("Threads didn't join"); + } + } + + /** + * MockServer plays the role of peer C. Respond to two requests for votes + * with vote for self and then Assert.fail. + */ + void mockServer() throws InterruptedException, IOException { + byte b[] = new byte[36]; + ByteBuffer responseBuffer = ByteBuffer.wrap(b); + DatagramPacket packet = new DatagramPacket(b, b.length); + QuorumServer server = peers.get(Long.valueOf(2)); + DatagramSocket udpSocket = new DatagramSocket(server.addr.getPort()); + LOG.info("In MockServer"); + mockLatch.countDown(); + Vote current = new Vote(2, 1); + for (int i=0;i<2;++i) { + udpSocket.receive(packet); + responseBuffer.rewind(); + LOG.info("Received " + responseBuffer.getInt() + " " + responseBuffer.getLong() + " " + responseBuffer.getLong()); + LOG.info("From " + packet.getSocketAddress()); + responseBuffer.clear(); + responseBuffer.getInt(); // Skip the xid + responseBuffer.putLong(2); + + responseBuffer.putLong(current.getId()); + responseBuffer.putLong(current.getZxid()); + packet.setData(b); + udpSocket.send(packet); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/LETest.java b/src/java/test/org/apache/zookeeper/test/LETest.java new file mode 100644 index 0000000..0fd3430 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/LETest.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 java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Random; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.LeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.Vote; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.junit.Assert; +import org.junit.Test; + +public class LETest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(LETest.class); + volatile Vote votes[]; + volatile boolean leaderDies; + volatile long leader = -1; + Random rand = new Random(); + class LEThread extends Thread { + LeaderElection le; + int i; + QuorumPeer peer; + LEThread(LeaderElection le, QuorumPeer peer, int i) { + this.le = le; + this.i = i; + this.peer = peer; + } + public void run() { + try { + Vote v = null; + while(true) { + v = le.lookForLeader(); + votes[i] = v; + if (v.getId() == i) { + synchronized(LETest.this) { + if (leaderDies) { + leaderDies = false; + peer.stopLeaderElection(); + LOG.info("Leader " + i + " dying"); + leader = -2; + } else { + leader = i; + } + LETest.this.notifyAll(); + } + break; + } + synchronized(LETest.this) { + if (leader == -1) { + LETest.this.wait(); + } + if (leader == v.getId()) { + break; + } + } + Thread.sleep(rand.nextInt(1000)); + peer.setCurrentVote(new Vote(peer.getId(), 0)); + } + LOG.info("Thread " + i + " votes " + v); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Test + public void testLE() throws Exception { + int count = 30; + HashMap peers = new HashMap(count); + ArrayList threads = new ArrayList(count); + File tmpdir[] = new File[count]; + int port[] = new int[count]; + votes = new Vote[count]; + for(int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), + new QuorumServer(i, "127.0.0.1", PortAssignment.unique(), 0, null)); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + LeaderElection le[] = new LeaderElection[count]; + leaderDies = true; + boolean allowOneBadLeader = leaderDies; + for(int i = 0; i < le.length; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], + port[i], 0, i, 1000, 2, 2); + peer.startLeaderElection(); + le[i] = new LeaderElection(peer); + LEThread thread = new LEThread(le[i], peer, i); + thread.start(); + threads.add(thread); + } + for(int i = 0; i < threads.size(); i++) { + threads.get(i).join(15000); + if (threads.get(i).isAlive()) { + Assert.fail("Threads didn't join"); + } + } + long id = votes[0].getId(); + for(int i = 1; i < votes.length; i++) { + if (votes[i] == null) { + Assert.fail("Thread " + i + " had a null vote"); + } + if (votes[i].getId() != id) { + if (allowOneBadLeader && votes[i].getId() == i) { + allowOneBadLeader = false; + } else { + Assert.fail("Thread " + i + " got " + votes[i].getId() + " expected " + id); + } + } + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java b/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java new file mode 100644 index 0000000..9135307 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java @@ -0,0 +1,175 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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.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.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +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 { + 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()); + } + 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); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, + zk.stat.getCversion() + 1); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, + zk.stat.getCversion() + 1); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, + -1); + + // 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); + } + + /* + * 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); + String childStr = ""; + for (String s : child) { + childStr += s + " "; + } + LOG.info("Children: " + childStr + " for " + 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); + ArrayList 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 = ""; + for (String s : child) { + childStr += s + " "; + } + LOG.info("Children: " + childStr + " for " + parentName); + LOG.info("(cverions, pzxid): " +newCversion + ", " + newPzxid); + Assert.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 : " + header.getMagic() + + " Expected : " + FileTxnLog.TXNLOG_MAGIC); + Assert.assertTrue("Missing magic number ", + header.getMagic() == FileTxnLog.TXNLOG_MAGIC); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java b/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java new file mode 100644 index 0000000..dac3910 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.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.test; + +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.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; + +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 { + ZooKeeper zk = createZKClient(hostPort); + + // generate some transactions that will get logged + 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); + long expectedZxid = 0; + long lastZxid = 0; + TxnHeader hdr; + do { + hdr = itr.getHeader(); + expectedZxid++; + Assert.assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid != hdr.getZxid()); + Assert.assertTrue("excepting next transaction. expected=" + expectedZxid + ", retreived=" + hdr.getZxid(), (hdr.getZxid() == expectedZxid)); + lastZxid = hdr.getZxid(); + }while(itr.next()); + + Assert.assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid == TOTAL_TRANSACTIONS)); + } + + /** + * Test we can restore the snapshot that has data ahead of the zxid + * of the snapshot file. + */ + @Test + public void testRestore() throws Exception { + ZooKeeper zk = createZKClient(hostPort); + + // generate some transactions + 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 = getServer(serverFactory); + 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 = getServer(serverFactory); + long fZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + + // Verify lastProcessedZxid is set correctly + Assert.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 " + expectedPath + " found " + path); + Assert.assertTrue("Error in sequential znode creation expected " + + expectedPath + " found " + path, path.equals(expectedPath)); + Assert.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 { + ZooKeeper zk = createZKClient(hostPort); + + // generate some transactions + 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 = getServer(serverFactory); + 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(); + } + + /** + * ZOOKEEPER-1573: test restoring a snapshot with deleted txns ahead of the + * snapshot file's zxid. + */ + @Test + public void testReloadSnapshotWithMissingParent() throws Exception { + ZooKeeper zk = createZKClient(hostPort); + + // create transactions to create the snapshot with create/delete pattern + 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 = getServer(serverFactory); + 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/src/java/test/org/apache/zookeeper/test/MaxCnxnsTest.java b/src/java/test/org/apache/zookeeper/test/MaxCnxnsTest.java new file mode 100644 index 0000000..0a31ac9 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/MaxCnxnsTest.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 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.Assert; +import org.junit.Test; + +public class MaxCnxnsTest extends ClientBase { + final private 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 { + int i; + SocketChannel socket; + public CnxnThread(int i) { + super("CnxnThread-"+i); + this.i = 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 Assert.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) { + } + } + } + } + } + + /** + * 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 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) { + 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 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); + Assert.assertTrue("Did't recieve proper error response", + opResult instanceof ErrorResult); + ErrorResult errRes = (ErrorResult) opResult; + Assert.assertEquals("Did't recieve proper error code", + expectedResultCodes.get(i).intValue(), errRes.getErr()); + } + } else { + try { + zk.multi(ops); + Assert.fail("Shouldn't have validated in ZooKeeper client!"); + } catch (KeeperException e) { + Assert.assertEquals("Wrong exception", expectedErr, e.code() + .name()); + } catch (IllegalArgumentException e) { + Assert.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); + } + + /** + * 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); + } + + /** + * 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 { + multi(zk, opList); + Assert.fail("multi delete should failed for not empty directory"); + } catch (KeeperException.NotEmptyException e) { + } + + final CountDownLatch latch = new CountDownLatch(1); + + zk.exists("/foo/bar", new Watcher() { + @Override + public void process(WatchedEvent event) { + if (event.getType() == Event.EventType.NodeDeleted){ + latch.countDown(); + } + } + }); + + epheZk.close(); + + latch.await(); + + try { + zk.getData("/foo/bar", false, null); + Assert.fail("ephemeral node should have been deleted"); + } catch (KeeperException.NoNodeException e) { + } + + multi(zk, opList); + + try { + zk.getData("/foo", false, null); + Assert.fail("persistent node should have been deleted after multi"); + } catch (KeeperException.NoNodeException e) { + } + } + + @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)); + + Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk + .exists(chRoot + "/myid", false)); + Assert.assertNotNull("zNode is not created under chroot:" + chRoot, + zk_chroot.exists("/myid", false)); + Assert.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)); + Assert.assertNull("zNode exists under chroot:" + chRoot, zk.exists( + chRoot + "/myid", false)); + Assert.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++) { + Assert.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); + + Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk + .exists(chRoot + childPath, false)); + Assert.assertNotNull("zNode is not created under chroot:" + chRoot, + zk_chroot.exists(childPath, false)); + Assert.assertNull("zNode is created directly under '/', ignored configured chroot", + zk.exists(childPath, false)); + Assert.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); + + Assert.assertNull("chroot:" + chRoot + " exists after delete", zk + .exists(chRoot + "/myid", false)); + Assert.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 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 + Assert.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) + )); + Assert.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 + Assert.assertNull(zk.exists("/multi/a/1", null)); + Assert.assertNull(zk.exists("/multi/a", null)); + Assert.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++) { + Assert.assertArrayEquals(names[i].getBytes(), zk.getData(names[i], false, null)); + } + } + + @Test + public void testUpdateConflict() throws Exception { + + Assert.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) + )); + Assert.fail("Should have thrown a KeeperException for invalid version"); + } catch (KeeperException e) { + //PASS + LOG.error("STACKTRACE: " + e); + } + + Assert.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) + )); + + Assert.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) + )); + Assert.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 + Assert.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(); + } + } + Assert.assertFalse("/multi should have been deleted so setData should have failed", + KeeperException.Code.OK.intValue() == res.rc); + Assert.assertNull(zk.exists("/multi", null)); + results = res.results; + } else { + try { + zk.multi(ops); + Assert.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 + Assert.assertNull(zk.exists("/multi", null)); + results = e.getResults(); + } + } + + Assert.assertNotNull(results); + for (OpResult r : results) { + LOG.info("RESULT==> " + r); + if (r instanceof ErrorResult) { + ErrorResult er = (ErrorResult) r; + LOG.info("ERROR RESULT: " + er + " ERR=>" + 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 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)); + } + + 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/src/java/test/org/apache/zookeeper/test/NioNettySuiteBase.java b/src/java/test/org/apache/zookeeper/test/NioNettySuiteBase.java new file mode 100644 index 0000000..d61a3e2 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/NioNettySuiteBase.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.test; + +import org.apache.zookeeper.server.NettyServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.AfterClass; +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()); + } + + @AfterClass + public static void tearDown() { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/NioNettySuiteHammerTest.java b/src/java/test/org/apache/zookeeper/test/NioNettySuiteHammerTest.java new file mode 100644 index 0000000..96e7b0c --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/NioNettySuiteHammerTest.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.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/src/java/test/org/apache/zookeeper/test/NioNettySuiteTest.java b/src/java/test/org/apache/zookeeper/test/NioNettySuiteTest.java new file mode 100644 index 0000000..eaf17b9 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/NioNettySuiteTest.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.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 + }) +public class NioNettySuiteTest extends NioNettySuiteBase { +} diff --git a/src/java/test/org/apache/zookeeper/test/NonRecoverableErrorTest.java b/src/java/test/org/apache/zookeeper/test/NonRecoverableErrorTest.java new file mode 100644 index 0000000..133d920 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/NonRecoverableErrorTest.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.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +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.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.Assert; +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(); + 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); + 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)); + } + + 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++) { + Assert.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--; + } + Assert.assertTrue("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/src/java/test/org/apache/zookeeper/test/NullDataTest.java b/src/java/test/org/apache/zookeeper/test/NullDataTest.java new file mode 100644 index 0000000..8f1a1bb --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/NullDataTest.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 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.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +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); + Assert.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/src/java/test/org/apache/zookeeper/test/OOMTest.java b/src/java/test/org/apache/zookeeper/test/OOMTest.java new file mode 100644 index 0000000..465df98 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/OOMTest.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.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +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.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +import org.junit.Test; + +public class OOMTest extends ZKTestCase implements Watcher { + @Test + public void testOOM() throws IOException, InterruptedException, KeeperException { + // This test takes too long tos run! + if (true) + return; + File tmpDir = ClientBase.createTmpDir(); + // Grab some memory so that it is easier to cause an + // OOM condition; + ArrayList 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); + Assert.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(); + Assert.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, this); + 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, this); + 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, this); + 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, this); + for (int i = 0; i < 10000; i++) { + zk.getChildren("/" + i, true); + } + zk.close(); + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent) + */ + public void process(WatchedEvent event) { + System.err.println("Got event " + event.getType() + " " + + event.getState() + " " + event.getPath()); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/OSMXBeanTest.java b/src/java/test/org/apache/zookeeper/test/OSMXBeanTest.java new file mode 100644 index 0000000..ce21ab8 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/OSMXBeanTest.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.test; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.zookeeper.server.util.OSMXBean; + +public class OSMXBeanTest { + + 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(); + Assert.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() == true) { + ofdc = osMbean.getOpenFileDescriptorCount(); + LOG.info("open fdcount is: " + ofdc); + } + Assert.assertFalse("The number of open file descriptor is negative",(ofdc < 0)); + } + + @Test + public final void testGetMaxFileDescriptorCount() { + if (osMbean != null && osMbean.getUnix() == true) { + mfdc = osMbean.getMaxFileDescriptorCount(); + LOG.info("max fdcount is: " + mfdc); + } + Assert.assertFalse("The max file descriptor number is negative",(mfdc < 0)); + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.java b/src/java/test/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.java new file mode 100644 index 0000000..9d373fc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.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.test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.junit.Test; + +public class ObserverHierarchicalQuorumTest extends HierarchicalQuorumTest { + private static final Logger LOG = LoggerFactory.getLogger(QuorumBase.class); + + /** + * 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/src/java/test/org/apache/zookeeper/test/ObserverLETest.java b/src/java/test/org/apache/zookeeper/test/ObserverLETest.java new file mode 100644 index 0000000..123ba0b --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ObserverLETest.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.*; + +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/src/java/test/org/apache/zookeeper/test/ObserverQuorumHammerTest.java b/src/java/test/org/apache/zookeeper/test/ObserverQuorumHammerTest.java new file mode 100644 index 0000000..03b4000 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ObserverQuorumHammerTest.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.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/src/java/test/org/apache/zookeeper/test/ObserverTest.java b/src/java/test/org/apache/zookeeper/test/ObserverTest.java new file mode 100644 index 0000000..f7a82c6 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ObserverTest.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.test.ClientBase.CONNECTION_TIMEOUT; + +import java.util.concurrent.CountDownLatch; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +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.server.quorum.QuorumPeerTestBase; +import org.junit.Assert; +import org.junit.Test; + +public class ObserverTest extends QuorumPeerTestBase implements Watcher{ + protected static final Logger LOG = + LoggerFactory.getLogger(ObserverTest.class); + + CountDownLatch latch; + ZooKeeper zk; + WatchedEvent lastEvent = null; + + /** + * 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. + * @throws Exception + */ + @Test + public void testObserver() throws Exception { + ClientBase.setupTestEnv(); + // We expect two notifications before we want to continue + latch = new CountDownLatch(2); + + 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(); + + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + final int CLIENT_PORT_OBS = PortAssignment.unique(); + + + String quorumCfgSection = + "electionAlg=3\n" + + "server.1=127.0.0.1:" + (PORT_QP1) + + ":" + (PORT_QP_LE1) + + "\nserver.2=127.0.0.1:" + (PORT_QP2) + + ":" + (PORT_QP_LE2) + + "\nserver.3=127.0.0.1:" + + (PORT_OBS)+ ":" + (PORT_OBS_LE) + ":observer"; + String obsCfgSection = quorumCfgSection + "\npeerType=observer"; + 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_OBS, obsCfgSection); + q1.start(); + q2.start(); + q3.start(); + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, + CONNECTION_TIMEOUT)); + Assert.assertTrue("waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, + CONNECTION_TIMEOUT)); + + 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 + Assert.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); + + Assert.assertEquals(zk.getState(), States.CONNECTED); + + LOG.info("Shutting down server 2"); + // Now kill one of the other real servers + q2.shutdown(); + + Assert.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(); + Assert.assertNotSame("Client is still connected to non-quorate cluster", + KeeperState.SyncConnected,lastEvent.getState()); + + LOG.info("Latch returned"); + + try { + Assert.assertFalse("Shouldn't get a response when cluster not quorate!", + new String(zk.getData("/obstest", null, null)).equals("test")); + } + 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); + q2.start(); + + LOG.info("Waiting for server 2 to come up"); + Assert.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 + Assert.assertTrue("Client didn't reconnect to quorate ensemble (state was" + + lastEvent.getState() + ")", + (KeeperState.SyncConnected==lastEvent.getState() || + KeeperState.Expired==lastEvent.getState())); + + LOG.info("Shutting down all servers"); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + LOG.info("Closing zk client"); + + zk.close(); + Assert.assertTrue("Waiting for server 1 to shut down", + ClientBase.waitForServerDown("127.0.0.1:"+CLIENT_PORT_QP1, + ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("Waiting for server 2 to shut down", + ClientBase.waitForServerDown("127.0.0.1:"+CLIENT_PORT_QP2, + ClientBase.CONNECTION_TIMEOUT)); + Assert.assertTrue("Waiting for server 3 to shut down", + ClientBase.waitForServerDown("127.0.0.1:"+CLIENT_PORT_OBS, + ClientBase.CONNECTION_TIMEOUT)); + + } + + /** + * Implementation of watcher interface. + */ + public void process(WatchedEvent event) { + lastEvent = event; + latch.countDown(); + LOG.info("Latch got event :: " + event); + } + + /** + * 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\npeerType=observer\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + q1.join(ClientBase.CONNECTION_TIMEOUT); + Assert.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); + Assert.assertFalse(q1.isAlive()); + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/QuorumBase.java b/src/java/test/org/apache/zookeeper/test/QuorumBase.java new file mode 100644 index 0000000..49cff35 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumBase.java @@ -0,0 +1,341 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.util.OSMXBean; +import org.junit.Assert; +import org.junit.Test; + + +public class QuorumBase 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 portLE1; + protected int portLE2; + protected int portLE3; + protected int portLE4; + protected int portLE5; + + @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(); + + hostPort = "127.0.0.1:" + port1 + + ",127.0.0.1:" + port2 + + ",127.0.0.1:" + port3 + + ",127.0.0.1:" + port4 + + ",127.0.0.1:" + port5; + 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() == true) { + 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; + HashMap peers = new HashMap(); + peers.put(Long.valueOf(1), new QuorumServer(1, "127.0.0.1", port1 + 1000, + portLE1 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(2), new QuorumServer(2, "127.0.0.1", port2 + 1000, + portLE2 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(3), new QuorumServer(3, "127.0.0.1", port3 + 1000, + portLE3 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(4), new QuorumServer(4, "127.0.0.1", port4 + 1000, + portLE4 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, "127.0.0.1", port5 + 1000, + portLE5 + 1000, + 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 " + port1); + s1 = new QuorumPeer(peers, s1dir, s1dir, port1, 3, 1, tickTime, initLimit, syncLimit); + Assert.assertEquals(port1, s1.getClientPort()); + LOG.info("creating QuorumPeer 2 port " + port2); + s2 = new QuorumPeer(peers, s2dir, s2dir, port2, 3, 2, tickTime, initLimit, syncLimit); + Assert.assertEquals(port2, s2.getClientPort()); + LOG.info("creating QuorumPeer 3 port " + port3); + s3 = new QuorumPeer(peers, s3dir, s3dir, port3, 3, 3, tickTime, initLimit, syncLimit); + Assert.assertEquals(port3, s3.getClientPort()); + LOG.info("creating QuorumPeer 4 port " + port4); + s4 = new QuorumPeer(peers, s4dir, s4dir, port4, 3, 4, tickTime, initLimit, syncLimit); + Assert.assertEquals(port4, s4.getClientPort()); + LOG.info("creating QuorumPeer 5 port " + port5); + s5 = new QuorumPeer(peers, s5dir, s5dir, port5, 3, 5, tickTime, initLimit, syncLimit); + Assert.assertEquals(port5, 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()); + + 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(",")) { + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(hp, + CONNECTION_TIMEOUT)); + LOG.info(hp + " is accepting client connections"); + } + + // 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 void setupServers() throws IOException { + setupServer(1); + setupServer(2); + setupServer(3); + setupServer(4); + setupServer(5); + } + + HashMap peers = null; + public void setupServer(int i) throws IOException { + int tickTime = 2000; + int initLimit = 3; + int syncLimit = 3; + + if(peers == null){ + peers = new HashMap(); + + peers.put(Long.valueOf(1), new QuorumServer(1, "127.0.0.1", port1 + 1000, + portLE1 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(2), new QuorumServer(2, "127.0.0.1", port2 + 1000, + portLE2 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(3), new QuorumServer(3, "127.0.0.1", port3 + 1000, + portLE3 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(4), new QuorumServer(4, "127.0.0.1", port4 + 1000, + portLE4 + 1000, + LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, "127.0.0.1", port5 + 1000, + portLE5 + 1000, + LearnerType.PARTICIPANT)); + } + + switch(i){ + case 1: + LOG.info("creating QuorumPeer 1 port " + port1); + s1 = new QuorumPeer(peers, s1dir, s1dir, port1, 3, 1, tickTime, initLimit, syncLimit); + Assert.assertEquals(port1, s1.getClientPort()); + break; + case 2: + LOG.info("creating QuorumPeer 2 port " + port2); + s2 = new QuorumPeer(peers, s2dir, s2dir, port2, 3, 2, tickTime, initLimit, syncLimit); + Assert.assertEquals(port2, s2.getClientPort()); + break; + case 3: + LOG.info("creating QuorumPeer 3 port " + port3); + s3 = new QuorumPeer(peers, s3dir, s3dir, port3, 3, 3, tickTime, initLimit, syncLimit); + Assert.assertEquals(port3, s3.getClientPort()); + break; + case 4: + LOG.info("creating QuorumPeer 4 port " + port4); + s4 = new QuorumPeer(peers, s4dir, s4dir, port4, 3, 4, tickTime, initLimit, syncLimit); + Assert.assertEquals(port4, s4.getClientPort()); + break; + case 5: + LOG.info("creating QuorumPeer 5 port " + port5); + s5 = new QuorumPeer(peers, s5dir, s5dir, port5, 3, 5, tickTime, initLimit, syncLimit); + Assert.assertEquals(port5, s5.getClientPort()); + } + } + + @Override + public void tearDown() throws Exception { + LOG.info("TearDown started"); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix() == true) { + LOG.info("fdcount after test is: " + + osMbean.getOpenFileDescriptorCount()); + } + + shutdownServers(); + + for (String hp : hostPort.split(",")) { + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(hp, + ClientBase.CONNECTION_TIMEOUT)); + LOG.info(hp + " is no longer accepting client connections"); + } + + JMXEnv.tearDown(); + } + public void shutdownServers() { + shutdown(s1); + shutdown(s2); + shutdown(s3); + shutdown(s4); + shutdown(s5); + } + + public static void shutdown(QuorumPeer qp) { + 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 " + qp.getName() + " to exit thread"); + 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()) { + Assert.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); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/QuorumHammerTest.java b/src/java/test/org/apache/zookeeper/test/QuorumHammerTest.java new file mode 100644 index 0000000..e5b377e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumHammerTest.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 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/src/java/test/org/apache/zookeeper/test/QuorumQuotaTest.java b/src/java/test/org/apache/zookeeper/test/QuorumQuotaTest.java new file mode 100644 index 0000000..bef1a33 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumQuotaTest.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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeperMain; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Test; + +public class QuorumQuotaTest extends QuorumBase { + private static final Logger LOG = + LoggerFactory.getLogger(QuorumQuotaTest.class); + + @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); + } + ZooKeeperMain.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)); + Assert.assertTrue("bytes are set", st.getBytes() == 1204L); + Assert.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)); + Assert.assertTrue("bytes are set", st.getBytes() == 2404L); + Assert.assertTrue("num count is set", st.getCount() == 601); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/QuorumTest.java b/src/java/test/org/apache/zookeeper/test/QuorumTest.java new file mode 100644 index 0000000..6f827ec --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumTest.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.test; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +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.Assert; +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(); + + @Before + public void setUp() throws Exception { + qb.setUp(); + ct.hostPort = qb.hostPort; + ct.setUpAll(); + } + + @After + public void tearDown() throws Exception { + ct.tearDownAll(); + qb.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() { + Assert.assertEquals(5,qb.s1.getView().size()); + Assert.assertEquals(5,qb.s2.getView().size()); + Assert.assertEquals(5,qb.s3.getView().size()); + Assert.assertEquals(5,qb.s4.getView().size()); + Assert.assertEquals(5,qb.s5.getView().size()); + } + + @Test + public void testViewContains() { + // Test view contains self + Assert.assertTrue(qb.s1.viewContains(qb.s1.getId())); + + // Test view contains other servers + Assert.assertTrue(qb.s1.viewContains(qb.s2.getId())); + + // Test view does not contain non-existant servers + Assert.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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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; + Assert.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 + Assert.assertTrue(qb.s1.isAlive()); + Assert.assertTrue(qb.s2.isAlive()); + Assert.assertTrue(qb.s3.isAlive()); + Assert.assertTrue(qb.s4.isAlive()); + Assert.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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, + new Watcher() {public void process(WatchedEvent event) { + }}, + 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(hostPorts[(i+1)%hostPorts.length] + " Sync returned " + result[0]); + Assert.assertTrue(result[0] == KeeperException.Code.OK.intValue()); + try { + zk.setData("/", new byte[1], -1); + Assert.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; + } + } + } + + /** + * 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); + Assert.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); + } + Assert.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(); + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/QuorumUtil.java b/src/java/test/org/apache/zookeeper/test/QuorumUtil.java new file mode 100644 index 0000000..1f2dbbc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumUtil.java @@ -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. + */ + +package org.apache.zookeeper.test; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.junit.Assert; + +/** + * 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); + + public 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 electionAlg; + + /** + * 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; + 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, "127.0.0.1", ps.clientPort + 1000, + PortAssignment.unique() + 1000, + 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 " + i + "; public port " + ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, + electionAlg, ps.id, tickTime, initLimit, syncLimit); + Assert.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); + } + + 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(",")) { + Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, + ClientBase.CONNECTION_TIMEOUT)); + LOG.info(hp + " is accepting client connections"); + } + + // 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) { + Assert.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 " + ps.id + "; public port " + ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, + ps.id, tickTime, initLimit, syncLimit); + Assert.assertEquals(ps.clientPort, ps.peer.getClientPort()); + + ps.peer.start(); + } + + public void restart(int id) throws IOException { + start(id); + Assert.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 " + ps.id + "; public port " + ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, + ps.id, tickTime, initLimit, syncLimit); + Assert.assertEquals(ps.clientPort, ps.peer.getClientPort()); + + ps.peer.start(); + Assert.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(",")) { + Assert.assertTrue("Waiting for server down", ClientBase.waitForServerDown(hp, + ClientBase.CONNECTION_TIMEOUT)); + LOG.info(hp + " is no longer accepting client connections"); + } + } + + 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 " + qp.getName() + " to exit thread"); + qp.join(30000); + if (qp.isAlive()) { + Assert.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 void tearDown() throws Exception { + LOG.info("TearDown started"); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix() == true) { + LOG.info("fdcount after test is: " + osMbean.getOpenFileDescriptorCount()); + } + + shutdownAll(); + JMXEnv.tearDown(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/QuorumZxidSyncTest.java b/src/java/test/org/apache/zookeeper/test/QuorumZxidSyncTest.java new file mode 100644 index 0000000..6e46edc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/QuorumZxidSyncTest.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.test; + +import java.io.File; + +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.junit.After; +import org.junit.Assert; +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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + zk.create("/2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + deleteFiles(qb.s1dir); + deleteFiles(qb.s2dir); + deleteFiles(qb.s3dir); + deleteFiles(qb.s4dir); + qb.setupServers(); + qb.s1.start(); + qb.s2.start(); + qb.s3.start(); + qb.s4.start(); + Assert.assertTrue("Servers didn't come up", ClientBase.waitForServerUp(qb.hostPort, 10000)); + qb.s5.start(); + String hostPort = "127.0.0.1:" + qb.s5.getClientPort(); + Assert.assertFalse("Servers came up, but shouldn't have since it's ahead of leader", + ClientBase.waitForServerUp(hostPort, 10000)); + } + + private void deleteFiles(File f) { + File v = new File(f, "version-2"); + for(File c: v.listFiles()) { + c.delete(); + } + } + + /** + * 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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, new Watcher() { + public void process(WatchedEvent event) { + }}); + boolean saw2 = false; + for(String child: zk.getChildren("/", false)) { + if (child.equals("2")) { + saw2 = true; + } + } + zk.close(); + Assert.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/src/java/test/org/apache/zookeeper/test/ReadOnlyModeTest.java b/src/java/test/org/apache/zookeeper/test/ReadOnlyModeTest.java new file mode 100644 index 0000000..6f281d9 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ReadOnlyModeTest.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; + +import java.io.ByteArrayOutputStream; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.util.regex.Pattern; + +import junit.framework.Assert; + +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); + Assert.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)); + Assert.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(); + Assert.fail("Write operation using multi-transaction" + + " api has succeeded during RO mode"); + } catch (NotReadOnlyException e) { + // ok + } + + Assert.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); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // read operation during r/o mode + String remoteData = new String(zk.getData(node, false, null)); + Assert.assertEquals(data, remoteData); + + try { + zk.setData(node, "no way".getBytes(), -1); + Assert.fail("Write operation has succeeded during RO mode"); + } catch (NotReadOnlyException e) { + // ok + } + + watcher.reset(); + qu.start(2); + Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( + "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); + 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); + } + } + Assert.assertTrue("Did not succeed in connecting in 30s", success); + Assert.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); + long start = Time.currentElapsedTime(); + while (!(zk.getState() == States.CONNECTEDREADONLY)) { + Thread.sleep(200); + // FIXME this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this + Assert.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(); + LOG.debug("Connection string: {}", qu.getConnString()); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, + watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); + Assert.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); + Assert.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); + Assert.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); + Assert.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; + } + } + Assert.assertTrue( + "Majority server wasn't found while connected to r/o server", + found); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/RecoveryTest.java b/src/java/test/org/apache/zookeeper/test/RecoveryTest.java new file mode 100644 index 0000000..c084a68 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/RecoveryTest.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. + */ + +package org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +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.Assert; +import org.junit.Test; + +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"); + + Assert.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); + Assert.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(); + Assert.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); + + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(HOSTPORT, + CONNECTION_TIMEOUT)); + + startSignal.await(CONNECTION_TIMEOUT, + TimeUnit.MILLISECONDS); + Assert.assertTrue("count == 0", startSignal.getCount() == 0); + + Stat stat = new Stat(); + for (int i = 0; i < 10; i++) { + path = "/" + i; + LOG.info("Checking " + path); + Assert.assertEquals(new String(zk.getData(path, false, stat)), path + + "!"); + for (int j = 0; j < 10; j++) { + String subpath = path + "/" + j; + Assert.assertEquals(new String(zk.getData(subpath, false, stat)), + subpath + "!"); + for (int k = 0; k < 20; k++) { + String subsubpath = subpath + "/" + k; + Assert.assertEquals(new String(zk.getData(subsubpath, false, + stat)), subsubpath + "!"); + } + } + } + f.shutdown(); + zks.shutdown(); + + Assert.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); + + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(HOSTPORT, + CONNECTION_TIMEOUT)); + + startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertTrue("count == 0", startSignal.getCount() == 0); + + stat = new Stat(); + LOG.info("Check 2"); + for (int i = 0; i < 10; i++) { + path = "/" + i; + Assert.assertEquals(new String(zk.getData(path, false, stat)), + path + "!"); + for (int j = 0; j < 10; j++) { + String subpath = path + "/" + j; + Assert.assertEquals(new String(zk.getData(subpath, false, stat)), + subpath + "!"); + for (int k = 0; k < 20; k++) { + String subsubpath = subpath + "/" + k; + Assert.assertEquals(new String(zk.getData(subsubpath, false, + stat)), subsubpath + "!"); + } + } + } + zk.close(); + + f.shutdown(); + zks.shutdown(); + + Assert.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/src/java/test/org/apache/zookeeper/test/RepeatStartupTest.java b/src/java/test/org/apache/zookeeper/test/RepeatStartupTest.java new file mode 100644 index 0000000..f714a2c --- /dev/null +++ b/src/java/test/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 org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +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(); + qb.shutdown(qb.s1); + qb.shutdown(qb.s2); + qb.shutdown(qb.s3); + qb.shutdown(qb.s4); + qb.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"); + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + PORT, + QuorumTest.CONNECTION_TIMEOUT)); + factory.shutdown(); + zks.shutdown(); + Assert.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/src/java/test/org/apache/zookeeper/test/RestoreCommittedLogTest.java b/src/java/test/org/apache/zookeeper/test/RestoreCommittedLogTest.java new file mode 100644 index 0000000..65cc0e2 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/RestoreCommittedLogTest.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.test; + +import java.io.File; +import java.util.List; +import java.util.LinkedList; + +import org.apache.log4j.Logger; +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.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +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.Assert; +import org.junit.Test; + +/** 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 implements Watcher { + private static final Logger LOG = Logger.getLogger(RestoreCommittedLogTest.class); + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + /** + * test the purge + * @throws Exception an exception might be thrown here + */ + @Test + public void testRestoreCommittedLog() 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); + Assert.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< 2000; i++) { + zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + Assert.assertTrue("waiting for server to shutdown", + ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + // start server again + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + zks.startdata(); + LinkedList committedLog = zks.getZKDatabase().getCommittedLog(); + int logsize = committedLog.size(); + LOG.info("committedLog size = " + logsize); + Assert.assertTrue("log size != 0", (logsize != 0)); + zks.shutdown(); + } + + public void process(WatchedEvent event) { + // do nothing + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java new file mode 100644 index 0000000..9e28278 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.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 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.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.junit.Assert; +import org.junit.Test; + +public class SaslAuthDesignatedClientTest extends ClientBase { + static { + System.setProperty("zookeeper.authProvider.1","org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(ZooKeeperSaslClient.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) { + Assert.fail("test failed :" + e); + } + finally { + zk.close(); + } + } + + @Test + public void testSaslConfig() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.getChildren("/", false); + Assert.assertFalse(zk.getSaslClient(). + clientTunneledAuthenticationInProgress()); + Assert.assertEquals(zk.getSaslClient().getSaslState(), + ZooKeeperSaslClient.SaslState.COMPLETE); + Assert.assertNotNull( + javax.security.auth.login.Configuration.getConfiguration(). + getAppConfigurationEntry("MyZookeeperClient")); + Assert.assertSame(zk.getSaslClient().getLoginContext(), + "MyZookeeperClient"); + } catch (KeeperException e) { + Assert.fail("test failed :" + e); + } finally { + zk.close(); + } + } + + +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.java new file mode 100644 index 0000000..aa30870 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.ZooKeeperSaslServer; +import org.apache.zookeeper.JaasConfiguration; +import org.junit.Assert; +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 + synchronized public 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); + Assert.assertEquals(authFailed.get(), 0); + + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + } catch (KeeperException e) { + Assert.fail("test failed :" + e); + } + finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.java new file mode 100644 index 0000000..5291141 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.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.test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.junit.Assert; +import org.junit.Test; + +public class SaslAuthFailDesignatedClientTest extends ClientBase { + static { + System.setProperty("zookeeper.authProvider.1","org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(ZooKeeperSaslClient.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. + } + } + + private AtomicInteger authFailed = new AtomicInteger(0); + + 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 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)) + { + Assert.fail("Unable to connect to server"); + } + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Assert.fail("Should have gotten exception."); + } catch (KeeperException e) { + // ok, exception as expected. + LOG.info("Got exception as expected: " + e); + } + finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthFailNotifyTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthFailNotifyTest.java new file mode 100644 index 0000000..2b00d86 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthFailNotifyTest.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.test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Test; +import org.junit.Assert; + +public class SaslAuthFailNotifyTest 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 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) { + synchronized(authFailed) { + authFailed.incrementAndGet(); + authFailed.notify(); + } + } + else { + super.process(event); + } + } + } + + @Test + public void testBadSaslAuthNotifiesWatch() throws Exception { + ZooKeeper zk = createClient(); + // wait for authFailed event from client's EventThread. + synchronized(authFailed) { + authFailed.wait(); + } + Assert.assertEquals(authFailed.get(),1); + zk.close(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthFailTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthFailTest.java new file mode 100644 index 0000000..33a505e --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthFailTest.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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Test; +import org.junit.Assert; + +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. + } + } + + @Test + public void testAuthFail() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Assert.fail("Should have gotten exception."); + } catch(Exception e ) { + // ok, exception as expected. + LOG.info("Got exception as expected: " + e); + } finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.java b/src/java/test/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.java new file mode 100644 index 0000000..98be0be --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.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 java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.junit.Assert; +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(ZooKeeperSaslClient.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. + } + } + + private AtomicInteger authFailed = new AtomicInteger(0); + + 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 testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Assert.fail("Should have gotten exception."); + } catch (KeeperException e) { + // ok, exception as expected. + LOG.info("Got exception as expected: " + e); + } + finally { + zk.close(); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SaslClientTest.java b/src/java/test/org/apache/zookeeper/test/SaslClientTest.java new file mode 100644 index 0000000..8213abc --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SaslClientTest.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.ZKTestCase; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +public class SaslClientTest extends ZKTestCase { + + private String existingPropertyValue = null; + + @Before + public void setUp() { + existingPropertyValue = System.getProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY); + } + + @After + public void tearDown() { + // Restore the System property if it was set previously + if (existingPropertyValue != null) { + System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, existingPropertyValue); + } + } + + @Test + public void testSaslClientDisabled() { + System.clearProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY); + Assert.assertTrue("SASL client disabled", ZooKeeperSaslClient.isEnabled()); + + for (String value : Arrays.asList("true", "TRUE")) { + System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, value); + Assert.assertTrue("SASL client disabled", ZooKeeperSaslClient.isEnabled()); + } + + for (String value : Arrays.asList("false", "FALSE")) { + System.setProperty(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, value); + Assert.assertFalse("SASL client disabled", ZooKeeperSaslClient.isEnabled()); + } + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SessionInvalidationTest.java b/src/java/test/org/apache/zookeeper/test/SessionInvalidationTest.java new file mode 100644 index 0000000..3a0b2ee --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SessionInvalidationTest.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.test; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +import junit.framework.Assert; + +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(); + Assert.assertEquals(1, zk.getChildren("/", false).size()); + + zk.close(); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SessionTest.java b/src/java/test/org/apache/zookeeper/test/SessionTest.java new file mode 100644 index 0000000..891b0f0 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SessionTest.java @@ -0,0 +1,394 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 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.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.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.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +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); + + Assert.assertTrue("waiting for server up", + ClientBase.waitForServerUp(HOSTPORT, + CONNECTION_TIMEOUT)); + } + + @After + public void tearDown() throws Exception { + serverFactory.shutdown(); + zs.shutdown(); + Assert.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)) { + Assert.fail("Unable to connect to server"); + } + + return zk; + } + +// FIXME this test is Assert.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"); +// +// Assert.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(name + " event:" + 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" + Long.toHexString(zk.getSessionId()) + + " was destroyed!"); + + // 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" + Long.toHexString(zk.getSessionId()) + + " was created!"); + zk.getData("/e", false, stat); + LOG.info("After get data /e"); + zk.close(); + + zk = createClient(); + Assert.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); + Assert.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); + } + } + Assert.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(hostPorts[(i+1)%hostPorts.length] + " Sync returned " + result[0]); + Assert.assertTrue(result[0] == KeeperException.Code.OK.intValue()); + zknew.setData("/", new byte[1], -1); + try { + zk.setData("/", new byte[1], -1); + Assert.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 Assert.failed connection attempt + Assert.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); + Assert.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); + Assert.assertEquals(TICK_TIME * 2, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate upper limit + zk = createClient(TICK_TIME * 30); + Assert.assertEquals(TICK_TIME * 20, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + } + + private class DupWatcher extends CountdownWatcher { + public LinkedList 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 = ClientBase.getServer(serverFactory); + zs.setMinSessionTimeout(MINSESS); + zs.setMaxSessionTimeout(MAXSESS); + } + + // validate typical case - requested == negotiated + int timeout = 120000; + DisconnectableZooKeeper zk = createClient(timeout); + Assert.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); + Assert.assertEquals(MINSESS, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate upper limit + zk = createClient(MAXSESS * 2); + Assert.assertEquals(MAXSESS, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SessionTimeoutTest.java b/src/java/test/org/apache/zookeeper/test/SessionTimeoutTest.java new file mode 100644 index 0000000..09badae --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SessionTimeoutTest.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 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.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +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 = new Watcher() { + @Override + public void process(WatchedEvent event) { + if ( event.getState() == Event.KeeperState.Expired ) { + expirationLatch.countDown(); + } + } + }; + zk.exists("/foo", watcher); + + zk.getTestable().injectSessionExpiration(); + Assert.assertTrue(expirationLatch.await(5, TimeUnit.SECONDS)); + + boolean gotException = false; + try { + zk.exists("/foo", false); + Assert.fail("Should have thrown a SessionExpiredException"); + } catch (KeeperException.SessionExpiredException e) { + // correct + gotException = true; + } + Assert.assertTrue(gotException); + } + + /** + * 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/src/java/test/org/apache/zookeeper/test/SledgeHammer.java b/src/java/test/org/apache/zookeeper/test/SledgeHammer.java new file mode 100644 index 0000000..614a93d --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SledgeHammer.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 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.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 SledgeHammer extends Thread implements Watcher { + ZooKeeper zk; + + int count; + + int readsPerWrite; + + public SledgeHammer(String hosts, int count, int readsPerWrite) + throws IOException { + zk = new ZooKeeper(hosts, 10000, this); + 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 (Exception e) { + e.printStackTrace(); + } + } + + /** + * @param args + * @throws IOException + * @throws KeeperException + * @throws NumberFormatException + */ + public static void main(String[] args) throws NumberFormatException, + IOException { + if (args.length != 3) { + System.err + .println("USAGE: SledgeHammer zookeeper_server reps reads_per_rep"); + System.exit(3); + } + SledgeHammer h = new SledgeHammer(args[0], Integer.parseInt(args[1]), + Integer.parseInt(args[2])); + h.start(); + System.exit(0); + } + + public void process(WatchedEvent event) { + synchronized (this) { + notifyAll(); + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/StandaloneTest.java b/src/java/test/org/apache/zookeeper/test/StandaloneTest.java new file mode 100644 index 0000000..f305423 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/StandaloneTest.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.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.junit.Assert; +import org.junit.Test; + +/** + * Standalone server tests. + */ +public class StandaloneTest extends QuorumPeerTestBase implements Watcher{ + protected static final Logger LOG = + LoggerFactory.getLogger(StandaloneTest.class); + + /** + * Ensure that a single standalone server comes up when misconfigured + * with a single server.# line in the configuration. This handles the + * case of HBase, which configures zoo.cfg in this way. Maintain b/w + * compatibility. + * TODO remove in a future version (4.0.0 hopefully) + */ + @Test + public void testStandaloneQuorum() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = + "server.1=127.0.0.1:" + (PortAssignment.unique()) + + ":" + (PortAssignment.unique()) + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + try { + Assert.assertTrue("waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, + CONNECTION_TIMEOUT)); + } finally { + q1.shutdown(); + } + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/StatTest.java b/src/java/test/org/apache/zookeeper/test/StatTest.java new file mode 100644 index 0000000..f4c01aa --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/StatTest.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.test; + +import java.io.IOException; + +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.junit.Assert; +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 Assert.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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(0, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid() + 1, stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(1, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.assertEquals(1, stat.getNumChildren()); + + stat = newStat(); + zk.getData(childname, false, stat); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(0, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); + Assert.assertEquals(childname.length(), stat.getDataLength()); + Assert.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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid() + i + 1, stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(i + 1, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.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); + + Assert.assertEquals(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); + Assert.assertEquals(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(0, stat.getCversion()); + Assert.assertEquals(0, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length(), stat.getDataLength()); + Assert.assertEquals(0, stat.getNumChildren()); + + zk.setData(name, (name + name).getBytes(), -1); + + stat = newStat(); + zk.getData(name, false, stat); + + Assert.assertNotSame(stat.getCzxid(), stat.getMzxid()); + Assert.assertEquals(stat.getCzxid(), stat.getPzxid()); + Assert.assertNotSame(stat.getCtime(), stat.getMtime()); + Assert.assertEquals(0, stat.getCversion()); + Assert.assertEquals(1, stat.getVersion()); + Assert.assertEquals(0, stat.getAversion()); + Assert.assertEquals(0, stat.getEphemeralOwner()); + Assert.assertEquals(name.length() * 2, stat.getDataLength()); + Assert.assertEquals(0, stat.getNumChildren()); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java b/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java new file mode 100644 index 0000000..a4107a5 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.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.test; + +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +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.junit.Assert.fail; +import static org.junit.matchers.JUnitMatchers.hasItems; +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; + +public class StaticHostProviderTest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(StaticHostProviderTest.class); + + @Test + public void testNextGoesRound() { + HostProvider hostProvider = getHostProvider((byte) 2); + InetSocketAddress first = hostProvider.next(0); + assertTrue(first instanceof InetSocketAddress); + 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 + 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); + } + + @Test + public void testLiteralIPNoReverseNS() { + byte size = 30; + HostProvider hostProvider = getHostProviderUnresolved(size); + for (int i = 0; i < size; i++) { + InetSocketAddress next = hostProvider.next(0); + assertTrue(next instanceof InetSocketAddress); + assertTrue(!next.isUnresolved()); + assertTrue("InetSocketAddress must not have hostname part " + + next.toString(), next.toString().startsWith("/")); + // Do NOT trigger the reverse name service lookup. + String hostname = next.getHostName(); + // In this case, the hostname equals literal IP address. + hostname.equals(next.getAddress().getHostAddress()); + } + } + + @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 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)); + } + + private Collection getUnresolvedServerAddresses(byte size) { + ArrayList list = new ArrayList(size); + while (size > 0) { + list.add(InetSocketAddress.createUnresolved("192.0.2." + size, 1234 + size)); + --size; + } + return list; + } + + private StaticHostProvider getHostProvider(byte size) { + ArrayList list = new ArrayList( + size); + while (size > 0) { + try { + list.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{-64, 0, 2, size}), 1234 + size)); + } catch (UnknownHostException e) { + LOG.error("Exception while resolving address", e); + fail("Failed to resolve address"); + } + --size; + } + return new StaticHostProvider(list); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/SyncCallTest.java b/src/java/test/org/apache/zookeeper/test/SyncCallTest.java new file mode 100644 index 0000000..4fb006a --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/SyncCallTest.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.test; + +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.CreateMode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Test; + +public class SyncCallTest extends ClientBase + implements ChildrenCallback, Children2Callback, StringCallback, VoidCallback +{ + 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 < 100; i++) + zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT, 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("/", new NullWatcher(), (ChildrenCallback)this, + results); + for(int i = 0; i < 100; i++) + zk.getChildren("/", new NullWatcher(), (Children2Callback)this, + results); + LOG.info("Submitted all operations:" + (new Date()).toString()); + + if(!opsCount.await(10000, TimeUnit.MILLISECONDS)) + Assert.fail("Haven't received all confirmations" + opsCount.getCount()); + + for(int i = 0; i < limit ; i++){ + Assert.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") + public void processResult(int rc, String path, Object ctx){ + ((List) ctx).add(rc); + opsCount.countDown(); + + } +} diff --git a/src/java/test/org/apache/zookeeper/test/TestHammer.java b/src/java/test/org/apache/zookeeper/test/TestHammer.java new file mode 100644 index 0000000..a73d6df --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/TestHammer.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 java.io.IOException; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.common.Time; + +public class TestHammer implements VoidCallback { + + /** + * @param args + */ + static int REPS = 50000; + public static void main(String[] args) { + long startTime = Time.currentElapsedTime(); + ZooKeeper zk = null; + try { + zk = new ZooKeeper(args[0], 10000, null); + } catch (IOException e1) { + // TODO Auto-generated catch block + 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/src/java/test/org/apache/zookeeper/test/TestUtils.java b/src/java/test/org/apache/zookeeper/test/TestUtils.java new file mode 100644 index 0000000..fa08b82 --- /dev/null +++ b/src/java/test/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 java.io.File; + +import org.junit.Assert; + +/** + * 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) + { + Assert.fail("file '" + f.getAbsolutePath()+"' deletion failed"); + } + } + } + return file.delete(); + } + return true; + } + + public static boolean deleteFileRecursively(File file) { + return deleteFileRecursively(file, false); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/TruncateTest.java b/src/java/test/org/apache/zookeeper/test/TruncateTest.java new file mode 100644 index 0000000..66ff63d --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/TruncateTest.java @@ -0,0 +1,256 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; + +import junit.framework.Assert; + +import org.apache.jute.Record; +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.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; + final int baseHostPort = PortAssignment.unique(); + + @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); + } + + volatile boolean connected; + Watcher nullWatcher = new Watcher() { + @Override + public void process(WatchedEvent event) { + connected = event.getState() == Watcher.Event.KeeperState.SyncConnected; + } + }; + + @Test + public void testTruncationStreamReset() 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.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(); + Assert.assertEquals(1, hdr.getZxid()); + Assert.assertTrue(txn instanceof SetDataTxn); + + iter.next(); + + hdr = iter.getHeader(); + txn = iter.getTxn(); + Assert.assertEquals(200, hdr.getZxid()); + Assert.assertTrue(txn instanceof SetDataTxn); + iter.close(); + 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(null, 0, 0, 0, null, null); + req.hdr = hdr; + req.txn = txn; + + zkdb.append(req); + zkdb.commit(); + } + + + @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()); + Assert.assertTrue("Failed to delete log file: " + logs[i].getName(), logs[i].delete()); + } + try { + zkdb.truncateLog(1); + Assert.assertTrue("Should not get here", false); + } + catch(IOException e) { + Assert.assertTrue("Should have received an IOException", true); + } + catch(NullPointerException npe) { + Assert.fail("This should not throw NPE!"); + } + + ClientBase.recursiveDelete(tmpdir); + } + + @Test + public void testTruncate() throws IOException, InterruptedException, KeeperException { + // Prime the server that is going to come in late with 50 txns + String hostPort = "127.0.0.1:" + baseHostPort; + int maxCnxns = 100; + ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, + hostPort, maxCnxns); + ClientBase.startServerInstance(dataDir1, factory, hostPort); + 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); + + ZooKeeper zk = new ZooKeeper(hostPort, 15000, nullWatcher); + 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 = ClientBase.getServer(factory); + + 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 port1 = baseHostPort+1; + int port2 = baseHostPort+2; + int port3 = baseHostPort+3; + + // Start up two of the quorum and add 10 txns + HashMap peers = new HashMap(); + peers.put(Long.valueOf(1), new QuorumServer(1, "127.0.0.1", port1 + 1000, 0, null)); + peers.put(Long.valueOf(2), new QuorumServer(2, "127.0.0.1", port2 + 1000, 0, null)); + peers.put(Long.valueOf(3), new QuorumServer(3, "127.0.0.1", port3 + 1000, 0, null)); + + QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 0, 2, tickTime, initLimit, syncLimit); + s2.start(); + QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 0, 3, tickTime, initLimit, syncLimit); + s3.start(); + connected = false; + zk = new ZooKeeper("127.0.0.1:" + port2, 15000, nullWatcher); + while(!connected) { + Thread.sleep(1000); + } + 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 = new ZooKeeper("127.0.0.1:" + port2, 15000, nullWatcher); + zk2.getData("/9", false, new Stat()); + try { + zk2.getData("/10", false, new Stat()); + Assert.fail("Should have gotten an error"); + } catch(KeeperException.NoNodeException e) { + // this is what we want + } + QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 0, 1, tickTime, initLimit, syncLimit); + s1.start(); + + connected = false; + ZooKeeper zk1 = new ZooKeeper("127.0.0.1:" + port1, 15000, nullWatcher); + while(!connected) { + Thread.sleep(1000); + } + 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()); + Assert.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/src/java/test/org/apache/zookeeper/test/UpgradeTest.java b/src/java/test/org/apache/zookeeper/test/UpgradeTest.java new file mode 100644 index 0000000..b8db0da --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/UpgradeTest.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.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; + +import java.io.File; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +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.apache.zookeeper.server.upgrade.UpgradeMain; +import org.junit.Assert; +import org.junit.Test; + +public class UpgradeTest extends ZKTestCase implements Watcher { + private final static Logger LOG = LoggerFactory.getLogger(UpgradeTest.class); + + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final File testData = new File( + System.getProperty("test.data.dir", "build/test/data")); + private CountDownLatch startSignal; + + /** + * test the upgrade + * @throws Exception + */ + @Test + public void testUpgrade() throws Exception { + File upgradeDir = new File(testData, "upgrade"); + UpgradeMain upgrade = new UpgradeMain(upgradeDir, upgradeDir); + upgrade.runUpgrade(); + ZooKeeperServer zks = new ZooKeeperServer(upgradeDir, upgradeDir, 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"); + Assert.assertTrue("waiting for server being up", + ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + Stat stat = zk.exists("/", false); + List children = zk.getChildren("/", false); + Collections.sort(children); + for (int i = 0; i < 10; i++) { + Assert.assertTrue("data tree sanity check", + ("test-" + i).equals(children.get(i))); + } + //try creating one node + zk.create("/upgrade", "upgrade".getBytes(), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + // check if its there + if (zk.exists("/upgrade", false) == null) { + Assert.assertTrue(false); + } + + zk.close(); + + // bring down the server + f.shutdown(); + Assert.assertTrue("waiting for server down", + ClientBase.waitForServerDown(HOSTPORT, + ClientBase.CONNECTION_TIMEOUT)); + + } + + 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/src/java/test/org/apache/zookeeper/test/WatchEventWhenAutoReset.java b/src/java/test/org/apache/zookeeper/test/WatchEventWhenAutoReset.java new file mode 100644 index 0000000..eed02c5 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/WatchEventWhenAutoReset.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.test; + +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.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import junit.framework.TestCase; + +public class WatchEventWhenAutoReset extends TestCase { + protected static final Logger LOG = LoggerFactory + .getLogger(WatchEventWhenAutoReset.class); + + // waiting time for expected condition + private static final int TIMEOUT = 30000; + + static public 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); + Assert.assertNotNull("do not receive a " + eventType, event); + Assert.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()); + } + + @Test + public void testNodeDataChanged() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + EventsWatcher watcher = new EventsWatcher(); + ZooKeeper zk1 = createClient(qu, 1, watcher); + ZooKeeper zk2 = createClient(qu, 2); + + String path = "/test-changed"; + + 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); + zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + qu.start(1); + watcher.waitForConnected(TIMEOUT); + watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged); + + zk1.exists(path, watcher); + qu.shutdown(1); + zk2.delete(path, -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.NodeDataChanged); + + qu.shutdownAll(); + } + + @Test + public void testNodeCreated() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + EventsWatcher watcher = new EventsWatcher(); + ZooKeeper zk1 = createClient(qu, 1, watcher); + ZooKeeper zk2 = createClient(qu, 2); + + 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); + + qu.shutdownAll(); + } + + @Test + public void testNodeDeleted() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + EventsWatcher watcher = new EventsWatcher(); + ZooKeeper zk1 = createClient(qu, 1, watcher); + ZooKeeper zk2 = createClient(qu, 2); + + 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); + + qu.shutdownAll(); + } + + @Test + public void testNodeChildrenChanged() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + EventsWatcher watcher = new EventsWatcher(); + ZooKeeper zk1 = createClient(qu, 1, watcher); + ZooKeeper zk2 = createClient(qu, 2); + + 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); + + qu.shutdownAll(); + } +} + diff --git a/src/java/test/org/apache/zookeeper/test/WatchedEventTest.java b/src/java/test/org/apache/zookeeper/test/WatchedEventTest.java new file mode 100644 index 0000000..0eaea67 --- /dev/null +++ b/src/java/test/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 java.util.EnumSet; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.proto.WatcherEvent; +import org.junit.Assert; +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"); + Assert.assertEquals(et, we.getType()); + Assert.assertEquals(ks, we.getState()); + Assert.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); + Assert.assertEquals(et, we.getType()); + Assert.assertEquals(ks, we.getState()); + Assert.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"); + WatchedEvent we = new WatchedEvent(wep); + Assert.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(); + + Assert.assertEquals(EventType.NodeCreated.getIntValue(), wew.getType()); + Assert.assertEquals(KeeperState.Expired.getIntValue(), wew.getState()); + Assert.assertEquals("blah", wew.getPath()); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/WatcherFuncTest.java b/src/java/test/org/apache/zookeeper/test/WatcherFuncTest.java new file mode 100644 index 0000000..a6c62e4 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/WatcherFuncTest.java @@ -0,0 +1,478 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.junit.Assert; +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) { + Assert.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) + { + Assert.assertEquals(expected.get(count), event.getType()); + count++; + } + Assert.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)){ + Assert.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 + { + Assert.assertNull(lsnr.exists("/foo", true)); + Assert.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(); + + Assert.assertNotNull(lsnr.exists("/foo", true)); + Assert.assertNotNull(lsnr.exists("/foo/bar", true)); + + try { + Assert.assertNull(lsnr.exists("/car", true)); + client.setData("/car", "missing".getBytes(), -1); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/car", e.getPath()); + } + + try { + Assert.assertNull(lsnr.exists("/foo/car", true)); + client.setData("/foo/car", "missing".getBytes(), -1); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.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(); + + Assert.assertNotNull(lsnr.exists("/foo", true)); + Assert.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); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo", e.getPath()); + } + try { + lsnr.getData("/foo/bar", true, null); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertNotNull(lsnr.getData("/foo", true, null)); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.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(); + + Assert.assertNotNull(lsnr.getData("/foo", true, null)); + Assert.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); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo", e.getPath()); + } + try { + lsnr.getChildren("/foo/bar", true); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertNotNull(lsnr.getChildren("/foo", true)); + + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeChildrenChanged); // /foo + Assert.assertNotNull(lsnr.getChildren("/foo/bar", true)); + + + client.setData("/foo", "parent".getBytes(), -1); + client.setData("/foo/bar", "child".getBytes(), -1); + + + Assert.assertNotNull(lsnr.exists("/foo", true)); + + Assert.assertNotNull(lsnr.getChildren("/foo", true)); + Assert.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(); + + Assert.assertNull(lsnr.exists("/foo", true)); + Assert.assertNull(lsnr.exists("/foo", w1)); + + Assert.assertNull(lsnr.exists("/foo/bar", w2)); + Assert.assertNull(lsnr.exists("/foo/bar", w3)); + Assert.assertNull(lsnr.exists("/foo/bar", w3)); + Assert.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 + Assert.assertNotNull(lsnr.exists("/foo", w1)); + + Assert.assertNotNull(lsnr.exists("/foo/bar", w2)); + Assert.assertNotNull(lsnr.exists("/foo/bar", w3)); + Assert.assertNotNull(lsnr.exists("/foo/bar", w4)); + Assert.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(); + + Assert.assertNotNull(lsnr.exists("/foo", true)); + Assert.assertNotNull(lsnr.exists("/foo", w1)); + Assert.assertNotNull(lsnr.exists("/foo", w1)); + + Assert.assertNotNull(lsnr.exists("/foo/bar", w2)); + Assert.assertNotNull(lsnr.exists("/foo/bar", w2)); + Assert.assertNotNull(lsnr.exists("/foo/bar", w3)); + Assert.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); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo", e.getPath()); + } + try { + lsnr.getData("/foo/bar", w2, null); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertNotNull(lsnr.getData("/foo", true, null)); + Assert.assertNotNull(lsnr.getData("/foo", w1, null)); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertNotNull(lsnr.getData("/foo/bar", w2, null)); + Assert.assertNotNull(lsnr.getData("/foo/bar", w3, null)); + Assert.assertNotNull(lsnr.getData("/foo/bar", w4, null)); + Assert.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(); + + Assert.assertNotNull(lsnr.getData("/foo", true, null)); + Assert.assertNotNull(lsnr.getData("/foo", w1, null)); + Assert.assertNotNull(lsnr.getData("/foo/bar", w2, null)); + Assert.assertNotNull(lsnr.getData("/foo/bar", w3, null)); + Assert.assertNotNull(lsnr.getData("/foo/bar", w3, null)); + Assert.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); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo", e.getPath()); + } + try { + lsnr.getChildren("/foo/bar", true); + Assert.fail(); + } catch (KeeperException e) { + Assert.assertEquals(KeeperException.Code.NONODE, e.code()); + Assert.assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertNotNull(lsnr.getChildren("/foo", true)); + Assert.assertNotNull(lsnr.getChildren("/foo", w1)); + + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeChildrenChanged); // /foo + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w2)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w2)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w3)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w4)); + + + client.setData("/foo", "parent".getBytes(), -1); + client.setData("/foo/bar", "child".getBytes(), -1); + + + Assert.assertNotNull(lsnr.exists("/foo", true)); + Assert.assertNotNull(lsnr.exists("/foo", w1)); + Assert.assertNotNull(lsnr.exists("/foo", true)); + Assert.assertNotNull(lsnr.exists("/foo", w1)); + + Assert.assertNotNull(lsnr.getChildren("/foo", true)); + Assert.assertNotNull(lsnr.getChildren("/foo", w1)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w2)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w3)); + Assert.assertNotNull(lsnr.getChildren("/foo/bar", w4)); + Assert.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/src/java/test/org/apache/zookeeper/test/WatcherTest.java b/src/java/test/org/apache/zookeeper/test/WatcherTest.java new file mode 100644 index 0000000..1c06690 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/WatcherTest.java @@ -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. + */ + +package org.apache.zookeeper.test; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.ClientCnxn; +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.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.Watcher.Event; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class WatcherTest extends ClientBase { + protected static final Logger LOG = LoggerFactory.getLogger(WatcherTest.class); + + private long timeOfLastWatcherInvocation; + + private 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 + ClientCnxn.setDisableAutoResetWatch(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); + Assert.assertEquals(name, event.getPath()); + Assert.assertEquals(Event.EventType.NodeDataChanged, event.getType()); + Assert.assertEquals(Event.KeeperState.SyncConnected, event.getState()); + event = watcher.events.poll(10, TimeUnit.SECONDS); + Assert.assertEquals(name, event.getPath()); + Assert.assertEquals(Event.EventType.NodeDeleted, event.getType()); + Assert.assertEquals(Event.KeeperState.SyncConnected, 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); + + Assert.assertEquals(ClientBase.getServer(serverFactory) + .getZKDatabase().getDataTree().getWatchCount(), 3); + + } finally { + if(zk1 != null) { + zk1.close(); + } + if(zk2 != null) { + zk2.close(); + } + } + + } + + final static 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); + Assert.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); + Assert.assertEquals(null, zk.exists("/test", false)); + waitForAllWatchers(); + for(int i = 0; i < COUNT/2; i++) { + Assert.assertEquals("For " + i, 1, watches[i].events.size()); + } + for(int i = COUNT/2; i < COUNT; i++) { + if (cbs[i].rc == 0) { + Assert.assertEquals("For " +i, 1, watches[i].events.size()); + } else { + Assert.assertEquals("For " +i, 0, watches[i].events.size()); + } + } + Assert.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 { + ClientCnxn.setDisableAutoResetWatch(true); + testWatcherAutoResetWithGlobal(); + } + + @Test + public void testWatcherAutoResetDisabledWithLocal() throws Exception { + ClientCnxn.setDisableAutoResetWatch(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); + } + + Assert.assertTrue(localWatcher.events.isEmpty()); + + stopServer(); + globalWatcher.waitForDisconnected(3000); + localWatcher.waitForDisconnected(500); + startServer(); + globalWatcher.waitForConnected(3000); + if (!isGlobal && !ClientCnxn.getDisableAutoResetWatch()) { + localWatcher.waitForConnected(500); + } + + Assert.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 (!ClientCnxn.getDisableAutoResetWatch()) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertEquals(e.getPath(), EventType.NodeDataChanged, e.getType()); + Assert.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 (!ClientCnxn.getDisableAutoResetWatch()) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + // The create will trigger the get children and the exist + // watches + Assert.assertEquals(EventType.NodeCreated, e.getType()); + Assert.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 (!ClientCnxn.getDisableAutoResetWatch()) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/watchtest", e.getPath()); + } else { + // we'll catch this later if it does happen after timeout, so + // why waste the time on poll + } + + Assert.assertTrue(localWatcher.events.isEmpty()); // ensure no late arrivals + stopServer(); + globalWatcher.waitForDisconnected(TIMEOUT); + try { + try { + localWatcher.waitForDisconnected(500); + if (!isGlobal && !ClientCnxn.getDisableAutoResetWatch()) { + Assert.fail("Got an event when I shouldn't have"); + } + } catch(TimeoutException toe) { + if (ClientCnxn.getDisableAutoResetWatch()) { + Assert.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); + Assert.assertEquals(EventType.NodeDeleted, e.getType()); + Assert.assertEquals("/watchtest/child2", e.getPath()); + + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertEquals(EventType.NodeChildrenChanged, e.getType()); + Assert.assertEquals("/watchtest", e.getPath()); + + Assert.assertTrue(localWatcher.events.isEmpty()); + + stopServer(); + globalWatcher.waitForDisconnected(TIMEOUT); + localWatcher.waitForDisconnected(500); + startServer(); + globalWatcher.waitForConnected(TIMEOUT); + if (!isGlobal && !ClientCnxn.getDisableAutoResetWatch()) { + localWatcher.waitForConnected(500); + } + + zk.delete("/watchtest/child", -1); + zk.delete("/watchtest", -1); + + if (!ClientCnxn.getDisableAutoResetWatch()) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + Assert.assertEquals(EventType.NodeDeleted, e.getType()); + Assert.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); + Assert.assertTrue(localWatcher.events.isEmpty()); + + } + +} diff --git a/src/java/test/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.java b/src/java/test/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.java new file mode 100644 index 0000000..c213b2a --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.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.test; + +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.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.server.SyncRequestProcessor; +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.Assert; +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 = new ZooKeeper(qb.hostPort, 10000, new Watcher() { + public void process(WatchedEvent event) { + }}); + 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; + } + + Assert.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(); + Assert.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]; + Assert.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); + } + + +} diff --git a/src/java/test/org/apache/zookeeper/test/ZooKeeperQuotaTest.java b/src/java/test/org/apache/zookeeper/test/ZooKeeperQuotaTest.java new file mode 100644 index 0000000..fbf09b3 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ZooKeeperQuotaTest.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.test; + +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.ZooKeeper; +import org.apache.zookeeper.ZooKeeperMain; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZooKeeperQuotaTest extends ClientBase { + + + private static final Logger LOG = LoggerFactory.getLogger( + ZooKeeperQuotaTest.class); + + @Test + public void testQuota() throws IOException, + InterruptedException, KeeperException, 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); + ZooKeeperMain.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)); + Assert.assertTrue("bytes are set", st.getBytes() == 5L); + Assert.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)); + Assert.assertTrue("bytes are set", qst.getBytes() == 8L); + Assert.assertTrue("count is set", qst.getCount() == 2); + + //force server to restart and load from snapshot, not txn log + stopServer(); + startServer(); + stopServer(); + startServer(); + ZooKeeperServer server = getServer(serverFactory); + Assert.assertNotNull("Quota is still set", + server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null); + } +} diff --git a/src/java/test/org/apache/zookeeper/test/ZooKeeperTestClient.java b/src/java/test/org/apache/zookeeper/test/ZooKeeperTestClient.java new file mode 100644 index 0000000..0bbba61 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/ZooKeeperTestClient.java @@ -0,0 +1,422 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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.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.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; + +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)) { + Assert.fail("children lists from getChildren()/getChildren2() do not match"); + } + + if (!stat.equals(stat)) { + Assert.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) { + Assert.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) { + Assert.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) { + Assert.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) { + Assert.fail("Unexpected exception code for createin: " + ke.getMessage()); + } + } + + stat = zk.exists(nodeName, false); + if (stat == null) { + Assert.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) { + Assert.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) { + Assert.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)) { + Assert.fail("children lists from getChildren()/getChildren2() do not match"); + } + + if (!stat_parent.equals(stat)) { + Assert.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) { + Assert.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)) { + Assert.fail("Unexpected event was delivered: " + event.toString()); + } + + stat_node = zk_1.exists(nodeName, false); + if (stat_node == null) { + Assert.fail("node " + nodeName + " should exist"); + } + + try { + zk.delete(parentName, -1); + Assert.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) { + Assert.fail("Unexpected exception code for delete: " + code); + } + } + + try { + zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + Assert.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) { + Assert.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)) { + Assert.fail("children lists from getChildren()/getChildren2() does not match"); + } + + if (children1.size() > 0) { + Assert.fail("ephemeral node " + nodeName + " should not have children"); + } + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NONODE; + if (!valid) { + Assert.fail("Unexpected exception code for createin: " + code); + } + } + firstGen1 = zk_1.getChildren(parentName, true); + firstGen2 = zk_1.getChildren(parentName, true, null); + + if (!firstGen1.equals(firstGen2)) { + Assert.fail("children list from getChildren()/getChildren2() does not match"); + } + + stat_node = zk_1.exists(nodeName, true); + if (stat_node == null) { + Assert.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(); + + Stat no_stat = 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); + Assert.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); + Assert.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) { + Assert.fail("node " + nodeName + " should have been deleted"); + } + if (firstGen1.contains(nodeName)) { + Assert.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) { + Assert.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) { + Assert.fail("Unexpected exception code for create: " + ke.getMessage()); + } + } + try { + zk.setData(nodeName, "hi".getBytes(), 5700); + Assert.fail("Should have gotten BadVersion exception"); + } catch (KeeperException ke) { + if (ke.code() != Code.BADVERSION) { + Assert.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)) { + Assert.fail("The retrieved data [" + retrieved + + "] is differented than the expected [hi]"); + } + try { + zk.delete(nodeName, 6800); + Assert.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) { + Assert.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) { + Assert.fail("Unexpected exception code for delete: " + code); + } + } + deleteZKDir(zk, nodeName); + zk.close(); + } + + public void my_test_1() throws IOException, + InterruptedException, KeeperException { + enode_test_1(); + enode_test_2(); + delete_create_get_set_test_1(); + } + + synchronized public 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/src/lastRevision.bat b/src/lastRevision.bat new file mode 100644 index 0000000..6899947 --- /dev/null +++ b/src/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/src/lastRevision.sh b/src/lastRevision.sh new file mode 100644 index 0000000..0690c7d --- /dev/null +++ b/src/lastRevision.sh @@ -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. + +# Find the current revision, store it in a file +FILE=$1 +LASTREV=`git rev-parse HEAD` + +echo "lastRevision=${LASTREV}" > $FILE diff --git a/src/packages/deb/init.d/zookeeper b/src/packages/deb/init.d/zookeeper new file mode 100644 index 0000000..d0f7216 --- /dev/null +++ b/src/packages/deb/init.d/zookeeper @@ -0,0 +1,140 @@ +#! /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. + +### BEGIN INIT INFO +# Provides: zookeeper +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Apache ZooKeeper server +### END INIT INFO + +set -e + +# /etc/init.d/zookeeper: start and stop the Apache ZooKeeper daemon + +umask 022 + +. /usr/libexec/zkEnv.sh + +. /lib/lsb/init-functions + +ZOOPIDDIR=/var/lib/zookeeper/data +ZOOPIDFILE=${ZOOPIDDIR}/zookeeper_server.pid + +check_privsep_dir() { + # Create the PrivSep empty dir if necessary + if [ ! -d ${ZOOPIDDIR} ]; then + mkdir -p ${ZOOPIDDIR} + chown zookeeper:hadoop ${ZOOPIDDIR} + chmod 0775 ${ZOOPIDDIR} + fi +} + +# Are we running from init? +run_by_init() { + ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ] +} + +check_for_no_start() { + # forget it if we're trying to start, and /etc/zookeeper/zookeeper_not_to_be_run exists + if [ -e /etc/zookeeper/zookeeper_not_to_be_run ]; then + if [ "$1" = log_end_msg ]; then + log_end_msg 0 + fi + if ! run_by_init; then + log_action_msg "Apache ZooKeeper server not in use (/etc/zookeeper/zookeeper_not_to_be_run)" + fi + exit 0 + fi +} + +export PATH="${PATH:+$PATH:}/usr/sbin:/usr/bin" + +case "$1" in + start) + check_for_no_start + check_privsep_dir + log_daemon_msg "Starting Apache ZooKeeper server" "zookeeper" + if start-stop-daemon --start --quiet --oknodo --pidfile ${ZOOPIDFILE} -c zookeeper -x ${ZOOKEEPER_PREFIX}/sbin/zkServer.sh start; then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; + stop) + log_daemon_msg "Stopping Apache ZooKeeper server" "zookeeper" + if start-stop-daemon --stop --quiet --oknodo --pidfile ${ZOOPIDFILE}; then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; + + restart) + check_privsep_dir + log_daemon_msg "Restarting Apache ZooKeeper server" "zookeeper" + start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile ${ZOOPIDFILE} + check_for_no_start log_end_msg + if start-stop-daemon --start --quiet --oknodo --pidfile ${ZOOPIDFILE} -c zookeeper -x ${ZOOKEEPER_PREFIX}/sbin/zkServer.sh start; then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; + + try-restart) + check_privsep_dir + log_daemon_msg "Restarting Apache ZooKeeper server" "zookeeper" + set +e + start-stop-daemon --stop --quiet --retry 30 --pidfile ${ZOOPIDFILE} + RET="$?" + set -e + case $RET in + 0) + # old daemon stopped + check_for_no_start log_end_msg + if start-stop-daemon --start --quiet --oknodo --pidfile ${ZOOPIDFILE} -c zookeeper -x ${ZOOKEEPER_PREFIX}/sbin/zkServer.sh start; then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; + 1) + # daemon not running + log_progress_msg "(not running)" + log_end_msg 0 + ;; + *) + # failed to stop + log_progress_msg "(failed to stop)" + log_end_msg 1 + ;; + esac + ;; + + status) + status_of_proc -p ${ZOOPIDFILE} ${JAVA_HOME}/bin/java zookeeper && exit 0 || exit $? + ;; + + *) + log_action_msg "Usage: /etc/init.d/zookeeper {start|stop|restart|try-restart|status}" + exit 1 +esac + +exit 0 diff --git a/src/packages/deb/zookeeper.control/conffile b/src/packages/deb/zookeeper.control/conffile new file mode 100644 index 0000000..6fdd65b --- /dev/null +++ b/src/packages/deb/zookeeper.control/conffile @@ -0,0 +1,15 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +/etc/zookeeper diff --git a/src/packages/deb/zookeeper.control/control b/src/packages/deb/zookeeper.control/control new file mode 100644 index 0000000..c746906 --- /dev/null +++ b/src/packages/deb/zookeeper.control/control @@ -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. +Package: zookeeper +Version: @version@ +Section: misc +Priority: optional +Architecture: all +Depends: sun-java6-jre +Maintainer: Apache Software Foundation +Description: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. +Distribution: development diff --git a/src/packages/deb/zookeeper.control/postinst b/src/packages/deb/zookeeper.control/postinst new file mode 100644 index 0000000..a6cbd22 --- /dev/null +++ b/src/packages/deb/zookeeper.control/postinst @@ -0,0 +1,23 @@ +#!/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. + +bash /usr/sbin/update-zookeeper-env.sh \ + --prefix=/usr \ + --conf-dir=/etc/zookeeper \ + --log-dir=/var/log/zookeeper \ + --pid-dir=/var/run/zookeeper \ + --var-dir=/var/lib/zookeeper diff --git a/src/packages/deb/zookeeper.control/postrm b/src/packages/deb/zookeeper.control/postrm new file mode 100644 index 0000000..27842b7 --- /dev/null +++ b/src/packages/deb/zookeeper.control/postrm @@ -0,0 +1,19 @@ +#!/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. + +/usr/sbin/userdel zookeeper 2> /dev/null >/dev/null +exit 0 diff --git a/src/packages/deb/zookeeper.control/preinst b/src/packages/deb/zookeeper.control/preinst new file mode 100644 index 0000000..d4ca7f7 --- /dev/null +++ b/src/packages/deb/zookeeper.control/preinst @@ -0,0 +1,20 @@ +#!/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. + +getent group hadoop 2>/dev/null >/dev/null || /usr/sbin/groupadd -r hadoop + +/usr/sbin/useradd --comment "ZooKeeper" --shell /bin/bash -M -r --groups hadoop --home /usr/share/zookeeper zookeeper 2> /dev/null || : diff --git a/src/packages/deb/zookeeper.control/prerm b/src/packages/deb/zookeeper.control/prerm new file mode 100644 index 0000000..c986688 --- /dev/null +++ b/src/packages/deb/zookeeper.control/prerm @@ -0,0 +1,24 @@ +#!/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. + +/etc/init.d/zookeeper stop 2>/dev/null >/dev/null +bash /usr/sbin/update-zookeeper-env.sh \ + --prefix=/usr \ + --conf-dir=/etc/zookeeper \ + --log-dir=/var/log/zookeeper \ + --pid-dir=/var/run/zookeeper \ + --uninstal diff --git a/src/packages/rpm/init.d/zookeeper b/src/packages/rpm/init.d/zookeeper new file mode 100644 index 0000000..faeebb7 --- /dev/null +++ b/src/packages/rpm/init.d/zookeeper @@ -0,0 +1,84 @@ +#!/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. + +# +# ZooKeeper +# +# chkconfig: 2345 89 9 +# description: zookeeper + +source /etc/rc.d/init.d/functions +source /usr/libexec/zkEnv.sh + +RETVAL=0 +PIDFILE="${ZOOPIDFILE}" +desc="ZooKeeper daemon" + +start() { + echo -n $"Starting $desc (zookeeper): " + daemon --user zookeeper zkServer.sh start + RETVAL=$? + echo + [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zookeeper + return $RETVAL +} + +stop() { + echo -n $"Stopping $desc (zookeeper): " + daemon --user zookeeper zkServer.sh stop + RETVAL=$? + sleep 5 + echo + [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zookeeper $PIDFILE +} + +restart() { + stop + start +} + +checkstatus(){ + status -p $PIDFILE ${JAVA_HOME}/bin/java + RETVAL=$? +} + +condrestart(){ + [ -e /var/lock/subsys/zookeeper ] && restart || : +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + checkstatus + ;; + restart) + restart + ;; + condrestart) + condrestart + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart}" + exit 1 +esac + +exit $RETVAL diff --git a/src/packages/rpm/spec/zookeeper.spec b/src/packages/rpm/spec/zookeeper.spec new file mode 100644 index 0000000..8e195c7 --- /dev/null +++ b/src/packages/rpm/spec/zookeeper.spec @@ -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. + +# +# RPM Spec file for ZooKeeper version @version@ +# + +%define name zookeeper +%define version @version@ +%define release @package.release@ + +# Installation Locations +%define _prefix @package.prefix@ +%define _bin_dir %{_prefix}/bin +%define _conf_dir @package.conf.dir@ +%define _include_dir %{_prefix}/include +%define _lib_dir %{_prefix}/lib +%define _lib64_dir %{_prefix}/lib64 +%define _libexec_dir %{_prefix}/libexec +%define _log_dir @package.log.dir@ +%define _man_dir %{_prefix}/man +%define _pid_dir @package.pid.dir@ +%define _sbin_dir %{_prefix}/sbin +%define _share_dir %{_prefix}/share/zookeeper +%define _src_dir %{_prefix}/src +%define _var_dir @package.var.dir@ + +# Build time settings +%define _build_dir @package.build.dir@ +%define _final_name @final.name@ +%define _c_lib @c.lib@ +%define debug_package %{nil} + +# Disable brp-java-repack-jars for aspect J +%define __os_install_post \ + /usr/lib/rpm/redhat/brp-compress \ + %{!?__debug_package:/usr/lib/rpm/redhat/brp-strip %{__strip}} \ + /usr/lib/rpm/redhat/brp-strip-static-archive %{__strip} \ + /usr/lib/rpm/redhat/brp-strip-comment-note %{__strip} %{__objdump} \ + /usr/lib/rpm/brp-python-bytecompile %{nil} + +# RPM searches perl files for dependancies and this breaks for non packaged perl lib +# like thrift so disable this +%define _use_internal_dependency_generator 0 + +Summary: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. +License: Apache License, Version 2.0 +URL: http://zookeeper.apache.org/ +Vendor: Apache Software Foundation +Group: Development/Libraries +Name: %{name} +Version: %{version} +Release: %{release} +Source0: %{_final_name}.tar.gz +Source1: %{_final_name}-lib.tar.gz +Prefix: %{_prefix} +Prefix: %{_conf_dir} +Prefix: %{_log_dir} +Prefix: %{_pid_dir} +Prefix: %{_var_dir} +Requires: sh-utils, textutils, /usr/sbin/useradd, /usr/sbin/usermod, /sbin/chkconfig, /sbin/service, jdk >= 1.6 +AutoReqProv: no +Provides: zookeeper + +%description +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. + +%package lib +Summary: ZooKeeper C binding library +Group: System/Libraries +#Requires: %{name} == %{version} +Provides: zookeeper-lib + +%description lib +ZooKeeper C client library for communicating with ZooKeeper Server. + +%prep +%setup -D -b 1 -n %{_final_name} +%setup -D -a 0 -n %{_final_name} + +%build +mkdir -p ${RPM_BUILD_DIR}%{_prefix} +mkdir -p ${RPM_BUILD_DIR}%{_bin_dir} +mkdir -p ${RPM_BUILD_DIR}%{_include_dir} +mkdir -p ${RPM_BUILD_DIR}%{_lib_dir} +%ifarch amd64 x86_64 +mkdir -p ${RPM_BUILD_DIR}%{_lib64_dir} +%endif +mkdir -p ${RPM_BUILD_DIR}%{_libexec_dir} +mkdir -p ${RPM_BUILD_DIR}%{_log_dir} +mkdir -p ${RPM_BUILD_DIR}%{_conf_dir} +mkdir -p ${RPM_BUILD_DIR}%{_man_dir} +mkdir -p ${RPM_BUILD_DIR}%{_pid_dir} +mkdir -p ${RPM_BUILD_DIR}%{_sbin_dir} +mkdir -p ${RPM_BUILD_DIR}%{_share_dir} +mkdir -p ${RPM_BUILD_DIR}%{_var_dir} +mkdir -p ${RPM_BUILD_DIR}/etc/init.d + +cp ${RPM_BUILD_DIR}/%{_final_name}/src/packages/rpm/init.d/zookeeper ${RPM_BUILD_DIR}/etc/init.d/zookeeper +cp ${RPM_BUILD_DIR}/%{_final_name}/src/packages/update-zookeeper-env.sh ${RPM_BUILD_DIR}/%{_final_name}/sbin/update-zookeeper-env.sh +chmod 0755 ${RPM_BUILD_DIR}/%{_final_name}/sbin/* +chmod 0755 ${RPM_BUILD_DIR}/etc/init.d/zookeeper + +######################### +#### INSTALL SECTION #### +######################### +%install +pushd ${RPM_BUILD_DIR} +mv ${RPM_BUILD_DIR}/%{_final_name}/bin/* ${RPM_BUILD_DIR}%{_bin_dir} +mv ${RPM_BUILD_DIR}/%{_final_name}/libexec/* ${RPM_BUILD_DIR}%{_libexec_dir} +mv ${RPM_BUILD_DIR}/%{_final_name}/share/zookeeper/* ${RPM_BUILD_DIR}%{_share_dir} +mv ${RPM_BUILD_DIR}/%{_final_name}/conf/* ${RPM_BUILD_DIR}%{_conf_dir} +mv ${RPM_BUILD_DIR}/%{_final_name}/sbin/* ${RPM_BUILD_DIR}%{_sbin_dir} +cp -f ${RPM_BUILD_DIR}%{_conf_dir}/zoo_sample.cfg ${RPM_BUILD_DIR}%{_conf_dir}/zoo.cfg +popd ${RPM_BUILD_DIR} +rm -rf ${RPM_BUILD_DIR}/%{_final_name} + +%pre +getent group hadoop 2>/dev/null >/dev/null || /usr/sbin/groupadd -r hadoop + +/usr/sbin/useradd --comment "ZooKeeper" --shell /bin/bash -M -r --groups hadoop --home %{_share_dir} zookeeper 2> /dev/null || : + +%post +bash ${RPM_INSTALL_PREFIX0}/sbin/update-zookeeper-env.sh \ + --prefix=${RPM_INSTALL_PREFIX0} \ + --conf-dir=${RPM_INSTALL_PREFIX1} \ + --log-dir=${RPM_INSTALL_PREFIX2} \ + --pid-dir=${RPM_INSTALL_PREFIX3} \ + --var-dir=${RPM_INSTALL_PREFIX4} + +%preun +bash ${RPM_INSTALL_PREFIX0}/sbin/update-zookeeper-env.sh \ + --prefix=${RPM_INSTALL_PREFIX0} \ + --conf-dir=${RPM_INSTALL_PREFIX1} \ + --log-dir=${RPM_INSTALL_PREFIX2} \ + --pid-dir=${RPM_INSTALL_PREFIX3} \ + --var-dir=${RPM_INSTALL_PREFIX4} \ + --uninstall + +%files +%defattr(-,root,root) +%attr(0755,root,hadoop) %{_log_dir} +%attr(0775,root,hadoop) %{_pid_dir} +%attr(0775,root,hadoop) /etc/init.d/zookeeper +%config(noreplace) %{_conf_dir}/* +%{_prefix} + +%post lib +/sbin/ldconfig + +%files lib +%defattr(-,root,root) +%{_prefix}/lib/* +%{_prefix}/bin diff --git a/src/packages/templates/conf/zookeeper-env.sh b/src/packages/templates/conf/zookeeper-env.sh new file mode 100644 index 0000000..a9efb63 --- /dev/null +++ b/src/packages/templates/conf/zookeeper-env.sh @@ -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. +export JAVA_HOME=${JAVA_HOME} +export ZOO_LOG_DIR=${LOG_DIR} diff --git a/src/packages/update-zookeeper-env.sh b/src/packages/update-zookeeper-env.sh new file mode 100644 index 0000000..8428487 --- /dev/null +++ b/src/packages/update-zookeeper-env.sh @@ -0,0 +1,163 @@ +#!/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 configures zookeeper-env.sh and zoo.cfg. + +usage() { + echo " +usage: $0 + Required parameters: + --prefix=PREFIX path to install into + + Optional parameters: + --arch=i386 OS Architecture + --conf-dir=/etc/zookeeper Configuration directory + --log-dir=/var/log/zookeeper Log directory + --pid-dir=/var/run PID file location + " + exit 1 +} + +template_generator() { + REGEX='(\$\{[a-zA-Z_][a-zA-Z_0-9]*\})' + cat $1 | + while read line ; do + while [[ "$line" =~ $REGEX ]] ; do + LHS=${BASH_REMATCH[1]} + RHS="$(eval echo "\"$LHS\"")" + line=${line//$LHS/$RHS} + done + echo $line >> $2 + done +} + +OPTS=$(getopt \ + -n $0 \ + -o '' \ + -l 'arch:' \ + -l 'prefix:' \ + -l 'conf-dir:' \ + -l 'log-dir:' \ + -l 'pid-dir:' \ + -l 'var-dir:' \ + -l 'uninstall' \ + -- "$@") + +if [ $? != 0 ] ; then + usage +fi + +eval set -- "${OPTS}" +while true ; do + case "$1" in + --arch) + ARCH=$2 ; shift 2 + ;; + --prefix) + PREFIX=$2 ; shift 2 + ;; + --log-dir) + LOG_DIR=$2 ; shift 2 + ;; + --lib-dir) + LIB_DIR=$2 ; shift 2 + ;; + --conf-dir) + CONF_DIR=$2 ; shift 2 + ;; + --pid-dir) + PID_DIR=$2 ; shift 2 + ;; + --uninstall) + UNINSTALL=1; shift + ;; + --var-dir) + VAR_DIR=$2 ; shift 2 + ;; + --) + shift ; break + ;; + *) + echo "Unknown option: $1" + usage + exit 1 + ;; + esac +done + +for var in PREFIX; do + if [ -z "$(eval "echo \$$var")" ]; then + echo Missing param: $var + usage + fi +done + +ARCH=${ARCH:-i386} +CONF_DIR=${CONF_DIR:-$PREFIX/etc/zookeeper} +LIB_DIR=${LIB_DIR:-$PREFIX/lib} +LOG_DIR=${LOG_DIR:-$PREFIX/var/log} +PID_DIR=${PID_DIR:-$PREFIX/var/run} +VAR_DIR=${VAR_DIR:-$PREFIX/var/lib} +UNINSTALL=${UNINSTALL:-0} + +if [ "${ARCH}" != "i386" ]; then + LIB_DIR=${LIB_DIR}64 +fi + +if [ "${UNINSTALL}" -eq "1" ]; then + # Remove symlinks + if [ -e ${PREFIX}/etc/zookeeper ]; then + rm -f ${PREFIX}/etc/zookeeper + fi +else + # Create symlinks + if [ ${CONF_DIR} != ${PREFIX}/etc/zookeeper ]; then + mkdir -p ${PREFIX}/etc + ln -sf ${CONF_DIR} ${PREFIX}/etc/zookeeper + fi + + mkdir -p ${LOG_DIR} + chown zookeeper:hadoop ${LOG_DIR} + chmod 755 ${LOG_DIR} + + if [ ! -d ${PID_DIR} ]; then + mkdir -p ${PID_DIR} + chown zookeeper:hadoop ${PID_DIR} + chmod 755 ${PID_DIR} + fi + + if [ ! -d ${VAR_DIR} ]; then + mkdir -p ${VAR_DIR}/data + chown -R zookeeper:hadoop ${VAR_DIR} + chmod -R 755 ${VAR_DIR} + fi + + TFILE="/tmp/$(basename $0).$$.tmp" + if [ -z "${JAVA_HOME}" ]; then + if [ -e /etc/debian_version ]; then + JAVA_HOME=/usr/lib/jvm/java-6-sun/jre + else + JAVA_HOME=/usr/java/default + fi + fi + template_generator ${PREFIX}/share/zookeeper/templates/conf/zookeeper-env.sh $TFILE + cp ${TFILE} ${CONF_DIR}/zookeeper-env.sh + rm -f ${TFILE} + template_generator ${PREFIX}/share/zookeeper/templates/conf/zoo.cfg $TFILE + cp ${TFILE} ${CONF_DIR}/zoo.cfg + rm -f ${TFILE} +fi diff --git a/src/pom.template b/src/pom.template new file mode 100644 index 0000000..a02c0b3 --- /dev/null +++ b/src/pom.template @@ -0,0 +1,41 @@ +SKIP_LINE *************************************************************** +SKIP_LINE * Licensed to the Apache Software Foundation (ASF) under one +SKIP_LINE * or more contributor license agreements. See the NOTICE file +SKIP_LINE * distributed with this work for additional information +SKIP_LINE * regarding copyright ownership. The ASF licenses this file +SKIP_LINE * to you under the Apache License, Version 2.0 (the +SKIP_LINE * "License"); you may not use this file except in compliance +SKIP_LINE * with the License. You may obtain a copy of the License at +SKIP_LINE * +SKIP_LINE * http://www.apache.org/licenses/LICENSE-2.0 +SKIP_LINE * +SKIP_LINE * Unless required by applicable law or agreed to in writing, +SKIP_LINE * software distributed under the License is distributed on an +SKIP_LINE * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +SKIP_LINE * KIND, either express or implied. See the License for the +SKIP_LINE * specific language governing permissions and limitations +SKIP_LINE * under the License. +SKIP_LINE *************************************************************** + +${ivy.pom.license} +${ivy.pom.header} + + + 4.0.0 + ${ivy.pom.groupId} + ${ivy.pom.artifactId} + ${ivy.pom.packaging} + ${ivy.pom.version} + ${ivy.pom.name} + ${ivy.pom.description} + ${ivy.pom.url} + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + diff --git a/src/recipes/README.txt b/src/recipes/README.txt new file mode 100644 index 0000000..4f880cd --- /dev/null +++ b/src/recipes/README.txt @@ -0,0 +1,25 @@ +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. lock/. + +3) It would be great if you can provide both the java and c recipes for the zookeeper recipes. +The c recipes go in to recipe-name/src/c and the java implementation goes into 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 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 src/recipes/$recipename/src/c + diff --git a/src/recipes/build-recipes.xml b/src/recipes/build-recipes.xml new file mode 100644 index 0000000..470f593 --- /dev/null +++ b/src/recipes/build-recipes.xml @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/recipes/build.xml b/src/recipes/build.xml new file mode 100644 index 0000000..cd73104 --- /dev/null +++ b/src/recipes/build.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/recipes/election/README.txt b/src/recipes/election/README.txt new file mode 100644 index 0000000..10447ed --- /dev/null +++ b/src/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/src/recipes/election/build.xml b/src/recipes/election/build.xml new file mode 100644 index 0000000..3e1bcb8 --- /dev/null +++ b/src/recipes/election/build.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + diff --git a/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.java b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.java new file mode 100644 index 0000000..6c32ebc --- /dev/null +++ b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.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.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 + */ + public void onElectionEvent(EventType eventType); + +} diff --git a/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java new file mode 100644 index 0000000..8f8ae20 --- /dev/null +++ b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java @@ -0,0 +1,461 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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 logger = 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); + + logger.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 e) { + becomeFailed(e); + return; + } catch (InterruptedException e) { + becomeFailed(e); + return; + } + } + + /** + * Stops all election services, revokes any outstanding leader offers, and + * disconnects from ZooKeeper. + */ + public synchronized void stop() { + state = State.STOP; + dispatchEvent(EventType.STOP_START); + + logger.info("Stopping leader election support"); + + if (leaderOffer != null) { + try { + zooKeeper.delete(leaderOffer.getNodePath(), -1); + logger.info("Removed leader offer {}", leaderOffer.getNodePath()); + } catch (InterruptedException e) { + becomeFailed(e); + } catch (KeeperException e) { + becomeFailed(e); + } + } + + dispatchEvent(EventType.STOP_COMPLETE); + } + + private void makeOffer() throws KeeperException, InterruptedException { + state = State.OFFER; + dispatchEvent(EventType.OFFER_START); + + leaderOffer = new LeaderOffer(); + + leaderOffer.setHostName(hostName); + leaderOffer.setNodePath(zooKeeper.create(rootNodeName + "/" + "n_", + hostName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL_SEQUENTIAL)); + + logger.debug("Created leader offer {}", leaderOffer); + + dispatchEvent(EventType.OFFER_COMPLETE); + } + + private void determineElectionStatus() throws KeeperException, + InterruptedException { + + state = State.DETERMINE; + dispatchEvent(EventType.DETERMINE_START); + + String[] components = leaderOffer.getNodePath().split("/"); + + leaderOffer.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(this.leaderOffer.getId())) { + logger.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 { + dispatchEvent(EventType.READY_START); + + logger.info("{} not elected leader. Watching node:{}", + leaderOffer.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) { + logger.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. + */ + logger + .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); + + logger.info("Becoming leader with node:{}", leaderOffer.getNodePath()); + + dispatchEvent(EventType.ELECTED_COMPLETE); + } + + private void becomeFailed(Exception e) { + logger.error("Failed in state {} - Exception:{}", 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(leaderOffer.getNodePath()) + && state != State.STOP) { + logger.debug( + "Node {} deleted. Need to run through the election process.", + event.getPath()); + try { + determineElectionStatus(); + } catch (KeeperException e) { + becomeFailed(e); + } catch (InterruptedException e) { + becomeFailed(e); + } + } + } + } + + private void dispatchEvent(EventType eventType) { + logger.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:" + leaderOffer + " zooKeeper:" + + zooKeeper + " hostName:" + hostName + " 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 String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + /** + * The type of event. + */ + public static 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 static enum State { + START, OFFER, DETERMINE, ELECTED, READY, FAILED, STOP + } +} diff --git a/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderOffer.java b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderOffer.java new file mode 100644 index 0000000..188a6d5 --- /dev/null +++ b/src/recipes/election/src/java/org/apache/zookeeper/recipes/leader/LeaderOffer.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.recipes.leader; + +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 { + + @Override + public int compare(LeaderOffer o1, LeaderOffer o2) { + return o1.getId().compareTo(o2.getId()); + } + + } + +} diff --git a/src/recipes/election/test/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.java b/src/recipes/election/test/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.java new file mode 100644 index 0000000..571d47f --- /dev/null +++ b/src/recipes/election/test/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.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.recipes.leader; + +import java.io.IOException; +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 junit.framework.Assert; + +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.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LeaderElectionSupportTest extends ClientBase { + + private static final Logger logger = LoggerFactory + .getLogger(LeaderElectionSupportTest.class); + private static final String testRootNode = "/" + System.currentTimeMillis() + + "_"; + + private ZooKeeper zooKeeper; + + @Before + public void setUp() throws Exception { + super.setUp(); + + zooKeeper = createClient(); + + zooKeeper.create(testRootNode + Thread.currentThread().getId(), + new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + @After + public void tearDown() throws Exception { + if (zooKeeper != null) { + zooKeeper.delete(testRootNode + Thread.currentThread().getId(), -1); + } + + super.tearDown(); + } + + @Test + public void testNode() throws IOException, InterruptedException, + KeeperException { + + LeaderElectionSupport electionSupport = createLeaderElectionSupport(); + + electionSupport.start(); + Thread.sleep(3000); + electionSupport.stop(); + } + + @Test + public void testNodes3() throws IOException, InterruptedException, + KeeperException { + + 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 IOException, InterruptedException, + KeeperException { + + 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 IOException, InterruptedException, + KeeperException { + + 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 IOException, InterruptedException, + KeeperException { + + 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 KeeperException, + InterruptedException { + + 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(); + } + + private LeaderElectionSupport createLeaderElectionSupport() { + LeaderElectionSupport electionSupport = new LeaderElectionSupport(); + + electionSupport.setZooKeeper(zooKeeper); + electionSupport.setRootNodeName(testRootNode + + 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() { + + @Override + public void run() { + try { + electionSupport.start(); + Thread.sleep(sleepDuration); + electionSupport.stop(); + + latch.countDown(); + } catch (Exception e) { + logger.warn("Failed to run leader election due to: {}", + e.getMessage()); + failureCounter.incrementAndGet(); + } + } + }; + + t.start(); + + return t; + } + +} diff --git a/src/recipes/lock/README.txt b/src/recipes/lock/README.txt new file mode 100644 index 0000000..1fc4fbf --- /dev/null +++ b/src/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 src/c and read +the INSTALLATION instructions. +Please report any bugs on the jira + +http://issues.apache.org/jira/browse/ZOOKEEPER + + diff --git a/src/recipes/lock/build.xml b/src/recipes/lock/build.xml new file mode 100644 index 0000000..841cc01 --- /dev/null +++ b/src/recipes/lock/build.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + diff --git a/src/recipes/lock/src/c/INSTALL b/src/recipes/lock/src/c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/LICENSE b/src/recipes/lock/src/c/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/Makefile.am b/src/recipes/lock/src/c/Makefile.am new file mode 100644 index 0000000..9b36b43 --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/README.txt b/src/recipes/lock/src/c/README.txt new file mode 100644 index 0000000..326bba8 --- /dev/null +++ b/src/recipes/lock/src/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 src/recipes/lock/src/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/src/recipes/lock/src/c/acinclude.m4 b/src/recipes/lock/src/c/acinclude.m4 new file mode 100644 index 0000000..d0041d8 --- /dev/null +++ b/src/recipes/lock/src/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 fo 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/src/recipes/lock/src/c/aminclude.am b/src/recipes/lock/src/c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/c-doc.Doxyfile b/src/recipes/lock/src/c/c-doc.Doxyfile new file mode 100644 index 0000000..c56e86d --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/configure.ac b/src/recipes/lock/src/c/configure.ac new file mode 100644 index 0000000..3954123 --- /dev/null +++ b/src/recipes/lock/src/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}/../../../../../src/c +ZOOKEEPER_LD=-L${BUILD_PATH}/../../../../../src/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/src/recipes/lock/src/c/include/zoo_lock.h b/src/recipes/lock/src/c/include/zoo_lock.h new file mode 100644 index 0000000..5c06bbf --- /dev/null +++ b/src/recipes/lock/src/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/src/recipes/lock/src/c/src/zoo_lock.c b/src/recipes/lock/src/c/src/zoo_lock.c new file mode 100644 index 0000000..8a6d817 --- /dev/null +++ b/src/recipes/lock/src/c/src/zoo_lock.c @@ -0,0 +1,396 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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; +} + +/** + * unlock the mutex + */ +ZOOAPI int zkr_lock_unlock(zkr_lock_mutex_t *mutex) { + pthread_mutex_lock(&(mutex->pmutex)); + 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(("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; + pthread_mutex_unlock(&(mutex->pmutex)); + return 0; + } + LOG_WARN(("not able to connect to server - giving up")); + pthread_mutex_unlock(&(mutex->pmutex)); + return ZCONNECTIONLOSS; + } + pthread_mutex_unlock(&(mutex->pmutex)); + return ZSYSTEMERROR; +} + +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(strrchr(*a, '-')+1, strrchr(*b, '-')+1); +} + +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 i =0; + for (i=0; i < len; i++) { + if (strcmp(sorted_data[i], element) < 0) { + ret = sorted_data[i]; + } + } + 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(("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(("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(("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(("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(("unable to watch my predecessor")); + ret = zkr_lock_unlock(mutex); + while (ret == 0) { + //we have to give up our leadership + // since we cannot watch out predecessor + ret = zkr_lock_unlock(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(("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 zkr_lock_isowner(mutex); +} + + +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/src/recipes/lock/src/c/tests/TestClient.cc b/src/recipes/lock/src/c/tests/TestClient.cc new file mode 100644 index 0000000..2cc56cf --- /dev/null +++ b/src/recipes/lock/src/c/tests/TestClient.cc @@ -0,0 +1,201 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 + +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/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/LockListener.java b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/LockListener.java new file mode 100644 index 0000000..1c21ad6 --- /dev/null +++ b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/LockListener.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.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 + */ + public void lockAcquired(); + + /** + * call back called when the lock is + * released. + */ + public void lockReleased(); +} diff --git a/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.java b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.java new file mode 100644 index 0000000..4efdb85 --- /dev/null +++ b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.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.recipes.lock; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.apache.zookeeper.recipes.lock.ZooKeeperOperation; + +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * 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); + + protected final ZooKeeper zookeeper; + private AtomicBoolean closed = new AtomicBoolean(false); + private long retryDelay = 500L; + private int retryCount = 10; + 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 < retryCount; i++) { + try { + return operation.execute(); + } catch (KeeperException.SessionExpiredException e) { + LOG.warn("Session expired for: " + zookeeper + " so reconnecting due to: " + e, e); + throw e; + } catch (KeeperException.ConnectionLossException e) { + if (exception == null) { + exception = e; + } + LOG.debug("Attempt " + i + " failed with connection loss so " + + "attempting to reconnect: " + e, e); + 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(new ZooKeeperOperation() { + public boolean execute() throws KeeperException, InterruptedException { + Stat stat = zookeeper.exists(path, false); + if (stat != null) { + return true; + } + zookeeper.create(path, data, acl, flags); + return true; + } + }); + } catch (KeeperException e) { + LOG.warn("Caught: " + e, e); + } catch (InterruptedException e) { + LOG.warn("Caught: " + e, 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.debug("Failed to sleep: " + e, e); + } + } + } +} diff --git a/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/WriteLock.java b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/WriteLock.java new file mode 100644 index 0000000..5caebee --- /dev/null +++ b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/WriteLock.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.recipes.lock; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import static org.apache.zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; + +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * 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 acls 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 LockListener getLockListener() { + return this.callback; + } + + /** + * register a different call back listener + * @param callback the call back instance + */ + public 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 = new ZooKeeperOperation() { + public boolean execute() throws KeeperException, + InterruptedException { + zookeeper.delete(id, -1); + return Boolean.TRUE; + } + }; + zopdel.execute(); + } catch (InterruptedException e) { + LOG.warn("Caught: " + e, e); + //set that we have been interrupted. + Thread.currentThread().interrupt(); + } catch (KeeperException.NoNodeException e) { + // do nothing + } catch (KeeperException e) { + LOG.warn("Caught: " + e, e); + throw (RuntimeException) new RuntimeException(e.getMessage()). + initCause(e); + } + finally { + if (callback != null) { + callback.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 on path: " + event.getPath() + " state: " + + event.getState() + " type " + event.getType()); + try { + lock(); + } catch (Exception e) { + LOG.warn("Failed to acquire lock: " + e, 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; + if (LOG.isDebugEnabled()) { + LOG.debug("Found id created last time: " + id); + } + break; + } + } + if (id == null) { + id = zookeeper.create(dir + "/" + prefix, data, + getAcl(), EPHEMERAL_SEQUENTIAL); + + if (LOG.isDebugEnabled()) { + 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 + */ + 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); + } + if (id != null) { + List names = zookeeper.getChildren(dir, false); + if (names.isEmpty()) { + LOG.warn("No children in: " + dir + " when we've just " + + "created one! Lets recreate it..."); + // 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(); + if (LOG.isDebugEnabled()) { + 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()) { + if (callback != null) { + callback.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 && ownerId != 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/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZNodeName.java b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZNodeName.java new file mode 100644 index 0000000..2e32e59 --- /dev/null +++ b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZNodeName.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.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 misdetected a sequence suffix, + // so return -1. + } catch (NumberFormatException e) { + LOG.info("Number format exception for " + idx, e); + } catch (ArrayIndexOutOfBoundsException e) { + LOG.info("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; + + if (!name.equals(sequence.name)) return false; + + return true; + } + + @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/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.java b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.java new file mode 100644 index 0000000..54317ed --- /dev/null +++ b/src/recipes/lock/src/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.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.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 + */ + public boolean execute() throws KeeperException, InterruptedException; +} diff --git a/src/recipes/lock/test/org/apache/zookeeper/recipes/lock/WriteLockTest.java b/src/recipes/lock/test/org/apache/zookeeper/recipes/lock/WriteLockTest.java new file mode 100644 index 0000000..52f9f57 --- /dev/null +++ b/src/recipes/lock/test/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 org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +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/src/recipes/lock/test/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java b/src/recipes/lock/test/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java new file mode 100644 index 0000000..773c2ec --- /dev/null +++ b/src/recipes/lock/test/org/apache/zookeeper/recipes/lock/ZNodeNameTest.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.recipes.lock; + +import junit.framework.TestCase; + +import java.util.SortedSet; +import java.util.TreeSet; + +import org.junit.Test; + +/** + * test for znodenames + */ +public class ZNodeNameTest extends TestCase { + @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)); + } + assertEquals("The SortedSet does not have the expected size!", nodeNames.size(), expected.length); + + int index = 0; + for (ZNodeName nodeName : nodeNames) { + String name = nodeName.getName(); + assertEquals("Node " + index, expected[index++], name); + } + } + +} diff --git a/src/recipes/queue/README.txt b/src/recipes/queue/README.txt new file mode 100644 index 0000000..d59a3c3 --- /dev/null +++ b/src/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/src/recipes/queue/build.xml b/src/recipes/queue/build.xml new file mode 100644 index 0000000..0f3505a --- /dev/null +++ b/src/recipes/queue/build.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + diff --git a/src/recipes/queue/src/c/INSTALL b/src/recipes/queue/src/c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/LICENSE b/src/recipes/queue/src/c/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/Makefile.am b/src/recipes/queue/src/c/Makefile.am new file mode 100644 index 0000000..ecef3cc --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/README.txt b/src/recipes/queue/src/c/README.txt new file mode 100644 index 0000000..7ef253f --- /dev/null +++ b/src/recipes/queue/src/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 src/recipes/queue/src/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/src/recipes/queue/src/c/acinclude.m4 b/src/recipes/queue/src/c/acinclude.m4 new file mode 100644 index 0000000..d0041d8 --- /dev/null +++ b/src/recipes/queue/src/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 fo 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/src/recipes/queue/src/c/aminclude.am b/src/recipes/queue/src/c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/c-doc.Doxyfile b/src/recipes/queue/src/c/c-doc.Doxyfile new file mode 100644 index 0000000..32e09a1 --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/configure.ac b/src/recipes/queue/src/c/configure.ac new file mode 100644 index 0000000..a9fb7b1 --- /dev/null +++ b/src/recipes/queue/src/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}/../../../../../src/c +ZOOKEEPER_LD=-L${BUILD_PATH}/../../../../../src/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/src/recipes/queue/src/c/include/zoo_queue.h b/src/recipes/queue/src/c/include/zoo_queue.h new file mode 100644 index 0000000..ccc4602 --- /dev/null +++ b/src/recipes/queue/src/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(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/src/recipes/queue/src/c/src/zoo_queue.c b/src/recipes/queue/src/c/src/zoo_queue.c new file mode 100644 index 0000000..89ec24b --- /dev/null +++ b/src/recipes/queue/src/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(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(("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(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(("Error! Called await twice.")); + break; + case take_not_needed: + LOG_DEBUG(("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(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(("Error! Can't mark unneeded after waiting.")); + break; + case take_not_needed: + LOG_DEBUG(("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(latch); +} + + + +ZOOAPI int zkr_queue_take(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(take_latch); + }else{ + take_latch_waiter_mark_unneeded(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/src/recipes/queue/src/c/tests/TestClient.cc b/src/recipes/queue/src/c/tests/TestClient.cc new file mode 100644 index 0000000..5446d9b --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/c/tests/TestDriver.cc b/src/recipes/queue/src/c/tests/TestDriver.cc new file mode 100644 index 0000000..2b818f4 --- /dev/null +++ b/src/recipes/queue/src/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/src/recipes/queue/src/java/org/apache/zookeeper/recipes/queue/DistributedQueue.java b/src/recipes/queue/src/java/org/apache/zookeeper/recipes/queue/DistributedQueue.java new file mode 100644 index 0000000..c5d7c83 --- /dev/null +++ b/src/recipes/queue/src/java/org/apache/zookeeper/recipes/queue/DistributedQueue.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.recipes.queue; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.TreeMap; +import java.util.concurrent.CountDownLatch; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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.apache.zookeeper.data.Stat; + +/** + * + * 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 TreeMap orderedChildren(Watcher watcher) throws KeeperException, InterruptedException { + TreeMap orderedChildren = new TreeMap(); + + List childNames = null; + try{ + childNames = zookeeper.getChildren(dir, watcher); + }catch (KeeperException.NoNodeException e){ + throw e; + } + + 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 = new Long(suffix); + orderedChildren.put(childId,childName); + }catch(NumberFormatException e){ + LOG.warn("Found child node with improper format : " + childName + " " + e,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 = null; + + try{ + childNames = zookeeper.getChildren(dir, false); + }catch(KeeperException.NoNodeException e){ + LOG.warn("Caught: " +e,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,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 { + TreeMap 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 { + TreeMap 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 class LatchChildWatcher implements Watcher { + + CountDownLatch latch; + + public LatchChildWatcher(){ + latch = new CountDownLatch(1); + } + + public void process(WatchedEvent event){ + LOG.debug("Watcher fired on path: " + event.getPath() + " state: " + + event.getState() + " type " + event.getType()); + 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 { + TreeMap 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/src/recipes/queue/test/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java b/src/recipes/queue/test/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java new file mode 100644 index 0000000..c6cfae2 --- /dev/null +++ b/src/recipes/queue/test/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java @@ -0,0 +1,286 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF 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; + + + +public class DistributedQueueTest extends ClientBase { + + @After + public void tearDown() throws Exception { + super.tearDown(); + LOG.info("FINISHED " + getTestName()); + } + + + @Test + public void testOffer1() throws Exception { + String dir = "/testOffer1"; + String testString = "Hello World"; + final int num_clients = 1; + ZooKeeper clients[] = new ZooKeeper[num_clients]; + DistributedQueue queueHandles[] = new DistributedQueue[num_clients]; + 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 num_clients = 2; + ZooKeeper clients[] = new ZooKeeper[num_clients]; + DistributedQueue queueHandles[] = new DistributedQueue[num_clients]; + 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 num_clients = 1; + ZooKeeper clients[] = new ZooKeeper[num_clients]; + DistributedQueue queueHandles[] = new DistributedQueue[num_clients]; + 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"; + String testString = "Hello World"; + final int num_clients = 1; + ZooKeeper clients[] = new ZooKeeper[num_clients]; + DistributedQueue queueHandles[] = new DistributedQueue[num_clients]; + 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.assertTrue(false); + } + + public void createNremoveMtest(String dir,int n,int m) throws Exception{ + String testString = "Hello World"; + final int num_clients = 2; + ZooKeeper clients[] = new ZooKeeper[num_clients]; + DistributedQueue queueHandles[] = new DistributedQueue[num_clients]; + 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; idataWatches; + vectorexistWatches; + vectorchildWatches; + } + 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 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 DeleteRequest { + ustring path; + int version; + } + class GetChildrenRequest { + ustring path; + boolean watch; + } + 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 WatcherEvent { + int type; // event type + int state; // state of the Keeper client runtime + ustring path; + } + class ErrorResponse { + int err; + } + class CreateResponse { + ustring path; + } + 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 GetChildren2Response { + vector children; + org.apache.zookeeper.data.Stat stat; + } + class GetACLResponse { + vector acl; + org.apache.zookeeper.data.Stat stat; + } +} + +module org.apache.zookeeper.server.quorum { + class LearnerInfo { + long serverid; + int protocolVersion; + } + 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 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 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 ErrorTxn { + int err; + } + class Txn { + int type; + buffer data; + } + class MultiTxn { + vector txns; + } +}