public void Process(WatchedEvent @event) { var path = @event.Path; if (@event.Type == EventType.None) { // We are are being told that the state of the // connection has changed switch (@event.State) { case KeeperState.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 KeeperState.Expired: // It's all over Dead = true; _listener.Closing(KeeperException.Code.SESSIONEXPIRED); break; } } else { if (path != null && path.Equals(_znode)) { _zk.Exists(_znode, this); } } if (_chainedWatcher != null) { _chainedWatcher.Process(@event); } }
public void Process(WatchedEvent @event) { //if (@event.Type == EventType.NodeDataChanged) //{ Console.WriteLine("已经触发了" + @event.Type + "事件!" + DateTime.Now); //} }
public void Process(WatchedEvent e) { if (e.Type == EventType.NodeDataChanged) { Console.WriteLine("Path:{0},State:{0},Type:{0},Wrapper:{0}", e.Path, e.State, e.Type, e.Wrapper); //Console.WriteLine(e.Path); } }
void IWatcher.Process(WatchedEvent @event) { if (@event.Type == EventType.NodeDataChanged) lock (syncLock) try { Update(); } catch { } }
public void Process(WatchedEvent @event) { //if (@event.Type == EventType.NodeDataChanged) //{ Console.WriteLine("已经触发了" + @event.Type + @event.ToString() + "事件!Watcher2" + DateTime.Now); Console.Write(string.Join(",", zk.GetChildren("/root", this))); if (zk.Exists("/root/childone", true) != null) { zk.GetData("/root/childone", true, null); } Console.WriteLine(); //} }
private static void ProcessWatcher(IEnumerable <IWatcher> watchers, WatchedEvent watchedEvent) { foreach (IWatcher watcher in watchers) { try { watcher.Process(watchedEvent); } catch (Exception t) { LOG.Error("Error while calling watcher ", t); } } }
public void QueueEvent(WatchedEvent @event) { if (@event.Type == EventType.None && sessionState == @event.State) { return; } sessionState = @event.State; // materialize the watchers based on the event var pair = new ClientConnection.WatcherSetEventPair(conn.watcher.Materialize(@event.State, @event.Type, @event.Path), @event); // queue the pair (watch set & event) for later processing AppendToQueue(pair); }
private static void ProcessWatcher(IEnumerable<IWatcher> watchers,WatchedEvent watchedEvent) { foreach (IWatcher watcher in watchers) { try { if (null != watcher) { watcher.Process(watchedEvent); } } catch (Exception t) { LOG.Error("Error while calling watcher ", t); } } }
private static void ProcessWatcher(IEnumerable <IWatcher> watchers, WatchedEvent watchedEvent) { foreach (IWatcher watcher in watchers) { try { if (null != watcher) { watcher.Process(watchedEvent); } } catch (Exception t) { //modified by Yang Li //LOG.Error("Error while calling watcher ", t); LOG.Error(string.Format("Error while calling watcher.{0} The error message is: {0}{1}", Environment.NewLine, t.ToString()), t); } } }
private static void ProcessWatcher(IEnumerable <IWatcher> watchers, WatchedEvent watchedEvent) { foreach (IWatcher watcher in watchers) { try { if (null != watcher) { watcher.Process(watchedEvent); } } catch (Exception t) { #if !NET_CORE LOG.Error("Error while calling watcher ", t); #endif } } }
public void QueueEvent(WatchedEvent @event) { if (@event.Type == EventType.None && sessionState == @event.State) { return; } if (waitingEvents.IsAddingCompleted) { throw new InvalidOperationException("consumer has been disposed"); } sessionState = @event.State; // materialize the watchers based on the event var pair = new ClientConnection.WatcherSetEventPair(conn.watcher.Materialize(@event.State, @event.Type, @event.Path), @event); // queue the pair (watch set & event) for later processing waitingEvents.Add(pair); }
private void ReadResponse() { using (MemoryStream ms = new MemoryStream(incomingBuffer)) using (var reader = new EndianBinaryReader(EndianBitConverter.Big, ms, Encoding.UTF8)) { BinaryInputArchive bbia = BinaryInputArchive.GetArchive(reader); ReplyHeader replyHdr = new ReplyHeader(); replyHdr.Deserialize(bbia, "header"); if (replyHdr.Xid == -2) { // -2 is the xid for pings if (PingLog.IsDebugEnabled) { PingLog.Debug(string.Format("Got ping response for sessionid: 0x{0:X} after {1}ms", conn.SessionId, (DateTime.Now.Nanos() - lastPingSentNs) / 1000000)); } return; } if (replyHdr.Xid == -4) { // -4 is the xid for AuthPacket // TODO: process AuthPacket here if (LOG.IsDebugEnabled) { LOG.Debug(string.Format("Got auth sessionid:0x{0:X}", conn.SessionId)); } return; } if (replyHdr.Xid == -1) { // -1 means notification if (LOG.IsDebugEnabled) { LOG.Debug(string.Format("Got notification sessionid:0x{0}", conn.SessionId)); } WatcherEvent @event = new WatcherEvent(); @event.Deserialize(bbia, "response"); // convert from a server path to a client path if (conn.ChrootPath != null) { string serverPath = @event.Path; if (serverPath.CompareTo(conn.ChrootPath) == 0) @event.Path = "/"; else @event.Path = serverPath.Substring(conn.ChrootPath.Length); } WatchedEvent we = new WatchedEvent(@event); if (LOG.IsDebugEnabled) { LOG.Debug(string.Format("Got {0} for sessionid 0x{1:X}", we, conn.SessionId)); } conn.consumer.QueueEvent(we); return; } if (pendingQueue.IsEmpty()) { throw new IOException(string.Format("Nothing in the queue, but got {0}", replyHdr.Xid)); } Packet packet; lock (pendingQueueLock) { packet = pendingQueue.First.Value; pendingQueue.RemoveFirst(); } /* * Since requests are processed in order, we better get a response * to the first request! */ try { if (packet.header.Xid != replyHdr.Xid) { packet.replyHeader.Err = (int)KeeperException.Code.CONNECTIONLOSS; throw new IOException(string.Format("Xid out of order. Got {0} expected {1}", replyHdr.Xid, packet.header.Xid)); } packet.replyHeader.Xid = replyHdr.Xid; packet.replyHeader.Err = replyHdr.Err; packet.replyHeader.Zxid = replyHdr.Zxid; if (replyHdr.Zxid > 0) { lastZxid = replyHdr.Zxid; } if (packet.response != null && replyHdr.Err == 0) { packet.response.Deserialize(bbia, "response"); } if (LOG.IsDebugEnabled) { LOG.Debug(string.Format("Reading reply sessionid:0x{0:X}, packet:: {1}", conn.SessionId, packet)); } } finally { FinishPacket(packet); } } }
//updates remote region servers IP table. private void UpdateRegionServers(WatchedEvent @event) { //obtains a list of remote region server address. just likes: "gs-server-1003,60020,1433640602093" IEnumerable<string> children = _zkClient.GetChildren("/hbase/rs", new ZooKeeperWatcher(UpdateRegionServers)); List<IPEndPoint> regionServers = new List<IPEndPoint>(); foreach (string rs in children) { string[] args = rs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); regionServers.Add(new IPEndPoint(IPAddress.Parse(_hostMappingManager.GetIPAddressByHostName(args[0])), _tPort)); } Interlocked.Exchange(ref _regionServers, regionServers); }
private void ReadResponse(byte[] content) { using (var reader = new EndianBinaryReader(EndianBitConverter.Big, new MemoryStream(content), Encoding.UTF8)) { BinaryInputArchive bbia = BinaryInputArchive.GetArchive(reader); ReplyHeader replyHdr = new ReplyHeader(); replyHdr.Deserialize(bbia, "header"); if (replyHdr.Xid == -2) { // -2 is the xid for pings if (LOG.IsDebugEnabled) { LOG.DebugFormat("Got ping response for sessionid: 0x{0:X} after {1}ms", conn.SessionId, (DateTime.UtcNow.Nanos() - lastPingSentNs) / 1000000); } return; } if (replyHdr.Xid == -4) { // -2 is the xid for AuthPacket // TODO: process AuthPacket here if (LOG.IsDebugEnabled) { LOG.DebugFormat("Got auth sessionid:0x{0:X}", conn.SessionId); } return; } if (replyHdr.Xid == -1) { // -1 means notification if (LOG.IsDebugEnabled) { LOG.DebugFormat("Got notification sessionid:0x{0}", conn.SessionId); } WatcherEvent @event = new WatcherEvent(); @event.Deserialize(bbia, "response"); // convert from a server path to a client path if (conn.ChrootPath != null) { string serverPath = @event.Path; if (serverPath.CompareTo(conn.ChrootPath) == 0) { @event.Path = PathUtils.PathSeparator; } else { @event.Path = serverPath.Substring(conn.ChrootPath.Length); } } WatchedEvent we = new WatchedEvent(@event); if (LOG.IsDebugEnabled) { LOG.DebugFormat("Got {0} for sessionid 0x{1:X}", we, conn.SessionId); } conn.consumer.QueueEvent(we); return; } Packet packet; /* * Since requests are processed in order, we better get a response * to the first request! */ if (pendingQueue.TryDequeue(out packet)) { try { if (packet.header.Xid != replyHdr.Xid) { packet.replyHeader.Err = (int)KeeperException.Code.CONNECTIONLOSS; throw new IOException(new StringBuilder("Xid out of order. Got ").Append(replyHdr.Xid).Append(" expected ").Append(packet.header.Xid).ToString()); } packet.replyHeader.Xid = replyHdr.Xid; packet.replyHeader.Err = replyHdr.Err; packet.replyHeader.Zxid = replyHdr.Zxid; if (replyHdr.Zxid > 0) { lastZxid = replyHdr.Zxid; } if (packet.response != null && replyHdr.Err == 0) { packet.response.Deserialize(bbia, "response"); } if (LOG.IsDebugEnabled) { LOG.DebugFormat("Reading reply sessionid:0x{0:X}, packet:: {1}", conn.SessionId, packet); } } finally { FinishPacket(packet); } } else { throw new IOException(new StringBuilder("Nothing in the queue, but got ").Append(replyHdr.Xid).ToString()); } } }
/// <summary> /// Processes ZooKeeper event /// </summary> /// <param name="e"> /// The event data. /// </param> /// <remarks> /// Requires installed watcher /// </remarks> public void Process(WatchedEvent e) { this.EnsuresNotDisposed(); Logger.Debug("Received event: " + e); this.zooKeeperEventWorker = Thread.CurrentThread; if (this.shutdownTriggered) { Logger.Debug("ignoring event '{" + e.Type + " | " + e.Path + "}' since shutdown triggered"); return; } bool stateChanged = e.Path == null; bool znodeChanged = e.Path != null; bool dataChanged = e.Type == EventType.NodeDataChanged || e.Type == EventType.NodeDeleted || e.Type == EventType.NodeCreated || e.Type == EventType.NodeChildrenChanged; lock (this.somethingChanged) { try { if (stateChanged) { this.ProcessStateChange(e); } if (dataChanged) { this.ProcessDataOrChildChange(e); } } finally { if (stateChanged) { lock (this.stateChangedLock) { Monitor.PulseAll(this.stateChangedLock); } if (e.State == KeeperState.Expired) { lock (this.znodeChangedLock) { Monitor.PulseAll(this.znodeChangedLock); } foreach (string path in this.childChangedHandlers.Keys) { this.Enqueue(new ZooKeeperChildChangedEventArgs(path)); } foreach (string path in this.dataChangedHandlers.Keys) { this.Enqueue(new ZooKeeperDataChangedEventArgs(path)); } } } if (znodeChanged) { lock (this.znodeChangedLock) { Monitor.PulseAll(this.znodeChangedLock); } } } Monitor.PulseAll(this.somethingChanged); } }
public void Process(WatchedEvent watchedEvent) { if (watchedEvent.State != KeeperState.SyncConnected || watchedEvent.Path != Path) return; ProcessImpl(watchedEvent); }
private void ReadResponse(byte[] content) { using (var reader = new EndianBinaryReader(EndianBitConverter.Big, new MemoryStream(content), Encoding.UTF8)) { BinaryInputArchive bbia = BinaryInputArchive.GetArchive(reader); ReplyHeader replyHdr = new ReplyHeader(); replyHdr.Deserialize(bbia, "header"); if (replyHdr.Xid == -2) { // -2 is the xid for pings if (LOG.IsDebugEnabled) LOG.DebugFormat("Got ping response for sessionid: 0x{0:X} after {1}ms", conn.SessionId, (DateTime.UtcNow.Nanos() - lastPingSentNs) / 1000000); return; } if (replyHdr.Xid == -4) { // -2 is the xid for AuthPacket // TODO: process AuthPacket here if (LOG.IsDebugEnabled) LOG.DebugFormat("Got auth sessionid:0x{0:X}", conn.SessionId); return; } if (replyHdr.Xid == -1) { // -1 means notification if (LOG.IsDebugEnabled) LOG.DebugFormat("Got notification sessionid:0x{0}", conn.SessionId); WatcherEvent @event = new WatcherEvent(); @event.Deserialize(bbia, "response"); // convert from a server path to a client path if (conn.ChrootPath != null) { string serverPath = @event.Path; if (serverPath.CompareTo(conn.ChrootPath) == 0) @event.Path = PathUtils.PathSeparator; else @event.Path = serverPath.Substring(conn.ChrootPath.Length); } WatchedEvent we = new WatchedEvent(@event); if (LOG.IsDebugEnabled) LOG.DebugFormat("Got {0} for sessionid 0x{1:X}", we, conn.SessionId); conn.consumer.QueueEvent(we); return; } Packet packet; /* * Since requests are processed in order, we better get a response * to the first request! */ if (pendingQueue.TryDequeue(out packet)) { try { if (packet.header.Xid != replyHdr.Xid) { packet.replyHeader.Err = (int)KeeperException.Code.CONNECTIONLOSS; throw new IOException(new StringBuilder("Xid out of order. Got ").Append(replyHdr.Xid).Append(" expected ").Append(packet.header.Xid).ToString()); } packet.replyHeader.Xid = replyHdr.Xid; packet.replyHeader.Err = replyHdr.Err; packet.replyHeader.Zxid = replyHdr.Zxid; if (replyHdr.Zxid > 0) lastZxid = replyHdr.Zxid; if (packet.response != null && replyHdr.Err == 0) packet.response.Deserialize(bbia, "response"); if (LOG.IsDebugEnabled) LOG.DebugFormat("Reading reply sessionid:0x{0:X}, packet:: {1}", conn.SessionId, packet); } finally { FinishPacket(packet); } } else { throw new IOException(new StringBuilder("Nothing in the queue, but got ").Append(replyHdr.Xid).ToString()); } } }
public WatcherSetEventPair(IEnumerable <IWatcher> watchers, WatchedEvent @event) { this.Watchers = watchers; this.WatchedEvent = @event; }
protected override void ProcessImpl(WatchedEvent watchedEvent) { var path = Path; switch (watchedEvent.Type) { case EventType.NodeDataChanged: var data = _zooKeeper.GetData(path, new NodeMonitorWatcher(_zooKeeper, path, _action), new Stat()); _action(data); break; case EventType.NodeDeleted: _action(null); break; } }
public void Process(WatchedEvent @event) { Console.WriteLine(@event); }
public void QueueEvent(WatchedEvent @event) { if (@event.Type == EventType.None && sessionState == @event.State) return; sessionState = @event.State; // materialize the watchers based on the event var pair = new ClientConnection.WatcherSetEventPair(conn.watcher.Materialize(@event.State, @event.Type,@event.Path), @event); // queue the pair (watch set & event) for later processing AppendToQueue(pair); }
/// <summary> /// 通知 /// </summary> /// <param name="watchedEvent">被观察的事件</param> public void Process(WatchedEvent watchedEvent) { _callback(watchedEvent); }
public void Process(WatchedEvent @event) { if (@event.Path == GloabalConfig.CommandsRoot && @event.Type == EventType.NodeChildrenChanged) { var childres = Zk.GetChildren(GloabalConfig.CommandsRoot, this).ToList(); // 做成异步 var jobs = childres.Where(j => j.StartsWith(Name)).ToList().Select(job => GloabalConfig.CommandsRoot + "/" + job).ToList(); List<CommandInfo> jobInfos = new List<CommandInfo>(); foreach (var job in jobs) { string json = Encoding.UTF8.GetString(Zk.GetData(job, false, null)); CommandInfo jobInfo = JsonConvert.DeserializeObject<CommandInfo>(json); jobInfos.Add(jobInfo); } AppRuntime.Run(jobInfos); foreach (var job in jobs) { Zk.Delete(job, -1); } } if (@event.Path == GloabalConfig.Scheduler && @event.Type == EventType.NodeDataChanged) { List<JobInfo> jobInfos = JsonConvert.DeserializeObject<List<JobInfo>>(Encoding.UTF8.GetString(Zk.GetData(GloabalConfig.Scheduler, false, null))); _scheduler.UpdateScheduler(jobInfos); } }
/// <summary> /// Processess ZooKeeper state changes events /// </summary> /// <param name="e"> /// The event data. /// </param> private void ProcessStateChange(WatchedEvent e) { Logger.Info("zookeeper state changed (" + e.State + ")"); lock (this.stateChangedLock) { this.currentState = e.State; } if (this.shutdownTriggered) { return; } this.Enqueue(new ZooKeeperStateChangedEventArgs(e.State)); if (e.State == KeeperState.Expired) { this.Reconnect(this.connection.Servers, this.connection.SessionTimeout); this.Enqueue(ZooKeeperSessionCreatedEventArgs.Empty); } }
//release control until we have made sure the remote ZooKeeper's state. private void WaitForZooKeeperInitialization(WatchedEvent @event) { if (@event.State == KeeperState.SyncConnected) _zooKeeperInitLock.Set(); }
public void Process(WatchedEvent @event) { }
protected override void ProcessImpl(WatchedEvent watchedEvent) { var path = Path; var watcher = new ChildrenMonitorWatcher(_zooKeeper, path, _action); switch (watchedEvent.Type) { case EventType.NodeCreated: case EventType.NodeChildrenChanged: if (_zooKeeper.Exists(path, watcher) != null) { var childrens = _zooKeeper.GetChildren(path, watcher, new Stat()); _action(childrens); } else { _action(null); } break; case EventType.NodeDeleted: _zooKeeper.Exists(path, watcher); _action(null); break; } }
public void Process(WatchedEvent @event) { _event = @event; _connected.Set(); }
public void Process(WatchedEvent @event) { if (@event.State == KeeperState.SyncConnected) { _connectioned(); } else { _disconnect(); } }
public void Process(WatchedEvent e) { Logger.DebugFormat("Process called by handler. Received event, e.EventType:{0} e.State: {1} e.Path :{2} ", e.Type, e.State, e.Path); this.zooKeeperEventWorker = Thread.CurrentThread; if (this.shutdownTriggered) { Logger.DebugFormat("Shutdown triggered. Ignoring event. Type: {0}, Path: {1}, State: {2} ", e.Type, (e.Path ?? "null"), e.State); return; } try { this.EnsuresNotDisposed(); bool stateChanged = string.IsNullOrEmpty(e.Path); bool znodeChanged = !string.IsNullOrEmpty(e.Path); bool dataChanged = e.Type == EventType.NodeDataChanged || e.Type == EventType.NodeDeleted || e.Type == EventType.NodeCreated || e.Type == EventType.NodeChildrenChanged; Logger.DebugFormat("Process called by handler. stateChanged:{0} znodeChanged:{1} dataChanged:{2} ", stateChanged, znodeChanged, dataChanged); lock (this.somethingChanged) { try { if (stateChanged) { this.ProcessStateChange(e); } if (dataChanged) { this.ProcessDataOrChildChange(e); } } finally { if (stateChanged) { lock (this.stateChangedLock) { Monitor.PulseAll(this.stateChangedLock); } if (e.State == KeeperState.Expired) { lock (this.znodeChangedLock) { Monitor.PulseAll(this.znodeChangedLock); } foreach (string path in this.childChangedHandlers.Keys) { this.Enqueue(new ZooKeeperChildChangedEventArgs(path)); } foreach (string path in this.dataChangedHandlers.Keys) { this.Enqueue(new ZooKeeperDataChangedEventArgs(path)); } } } if (znodeChanged) { lock (this.znodeChangedLock) { Monitor.PulseAll(this.znodeChangedLock); } } } Monitor.PulseAll(this.somethingChanged); } } catch (Exception ex) { Logger.Error("Error occurred while processing event: " + ex.FormatException()); } }
protected abstract void ProcessImpl(WatchedEvent watchedEvent);
/// <summary> /// Processess ZooKeeper childs or data changes events /// </summary> /// <param name="e"> /// The event data. /// </param> private void ProcessDataOrChildChange(WatchedEvent e) { if (this.shutdownTriggered) { return; } if (e.Type == EventType.NodeChildrenChanged || e.Type == EventType.NodeCreated || e.Type == EventType.NodeDeleted) { this.Enqueue(new ZooKeeperChildChangedEventArgs(e.Path)); } if (e.Type == EventType.NodeDataChanged || e.Type == EventType.NodeCreated || e.Type == EventType.NodeDeleted) { this.Enqueue(new ZooKeeperDataChangedEventArgs(e.Path)); } }
public WatcherSetEventPair(HashSet <IWatcher> watchers, WatchedEvent @event) { this.watchers = watchers; this.@event = @event; }
/// <summary> /// Processess ZooKeeper state changes events /// </summary> /// <param name="e"> /// The event data. /// </param> private void ProcessStateChange(WatchedEvent e) { Logger.Info("ProcessStateChange==zookeeper state changed (" + e.State + ")"); lock (this.stateChangedLock) { Logger.InfoFormat("Current state:{0} in the lib:{1}", this.currentState, this.connection.GetInternalZKClient().State); this.currentState = e.State; } if (this.shutdownTriggered) { return; } this.Enqueue(new ZooKeeperStateChangedEventArgs(e.State)); if (e.State == KeeperState.Expired) { while (true) { try { this.Reconnect(this.connection.Servers, this.connection.SessionTimeout); this.Enqueue(ZooKeeperSessionCreatedEventArgs.Empty); break; } catch (Exception ex) { Logger.Error("Exception occurred while trying to reconnect to ZooKeeper", ex); Thread.Sleep(1000); continue; } } } }
public void Process(WatchedEvent @event) { if (@event.Type == EventType.NodeDeleted) { Log.Info("watching child {0} is deleted", @event.Path); AttempToBecomeLeader(); } }
public void Process(WatchedEvent @event) { Log.DebugFormat("Watcher fired on path: {0} state: {1} type {2}", @event.Path, @event.State, @event.Type); reset.Set(); }
public WatcherSetEventPair(IEnumerable<IWatcher> watchers, WatchedEvent @event) { this.Watchers = watchers; this.WatchedEvent = @event; }
public void Process(WatchedEvent @event) { if (@event.Type == EventType.NodeDataChanged) { Log.Info("Process path:{0}", @event.Path); var ret3 = Handle.GetData(@event.Path, this, null); DataChangedHandler(@event.Path, ret3); } }