forked from jilonglv/ZooKeeperNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientConnectionEventConsumer.cs
156 lines (141 loc) · 5.34 KB
/
ClientConnectionEventConsumer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace ZooKeeperNet
{
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using NLog;
public class ClientConnectionEventConsumer : IStartable, IDisposable
{
private static readonly Logger LOG = LogManager.GetCurrentClassLogger();
private readonly ClientConnection conn;
private readonly Thread eventThread;
//ConcurrentQueue gives us the non-blocking way of processing, it reduced the contention so much
internal readonly BlockingCollection<ClientConnection.WatcherSetEventPair> waitingEvents = new BlockingCollection<ClientConnection.WatcherSetEventPair>();
/** This is really the queued session state until the event
* thread actually processes the event and hands it to the watcher.
* But for all intents and purposes this is the state.
*/
private volatile KeeperState sessionState = KeeperState.Disconnected;
public ClientConnectionEventConsumer(ClientConnection conn)
{
this.conn = conn;
eventThread = new Thread(new SafeThreadStart(PollEvents).Run) { Name = new StringBuilder("ZK-EventThread ").Append(conn.zooKeeper.Id).ToString(), IsBackground = true };
}
public void Start()
{
eventThread.Start();
}
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 {0}", t);
}
}
}
public void PollEvents()
{
try
{
while (!waitingEvents.IsCompleted)
{
try
{
ClientConnection.WatcherSetEventPair pair = null;
if (waitingEvents.TryTake(out pair, -1))
{
ProcessWatcher(pair.Watchers, pair.WatchedEvent);
}
}
catch (ObjectDisposedException)
{
}
catch (InvalidOperationException)
{
}
catch (OperationCanceledException)
{
//ignored
}
catch (Exception t)
{
LOG.Error("Caught unexpected throwable {0}", t);
}
}
}
catch (ThreadInterruptedException e)
{
LOG.Error("Event thread exiting due to interruption {0}", e);
}
if (LOG.IsTraceEnabled)
LOG.Info("EventThread shut down");
}
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 int isDisposed = 0;
private void InternalDispose()
{
if (Interlocked.CompareExchange(ref isDisposed, 1, 0) == 0)
{
try
{
waitingEvents.CompleteAdding();
if (eventThread.IsAlive)
{
eventThread.Join();
}
}
catch (Exception ex)
{
LOG.Warn("Error disposing {0} : {1}", this.GetType().FullName, ex.Message);
}
}
}
public void Dispose()
{
InternalDispose();
GC.SuppressFinalize(this);
}
~ClientConnectionEventConsumer()
{
InternalDispose();
}
}
}