I'm using c# driver for Cassandra with multi-threads processing.
At first, I tried to create a connection and execute command and then close the connection after done the work. But it's seem not to work for me, sometimes it's got an exception that No hosts available.
So, I changed to working with static connection. it's seem to work as well.
But when thread is working too fast, it's broken again. I've to put some Thread.Sleep for 1 second then it works.
And with this static solution, I tried to use Asynchronous process, BeginExecute and it's not work for me as well, exception No Hosts Available.
So, anyone has better ideas or better implementation on multi-threads processing working with Cassandra c# driver, it would be appreciate if you can share.
Thank you in advance.
Cheers,
Kin
CassandraSession can only have one connection at a time. It probably isn't thread safe now that I think about it. But the Connection Pool is thread safe, so if you use that you will always have a high availability connection
If you use asynchronous method,it looks like that:
Statement sta=new SimpleStatement("Select * from XXX where XXX;");
session.ExecutAsync(sta);
Make sure your setup is satisfying the connection requirements
The C# driver should definitely work in a multi-threading environment (1 cluster object, 1 session object/keyspace)
It's difficult to say why you are seeing the NoHostAvailable exception without seeing any code.
Related
I am writing a UWP app using the Windows.Devices.WiFi to basically get a lists of networks. Everything was working fine when I retrieved the information a time or two. However, I wanted to put the code into a timer so I can report regularly. Once I did this, I got "an attempt was made to establish a session to a network server, but there are already too many sessions established to that server."
I am not sure what is establishing connections as I am just trying to read the information. I am not even calling the ConnectAsync calls.
Can anyone help me out? I need to know what to dispose, or close, etc.
Update: Further analysis, I am finding that the call to FindAllAdaptersAsync multiple times is causing this issue.
I decided to cache up the list of adapters by only calling FindAllAdaptersAsync once. Thanks for the idea Henk. This seemed to fix my issue for now. However, I think that it is a bug with FindAllAdaptersAsync. I would think you should be able to call this as much as you like, unless maintaining the network connection is necessary every time. Or at least a way to free them up.
I've been experimenting with Stateless (HSM in C#) (https://code.google.com/p/stateless/) lately and I've come across something that I'm not really sure how to achieve.
Let's say I have the following states:
Start.
Connect
Read
Finish
What I'm trying to achieve is: when the TCP connection (in the Connect state) is established, advance to the Read state. Or, if it fails, advance to the Finish state (where it may return to the Connect state and attempt a new connection after a timeout period).
How can I achieve this auto advancing feature using Stateless, since firing triggers from within the states can cause a stack overflow exception?
Cheers
Given that I found no native solution on Stateless to do what I asked, I ended up wrapping the .Fire(trigger) in a task
Task.Start(() => _stateMachine.Fire(trigger));
Doing so, means that the state machine does not advance itself per say, but it's rather advanced by an external source, solving the SO exception.
My program starts with windows startup,
But a background worker is supposed to work instantly after the program is opened.
But it starts with a delay and then even returns false signs(it returns if a site is up),
Only after about 15 seconds the background-worker continues to work normally and the program too. I think this is because of .net framework trying to load, or internet connection that is not up yet, or something that didn't load yet(windows startup).
What can solve this, and what is the probable cause? (WinForm C#)
Edit:
Here is something I thought of,
I don't think though that this is a good practice. Is there a better way?
(Load method):
while (!netConnection())
{
}
if(netConnection())
bwCheck.RunWorkerAsync();
I think this is because of .net framework trying to load
Nope. If that were the case your program wouldn't run.
or internet connection that is not up yet, or
Yup. The network card/interface/connection/whatever is not initialized and connected to the internet yet. You can't expect a PC to be connected to the internet immediately at startup. Even more, what if your customer is on a domain using network authentication? What if they delay network communications until some task is complete (this was actually the problem in my case below. Seriously.)
It may take even longer to get it up and running in that case (read: don't add a Thread.Sleep() in a vain attempt to 'fix' the issue.
I had to fix a problem like this once in a systems design where we communicated to a motion control board via the ethernet bus in a PC. I ended up adding some code to monitor the status of the network connection and, only when it was established, started talking to the device via the network card.
EDIT: As SLaks pointed out in the comments, this is pretty simple in C#: The NetworkAvailabilityChanged event for your programming pleasure.
It is absolutely because of everything still starting up. Services can still be coming online long after you log in, the quick login dialog you see was an optimization in windows to let you log in while everything else still starts up.
Take note of
How to detect working internet connection in C#?
specifically a technique that avoids the loopback adapter:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
UPDATE: The following error was actually due to a simple bug which I missed. The only real message here that tired and stupid is a bad combination.
For reasons to do with some specific features of an ODBC driver we're forced to use, I've been trying to write a small application which directly uses ODBC calls. Since C# 2.0 is what I know most, I've been doing this using P/Invoke calls into ODBC32.dll.
The code I've written initially has been multithreaded. But I've noticed that as soon as I jump threads I'm getting AccessViolationExceptions. For instance, when I generate IntPtr references to an Environment and Connection in one thread and then try to use these in another thread in the generation of a Statement (SQLAllocStmt), it all goes pop.
I'm sure I can work around this, but is there some obvious reason for this? Is the unmanaged memory allocated by the calls into ODBC32.dll somehow bound to a particular thread?
This depends on:
The odbc driver: What is it?
Your code: Are you freeing the memory without realizing it?
Consider:
Whether you really need to do this. Can't you control the behaviour of the driver using the connection string, or using driver-specific commands through a command object?
If you do have to do this, can you isolate it into a single STA thread and use marshalling, or a task queue, to simplify your job?
If you do have to use it from multiple threads, can't you make sure each thread has it's own Connection and Environment?
Are there any templates/patterns/guides I can follow for designing a multithreaded server? I can't find anything terribly useful online through my google searches.
My program will start a thread to listen for connections using TcpListener.
Every client connection will be handled by it's own IClientHandler thread. The server will wrap the clientHandler.HandleClient in a delegate, call BeginInvoke, and then quit caring about it.
I also need to be able to cleanly shutdown the listening thread, which is something I'm not finding a lot of exampes of online.
I'm assuming some mix of lock/AutoResetEvents/threading magic combined with the async BeginAceptTcpClient and EndAcceptTcpClient will get me there, but when it comes to networking code, to me it's all been done. So I have to believe there's just some pattern out there I can follow and not get totally confused by the myriad multithreaded corner cases I can never seem to get perfect.
Thanks.
Oddly enough you may find something on a Computer Science Assignment, CSC 512 Programming Assignment 4: Multi-Threaded Server With Patterns. Altough it's C++ voodoo but the theory is quite understandable for someone who can do C#.
Acceptor/ Connector
Monitor Object
Thread Safe Interface
Wrapper Facade
Scoped Locking
Strategized Locking
Reactor
Half Sync/Half-Async
Leaders/Followers
Altough you can get the whole list of nice readings on the main page.
Take a look at this previous question:
How do you minimize the number of threads used in a tcp server application?
It's not strictly C# specific, but it has some good advice.