improve: Reduce lock contention in RemoteQueryManager
This commit is contained in:
1 parent
b7f600b968
commit
7798dfd825
1 file changed
+48
-17
@@ -1,6 +1,5 @@
|
||||
using bts;
|
||||
|
||||
using Bts.DataStream;
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.DataStreamExpressions;
|
||||
using Bts.PubSub;
|
||||
@@ -58,8 +57,7 @@ namespace BCG {
|
||||
readonly object syncRoot = new();
|
||||
IDisposable? cloudStreamsSub;
|
||||
IDisposable? remoteRequestsSub;
|
||||
bool isDisposed;
|
||||
|
||||
State state;
|
||||
|
||||
public RemoteQueryManager(IManagedSubscriber managedSubscriber, IPubTableBroker pubTableBroker, ITableStreamFactory tableStreamFactory, IEnumerable<IDataStreamFactory> streamFactories) {
|
||||
_sub = managedSubscriber;
|
||||
@@ -71,30 +69,63 @@ namespace BCG {
|
||||
: new CompositeDataStreamFactory(streamFactoryList);
|
||||
}
|
||||
|
||||
enum State {
|
||||
Idle = 0,
|
||||
Running = 1,
|
||||
Disposed = 2,
|
||||
WaitingToDispose = 3,
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) {
|
||||
lock (syncRoot) {
|
||||
Log.assert(!isDisposed);
|
||||
Log.isnull(cloudStreamsSub);
|
||||
Log.isnull(remoteRequestsSub);
|
||||
var cloudStream = tableStreamFactory.Create("cloud_streams")
|
||||
?? throw new Exception("Failed to create TableStream 'cloud_stream'");
|
||||
cloudStreamsSub = cloudStream.AsObservable().Subscribe(_on_str);
|
||||
remoteRequestsSub = _sub.Observe("RemoteRequest").Subscribe(data => {
|
||||
RemoteRequest req = RemoteRequest.construct_from_bytes(data);
|
||||
_spawn_remote_stream(req.published_table_name, req.stream_expression, false);
|
||||
});
|
||||
switch (state) {
|
||||
case State.Idle:
|
||||
break;
|
||||
case State.WaitingToDispose:
|
||||
case State.Disposed:
|
||||
throw new ObjectDisposedException(nameof(RemoteQueryManager));
|
||||
default:
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
state = State.Running;
|
||||
}
|
||||
if (cloudStreamsSub is not null) {
|
||||
throw new InvalidOperationException("cloudStreamsSub already created");
|
||||
}
|
||||
if (remoteRequestsSub is not null) {
|
||||
throw new InvalidOperationException("remoteRequestsSub already created");
|
||||
}
|
||||
var cloudStream = tableStreamFactory.Create("cloud_streams")
|
||||
?? throw new Exception("Failed to create TableStream 'cloud_stream'");
|
||||
cloudStreamsSub = cloudStream.AsObservable().Subscribe(_on_str);
|
||||
remoteRequestsSub = _sub.Observe("RemoteRequest").Subscribe(data => {
|
||||
RemoteRequest req = RemoteRequest.construct_from_bytes(data);
|
||||
_spawn_remote_stream(req.published_table_name, req.stream_expression, false);
|
||||
});
|
||||
lock (syncRoot) {
|
||||
if (state == State.WaitingToDispose) {
|
||||
state = State.Disposed;
|
||||
}
|
||||
}
|
||||
if (state == State.Disposed) {
|
||||
Disposable.dispose(ref cloudStreamsSub);
|
||||
Disposable.dispose(ref remoteRequestsSub);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) {
|
||||
lock (syncRoot) {
|
||||
if (!isDisposed) {
|
||||
isDisposed = true;
|
||||
Disposable.dispose(ref cloudStreamsSub);
|
||||
Disposable.dispose(ref remoteRequestsSub);
|
||||
switch (state) {
|
||||
case State.Running:
|
||||
break;
|
||||
default:
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
state = State.Disposed;
|
||||
}
|
||||
Disposable.dispose(ref cloudStreamsSub);
|
||||
Disposable.dispose(ref remoteRequestsSub);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user