private async Task Watcher_OnChange(string path, Watcher.Event.KeeperState keeperState, Watcher.Event.EventType eventType)
        {
            switch (keeperState)
            {
            case Watcher.Event.KeeperState.Expired:
                Logger.LogWarning($"ZooKeeper has been {keeperState},Reconnecting...");
                ZooKeeperSessionId = 0;
                CreateZooKeeperClient();
                break;

            case Watcher.Event.KeeperState.Disconnected:
                Logger.LogWarning($"ZooKeeper has been {keeperState},Reconnecting...");
                CreateZooKeeperClient();
                break;

            case Watcher.Event.KeeperState.SyncConnected:
                if (ZooKeeperSessionId == 0)
                {
                    ZooKeeperSessionId = ZooKeeper.getSessionId();
                }
                await SubscribeNodes(path, eventType);

                break;
            }
        }
Example #2
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));
        }
Example #3
0
        /**
         * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent
         */

        internal WatchedEvent(WatcherEvent eventMessage)
        {
            keeperState = EnumUtil <Watcher.Event.KeeperState> .DefinedCast(eventMessage.getState());

            eventType = EnumUtil <Watcher.Event.EventType> .DefinedCast(eventMessage.get_Type());

            path = eventMessage.getPath();
        }
Example #4
0
        public void testInvalidIntConversion()
        {
            try {
                Watcher.Event.KeeperState ks = EnumUtil <Watcher.Event.KeeperState> .DefinedCast(324142);

                Assert.fail("Was able to create an invalid KeeperState via an integer");
            }
            catch (Exception)
            {
                // we're good.
            }
        }
        public static ConnectionState ToConnectionState(this Watcher.Event.KeeperState state)
        {
            switch (state)
            {
            case Watcher.Event.KeeperState.SyncConnected:
                return(ConnectionState.Connected);

            case Watcher.Event.KeeperState.ConnectedReadOnly:
                return(ConnectionState.ConnectedReadonly);

            case Watcher.Event.KeeperState.Expired:
                return(ConnectionState.Expired);

            default:
                return(ConnectionState.Disconnected);
            }
        }
Example #6
0
        private bool WaitForKeeperState(Watcher.Event.KeeperState keeperState, TimeSpan timeOut)
        {
            _logHandler.Info($"Waiting for keeper state {Convert.ToString(keeperState)}");


            bool stillWaiting = true;

            while (_currentState != keeperState)
            {
                if (!stillWaiting)
                {
                    return(false);
                }
                stillWaiting = _stateChangedCondition.WaitOne(timeOut);
            }
            _logHandler.Info($"State is {_currentState}");
            return(true);
        }
Example #7
0
        private void ProcessStateChanged(WatchedEvent @event)
        {
            this._currentState = @event.getState();

            this.HandleStateChanged(this._currentState);
            if (@event.getState() != Event.KeeperState.Expired)
            {
                return;
            }
            try
            {
                this.ReConnect(true);
            }
            catch (Exception e)
            {
                _logHandler.Error("Process state changed event error", e);
                this.HandleEstablishmentError(e);
            }
        }
Example #8
0
 internal WatchedEvent(Watcher.Event.EventType eventType, Watcher.Event.KeeperState keeperState, string path)
 {
     this.keeperState = keeperState;
     this.eventType   = eventType;
     this.path        = path;
 }
Example #9
0
 public void HandleStateChanged(Watcher.Event.KeeperState state)
 {
     Debug.WriteLine($"HandleStateChanged: {state.ToString()}");
 }
Example #10
0
 public void HandleStateChanged(Watcher.Event.KeeperState state)
 {
     Logger.Info($"状态变更:{state.ToString()}");
 }