예제 #1
0
        /**
         * Callback invoked by the ClientCnxnSocket once a connection has been
         * established.
         *
         * @param _negotiatedSessionTimeout
         * @param _sessionId
         * @param _sessionPasswd
         * @param isRO
         * @throws IOException
         */

        internal void onConnected(int _negotiatedSessionTimeout, long _sessionId,
                                  byte[] _sessionPasswd, bool isRO)
        {
            negotiatedSessionTimeout.Value = _negotiatedSessionTimeout;
            if (negotiatedSessionTimeout.Value <= 0)
            {
                state.Value = ZooKeeper.States.CLOSED;
                queueEvent(new WatchedEvent(
                               Watcher.Event.EventType.None,
                               Watcher.Event.KeeperState.Expired, null));
                queueEventOfDeath();
                throw new SessionExpiredException("Unable to reconnect to ZooKeeper service, session 0x" +
                                                  sessionId.ToHexString() + " has expired");
            }
            if (!readOnly && isRO)
            {
                LOG.error("Read/write client got connected to read-only server");
            }
            readTimeout    = negotiatedSessionTimeout.Value * 2 / 3;
            connectTimeout = negotiatedSessionTimeout.Value / hostProvider.size();
            hostProvider.onConnected();
            sessionId                 = _sessionId;
            sessionPasswd             = _sessionPasswd;
            state.Value               = (isRO) ? ZooKeeper.States.CONNECTEDREADONLY : ZooKeeper.States.CONNECTED;
            seenRwServerBefore.Value |= !isRO;
            LOG.info("Session establishment complete on server " + clientCnxnSocket.getRemoteSocketAddress() +
                     ", sessionid = 0x" + sessionId.ToHexString()
                     + ", negotiated timeout = " + negotiatedSessionTimeout.Value
                     + (isRO ? " (READ-ONLY mode)" : ""));
            Watcher.Event.KeeperState eventState = (isRO)
                    ? Watcher.Event.KeeperState.ConnectedReadOnly
                    : Watcher.Event.KeeperState.SyncConnected;
            queueEvent(new WatchedEvent(
                           Watcher.Event.EventType.None, eventState, null));
        }
예제 #2
0
        /**
         * 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
         */

        internal ClientCnxn(string chrootPath, HostProvider hostProvider,
                            int sessionTimeout, ZooKeeper zooKeeper,
                            ClientWatchManager watcher,
                            long sessionId, byte[] sessionPasswd, bool 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;
            clientCnxnSocket    = new ClientCnxnSocketNIO(this);
            state.Value         = ZooKeeper.States.CONNECTING;
        }