7:59 AM
@SylvainBoisse Yeah. It was a mistake on my end that I didn't made naming thing clear. Thanks for pointing that out, I will fix that. I also saw your edit and now I understood what needs to be done to avoid business thread and pinger thread to work on same socket.
I have one question on your edit though, I still see you are using
synchronized
on a socket. Do we still need that? Since now we are using polling
from priority queue for all the business thread and pinger thread also will be working on different socket so do we need synchronized
now?
Also just to make things clear, I don't do anything special to revive dead socket in my code, I just need to keep pinging them until I get acknowledgment back. When I say dead socket, then it generally means, the messaging queue machine on which socket was open is down or messaging queue app is dead.
Also I came up with different approach of solving the same problem which uses same concept of
BlockingQueue
as you suggested and it also considers some of your points as well I think. I am creating a SocketPool
for each datacenter where I ping only dead sockets to check whether they are live or not.
And then I am exposing methods where I can poll socket for business thread to use, release sockets back to queue, also report dead socket by taking a look at the acknowledgment (as you mentioned in this question).
So this new approach will expose all these methods where I can properly poll socket, release socket and report back dead socket from business thread. But somehow I feel like, I didn't got everything right from design perspective or maybe there might be some thread safety issues as well.
I think you will like this solution better so wanted to see if you can take a look at that as well and see what we can do to improve and make it better. In this solution I only ping dead sockets
(reported by business thread)
from pinger thread to check whether they are alive now or not? If they are alive, then only I will add them back to liveQueue.
After I get this thing simplified, only thing we need to do is calling those api's properly from
SendToQueue
class for polling, releasing and reporting back dead socket which is easy. Here is my question:
1
I have a list of sockets per datacenter (in general, I have around three datacenter and each datacenter having 20 sockets, so in total I have around 60 sockets). Now I have 10 business threads which want to send data on a socket. I need to make sure each thread works on a different socket so I cr...
3 hours later…
10:35 AM
Yeah i'm still using synchronized although no two thread should have a handle to the same socket. It's superfluous indeed. But if it really is, then it is also cheap (lock obtained almost instantaneously, no competition, no retries etc.) so I would keep it as a last barrier (in case the algo has a bug or for future evolutions)
understood for revival. Now that you mention that sockets are long-lived, with a complex state (dead/alive, but also idle/sending/waiting for ack) I am thinking m alternative idea of having smarter sockets that handle and manage their own state is even better than a centralized entity managing from above.
9 hours later…
7:39 PM
@SylvainBoisse Yes sockets are long-lived. They will only be down when the machine on which messaging queue is running is down or messaging queue app is dead. Once either of these two things are fixed, they will be back online and start giving acknowledgment back. What I do is, I keep sending message asynchronously and if acknowledgment is not received then I retry again to send and keep retrying until I get the acknowledgment back.
8:13 PM
Idea is very simple, I need to make sure, I don't share same socket between two threads at all - either two business thread or one business thread and other pinger timer thread for live socket check.
And if any socket is dead, then I don't want business thread to use that socket to send data on it. And as soon as dead socket is back online, add them back to liveQueue so that business thread can start using them. So either I can keep pinging all available sockets to check whether they are live or not and basis on that I can add them back to liveQueue or just ping dead sockets (reported by business thread) to see whether they have become live or not.
« first day (3 days earlier) ← previous day next day → last day (26 days later) »