Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 72CAC200C3D for ; Tue, 14 Mar 2017 19:27:46 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 7159A160B63; Tue, 14 Mar 2017 18:27:46 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 939E2160B7E for ; Tue, 14 Mar 2017 19:27:45 +0100 (CET) Received: (qmail 72131 invoked by uid 500); 14 Mar 2017 18:27:44 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 72119 invoked by uid 99); 14 Mar 2017 18:27:44 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Mar 2017 18:27:44 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 3EC12C1104 for ; Tue, 14 Mar 2017 18:27:44 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.452 X-Spam-Level: * X-Spam-Status: No, score=1.452 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_NEUTRAL=0.652, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id ntr3POtdkQz6 for ; Tue, 14 Mar 2017 18:27:43 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 0704B5FAF3 for ; Tue, 14 Mar 2017 18:27:43 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 003D6E0652 for ; Tue, 14 Mar 2017 18:27:42 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id AF216243A8 for ; Tue, 14 Mar 2017 18:27:41 +0000 (UTC) Date: Tue, 14 Mar 2017 18:27:41 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: dev@zookeeper.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (ZOOKEEPER-169) Content needed: "Connecting to ZooKeeper" MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Tue, 14 Mar 2017 18:27:46 -0000 [ https://issues.apache.org/jira/browse/ZOOKEEPER-169?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15924747#comment-15924747 ] ASF GitHub Bot commented on ZOOKEEPER-169: ------------------------------------------ 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
Connecting to ZooKeeper - + Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - zookeeper_init with the following signature: + + int zookeeper_init(const char *host, watcher_fn fn, int recv_timeout, const clientid_t *clientid, void *context, int flags); + + + + *host + + Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002" + + + + + fn + + Watcher function to process events when a notification is triggered. + + + + + recv_timeout + + Session expiration time in milliseconds. + + + + + *clientid + + We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session. + + + + + *context + + Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0. + + + + + flags + + In an initiation, we can leave it for 0. + + + + + We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code zkClient.cc : + +#include <stdio.h> +#include <zookeeper/zookeeper.h> +#include <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; +} + + + Compile the code with the multithreaded library mentioned before. + > g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client --- 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? > Content needed: "Connecting to ZooKeeper" > ----------------------------------------- > > Key: ZOOKEEPER-169 > URL: https://issues.apache.org/jira/browse/ZOOKEEPER-169 > Project: ZooKeeper > Issue Type: Sub-task > Components: documentation > Reporter: Robbie Scott > Original Estimate: 4h > Remaining Estimate: 4h > > We need the content which describes how to a client should connect to a zookeeper server. Ideally this would have pseudo code describing any lead-up (for example resource allocation, etc), a few lines of sample code in Java, and a few parallel lines in C. > Any commentary would be highly appreciated. Stuff like, "You can open an unlimited number of ZooKeeper connections because..." or "Opening too many connections to ZooKeeper is not advisable because..." -- in short, anything that is not obvious to the new ZooKeeper developer. -- This message was sent by Atlassian JIRA (v6.3.15#6346)