You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by SimplyFox <gi...@git.apache.org> on 2017/03/10 12:57:18 UTC

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

GitHub user SimplyFox opened a pull request:

    https://github.com/apache/zookeeper/pull/189

    ZOOKEEPER-169: Content needed: "Connecting to ZooKeeper"

    Added a simply example in C++ to demonstrate Client connecting to
    Zookeeper server using C bindings along with few lines of explanation.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/SimplyFox/zookeeper ZOOKEEPER-169

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/zookeeper/pull/189.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #189
    
----
commit e2cd20b6794d6ef432606faa7ccab0dd95970933
Author: Michelle <ph...@gmail.com>
Date:   2017-03-10T12:16:17Z

    ZOOKEEPER-169: Content needed: "Connecting to ZooKeeper"
    
    Added a simply example in C++ to demonstrate Client connecting to
    Zookeeper server using C bindings along with few lines of explanation.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper issue #189: ZOOKEEPER-169: Content needed: "Connecting to ZooKeepe...

Posted by SimplyFox <gi...@git.apache.org>.
Github user SimplyFox commented on the issue:

    https://github.com/apache/zookeeper/pull/189
  
    Hi @afine ,
    
    Ok, that makes sense. 
    I'll change it to C. Is the example ok?
    
    Thanks,
    Michelle


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/zookeeper/pull/189


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

Posted by SimplyFox <gi...@git.apache.org>.
Github user SimplyFox commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/189#discussion_r105862680
  
    --- Diff: src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml ---
    @@ -1714,7 +1714,115 @@ public abstract class ServerAuthenticationProvider implements AuthenticationProv
         <section id="sc_connectingToZk">
           <title>Connecting to ZooKeeper</title>
     
    -      <para></para>
    +      <para>Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - <emphasis>zookeeper_init</emphasis> with the following signature:</para>
    +
    +      <programlisting><emphasis>int</emphasis> <emphasis>zookeeper_init</emphasis>(<emphasis>const</emphasis> <emphasis>char</emphasis> *host, watcher_fn fn, <emphasis>int</emphasis> recv_timeout, <emphasis>const</emphasis> clientid_t *clientid, <emphasis>void</emphasis> *context, <emphasis>int</emphasis> flags);</programlisting>
    +
    +      <variablelist>
    +        <varlistentry>
    +          <term>*host</term>
    +            <listitem>
    +              <para>Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002"</para>
    +            </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>fn</term>
    +          <listitem>
    +              <para>Watcher function to process events when a notification is triggered.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>recv_timeout</term>
    +          <listitem>
    +              <para>Session expiration time in milliseconds.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*clientid</term>
    +          <listitem>
    +              <para>We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*context</term>
    +          <listitem>
    +              <para>Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>flags</term>
    +          <listitem>
    +              <para>In an initiation, we can leave it for 0.</para>
    +          </listitem>
    +        </varlistentry>
    +      </variablelist>
    +
    +      <para>We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code <emphasis>zkClient.cc</emphasis> :</para>
    +      <programlisting>
    +#include &lt;stdio.h>
    +#include &lt;zookeeper/zookeeper.h>
    +#include &lt;errno.h>
    +using namespace std;
    +
    +// Keeping track of the connection state
    +static int connected = 0;
    +static int expired   = 0;
    +
    +// *zkHandler handles the connection with Zookeeper
    +static zhandle_t *zkHandler;
    +
    +// watcher function would process events
    +void watcher(zhandle_t *zkH, int type, int state, const char *path, void *watcherCtx)
    +{
    +    if (type == ZOO_SESSION_EVENT) {
    +
    +        // state refers to states of zookeeper connection.
    +        // To keep it simple, we would demonstrate these 3: ZOO_EXPIRED_SESSION_STATE, ZOO_CONNECTED_STATE, ZOO_NOTCONNECTED_STATE
    +        // If you are using ACL, you should be aware of an authentication failure state - ZOO_AUTH_FAILED_STATE
    +        if (state == ZOO_CONNECTED_STATE) {
    +            connected = 1;
    +        } else if (state == ZOO_NOTCONNECTED_STATE ) {
    +            connected = 0;
    +        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
    +            expired = 1;
    +            connected = 0;
    +            zookeeper_close(zkH);
    +        }
    +    }
    +}
    +
    +int main(){
    +    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
    +
    +    // zookeeper_init returns the handler upon a successful connection, null otherwise
    +    zkHandler = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    +    
    +    if (!zkHandler) {
    +        return errno;
    +    }else{
    +        printf("Connection established with Zookeeper. \n");
    +    }
    +
    +    // Close Zookeeper connection
    +    zookeeper_close(zkHandler);
    +
    +    return 0;
    +}
    +      </programlisting>
    +
    +      <para>Compile the code with the multithreaded library mentioned before.</para>
    +      <para><command>&gt; g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client</command></para>
    --- End diff --
    
    Will change it to C compiler.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

Posted by SimplyFox <gi...@git.apache.org>.
Github user SimplyFox commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/189#discussion_r105990456
  
    --- Diff: src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml ---
    @@ -1714,7 +1714,115 @@ public abstract class ServerAuthenticationProvider implements AuthenticationProv
         <section id="sc_connectingToZk">
           <title>Connecting to ZooKeeper</title>
     
    -      <para></para>
    +      <para>Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - <emphasis>zookeeper_init</emphasis> with the following signature:</para>
    +
    +      <programlisting><emphasis>int</emphasis> <emphasis>zookeeper_init</emphasis>(<emphasis>const</emphasis> <emphasis>char</emphasis> *host, watcher_fn fn, <emphasis>int</emphasis> recv_timeout, <emphasis>const</emphasis> clientid_t *clientid, <emphasis>void</emphasis> *context, <emphasis>int</emphasis> flags);</programlisting>
    +
    +      <variablelist>
    +        <varlistentry>
    +          <term>*host</term>
    +            <listitem>
    +              <para>Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002"</para>
    +            </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>fn</term>
    +          <listitem>
    +              <para>Watcher function to process events when a notification is triggered.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>recv_timeout</term>
    +          <listitem>
    +              <para>Session expiration time in milliseconds.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*clientid</term>
    +          <listitem>
    +              <para>We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*context</term>
    +          <listitem>
    +              <para>Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>flags</term>
    +          <listitem>
    +              <para>In an initiation, we can leave it for 0.</para>
    +          </listitem>
    +        </varlistentry>
    +      </variablelist>
    +
    +      <para>We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code <emphasis>zkClient.cc</emphasis> :</para>
    +      <programlisting>
    +#include &lt;stdio.h>
    +#include &lt;zookeeper/zookeeper.h>
    +#include &lt;errno.h>
    +using namespace std;
    +
    +// Keeping track of the connection state
    +static int connected = 0;
    +static int expired   = 0;
    +
    +// *zkHandler handles the connection with Zookeeper
    +static zhandle_t *zkHandler;
    +
    +// watcher function would process events
    +void watcher(zhandle_t *zkH, int type, int state, const char *path, void *watcherCtx)
    +{
    +    if (type == ZOO_SESSION_EVENT) {
    +
    +        // state refers to states of zookeeper connection.
    +        // To keep it simple, we would demonstrate these 3: ZOO_EXPIRED_SESSION_STATE, ZOO_CONNECTED_STATE, ZOO_NOTCONNECTED_STATE
    +        // If you are using ACL, you should be aware of an authentication failure state - ZOO_AUTH_FAILED_STATE
    +        if (state == ZOO_CONNECTED_STATE) {
    +            connected = 1;
    +        } else if (state == ZOO_NOTCONNECTED_STATE ) {
    +            connected = 0;
    +        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
    +            expired = 1;
    +            connected = 0;
    +            zookeeper_close(zkH);
    +        }
    +    }
    +}
    +
    +int main(){
    +    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
    +
    +    // zookeeper_init returns the handler upon a successful connection, null otherwise
    +    zkHandler = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    +    
    +    if (!zkHandler) {
    +        return errno;
    +    }else{
    +        printf("Connection established with Zookeeper. \n");
    +    }
    +
    +    // Close Zookeeper connection
    +    zookeeper_close(zkHandler);
    +
    +    return 0;
    +}
    +      </programlisting>
    +
    +      <para>Compile the code with the multithreaded library mentioned before.</para>
    +      <para><command>&gt; g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client</command></para>
    --- End diff --
    
    Btw, I tested the code, and zoo_init doesn't seem to validate the port number. Given an invalid hostname, the zkhandler wont initialize. But given a wrong port number, it'd still work. Should I report it as a bug? or just specify in the documentation that a valid zookeeper port should be given?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

Posted by afine <gi...@git.apache.org>.
Github user afine commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/189#discussion_r106760299
  
    --- Diff: src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml ---
    @@ -1714,7 +1714,115 @@ public abstract class ServerAuthenticationProvider implements AuthenticationProv
         <section id="sc_connectingToZk">
           <title>Connecting to ZooKeeper</title>
     
    -      <para></para>
    +      <para>Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - <emphasis>zookeeper_init</emphasis> with the following signature:</para>
    +
    +      <programlisting><emphasis>int</emphasis> <emphasis>zookeeper_init</emphasis>(<emphasis>const</emphasis> <emphasis>char</emphasis> *host, watcher_fn fn, <emphasis>int</emphasis> recv_timeout, <emphasis>const</emphasis> clientid_t *clientid, <emphasis>void</emphasis> *context, <emphasis>int</emphasis> flags);</programlisting>
    +
    +      <variablelist>
    +        <varlistentry>
    +          <term>*host</term>
    +            <listitem>
    +              <para>Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002"</para>
    +            </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>fn</term>
    +          <listitem>
    +              <para>Watcher function to process events when a notification is triggered.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>recv_timeout</term>
    +          <listitem>
    +              <para>Session expiration time in milliseconds.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*clientid</term>
    +          <listitem>
    +              <para>We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*context</term>
    +          <listitem>
    +              <para>Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>flags</term>
    +          <listitem>
    +              <para>In an initiation, we can leave it for 0.</para>
    +          </listitem>
    +        </varlistentry>
    +      </variablelist>
    +
    +      <para>We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code <emphasis>zkClient.cc</emphasis> :</para>
    +      <programlisting>
    +#include &lt;stdio.h>
    +#include &lt;zookeeper/zookeeper.h>
    +#include &lt;errno.h>
    +using namespace std;
    +
    +// Keeping track of the connection state
    +static int connected = 0;
    +static int expired   = 0;
    +
    +// *zkHandler handles the connection with Zookeeper
    +static zhandle_t *zkHandler;
    +
    +// watcher function would process events
    +void watcher(zhandle_t *zkH, int type, int state, const char *path, void *watcherCtx)
    +{
    +    if (type == ZOO_SESSION_EVENT) {
    +
    +        // state refers to states of zookeeper connection.
    +        // To keep it simple, we would demonstrate these 3: ZOO_EXPIRED_SESSION_STATE, ZOO_CONNECTED_STATE, ZOO_NOTCONNECTED_STATE
    +        // If you are using ACL, you should be aware of an authentication failure state - ZOO_AUTH_FAILED_STATE
    +        if (state == ZOO_CONNECTED_STATE) {
    +            connected = 1;
    +        } else if (state == ZOO_NOTCONNECTED_STATE ) {
    +            connected = 0;
    +        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
    +            expired = 1;
    +            connected = 0;
    +            zookeeper_close(zkH);
    +        }
    +    }
    +}
    +
    +int main(){
    +    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
    +
    +    // zookeeper_init returns the handler upon a successful connection, null otherwise
    +    zkHandler = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    +    
    +    if (!zkHandler) {
    +        return errno;
    +    }else{
    +        printf("Connection established with Zookeeper. \n");
    +    }
    +
    +    // Close Zookeeper connection
    +    zookeeper_close(zkHandler);
    +
    +    return 0;
    +}
    +      </programlisting>
    +
    +      <para>Compile the code with the multithreaded library mentioned before.</para>
    +      <para><command>&gt; g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client</command></para>
    --- End diff --
    
    I would file a JIRA for that so we can make the best decision there.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper pull request #189: ZOOKEEPER-169: Content needed: "Connecting to Z...

Posted by afine <gi...@git.apache.org>.
Github user afine commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/189#discussion_r105765768
  
    --- Diff: src/docs/src/documentation/content/xdocs/zookeeperProgrammers.xml ---
    @@ -1714,7 +1714,115 @@ public abstract class ServerAuthenticationProvider implements AuthenticationProv
         <section id="sc_connectingToZk">
           <title>Connecting to ZooKeeper</title>
     
    -      <para></para>
    +      <para>Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - <emphasis>zookeeper_init</emphasis> with the following signature:</para>
    +
    +      <programlisting><emphasis>int</emphasis> <emphasis>zookeeper_init</emphasis>(<emphasis>const</emphasis> <emphasis>char</emphasis> *host, watcher_fn fn, <emphasis>int</emphasis> recv_timeout, <emphasis>const</emphasis> clientid_t *clientid, <emphasis>void</emphasis> *context, <emphasis>int</emphasis> flags);</programlisting>
    +
    +      <variablelist>
    +        <varlistentry>
    +          <term>*host</term>
    +            <listitem>
    +              <para>Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002"</para>
    +            </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>fn</term>
    +          <listitem>
    +              <para>Watcher function to process events when a notification is triggered.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>recv_timeout</term>
    +          <listitem>
    +              <para>Session expiration time in milliseconds.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*clientid</term>
    +          <listitem>
    +              <para>We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>*context</term>
    +          <listitem>
    +              <para>Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0.</para>
    +          </listitem>
    +        </varlistentry>
    +
    +        <varlistentry>
    +          <term>flags</term>
    +          <listitem>
    +              <para>In an initiation, we can leave it for 0.</para>
    +          </listitem>
    +        </varlistentry>
    +      </variablelist>
    +
    +      <para>We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code <emphasis>zkClient.cc</emphasis> :</para>
    +      <programlisting>
    +#include &lt;stdio.h>
    +#include &lt;zookeeper/zookeeper.h>
    +#include &lt;errno.h>
    +using namespace std;
    +
    +// Keeping track of the connection state
    +static int connected = 0;
    +static int expired   = 0;
    +
    +// *zkHandler handles the connection with Zookeeper
    +static zhandle_t *zkHandler;
    +
    +// watcher function would process events
    +void watcher(zhandle_t *zkH, int type, int state, const char *path, void *watcherCtx)
    +{
    +    if (type == ZOO_SESSION_EVENT) {
    +
    +        // state refers to states of zookeeper connection.
    +        // To keep it simple, we would demonstrate these 3: ZOO_EXPIRED_SESSION_STATE, ZOO_CONNECTED_STATE, ZOO_NOTCONNECTED_STATE
    +        // If you are using ACL, you should be aware of an authentication failure state - ZOO_AUTH_FAILED_STATE
    +        if (state == ZOO_CONNECTED_STATE) {
    +            connected = 1;
    +        } else if (state == ZOO_NOTCONNECTED_STATE ) {
    +            connected = 0;
    +        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
    +            expired = 1;
    +            connected = 0;
    +            zookeeper_close(zkH);
    +        }
    +    }
    +}
    +
    +int main(){
    +    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
    +
    +    // zookeeper_init returns the handler upon a successful connection, null otherwise
    +    zkHandler = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
    +    
    +    if (!zkHandler) {
    +        return errno;
    +    }else{
    +        printf("Connection established with Zookeeper. \n");
    +    }
    +
    +    // Close Zookeeper connection
    +    zookeeper_close(zkHandler);
    +
    +    return 0;
    +}
    +      </programlisting>
    +
    +      <para>Compile the code with the multithreaded library mentioned before.</para>
    +      <para><command>&gt; g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client</command></para>
    --- End diff --
    
    can we use a c compiler for consistency?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---