1

I have a PriorityBlockingQueue in which I am adding SocketHolder as shown below. And let's say current datacenter where this code is running is abc. We have three datacenters abc, def and pqr.

private static final PriorityBlockingQueue<SocketHolder> liveQueue = new PriorityBlockingQueue<>();

for (SocketHolder socket : listOfSockets) {
    liveQueue.add(socket);
}

Now what I need to do is:

  • I have to setup some sort of ordering on liveQueue so that all SocketHolder whose datacenter is abc (which is current datacenter where the code is running), they should be at the top so which means when we retrieve it from this liveQueue all SocketHolder with abc datacenter should come out first and then later on all other SocketHolder with other dc's. Similarly for cases where current datacenter is either pqr or def.

Below is my SocketHolder class that implements Comparable interface but I am confuse what I need to do in my compareTo method so that at the top of liveQueue (after adding elements to it) we have all SocketHolder whose datacenter are current datacenter and then followed by other datacenter in which I have no priority.

public final class SocketHolder implements Comparable<SocketHolder> {
  private final Socket socket;
  private final Datacenter dc;
  private final Context context;

  public SocketHolder(Socket socket, Datacenter dc, Context context) {
    super();
    this.socket = socket;
    this.dc = dc;
    this.context = context;
  }

  @Override
  public int compareTo(SocketHolder o) {
    // String currentDC = Utils.CURRENT_DATACENTER.get().name();
    return 0;
  }
}
user1950349
  • 4,738
  • 19
  • 67
  • 119
  • I would just use a different queue for the current data center and process it first. PQ is overkill for this, as there appear to be essentially only two priorities: 'this' and 'other'. – user207421 Mar 10 '18 at 00:28
  • A PQ has the advantage that if a 'late' task is submitted, it will immediately bubble to the top. – Ian Mc Mar 10 '18 at 00:53
  • @IanMc Only if you processes the queues unintelligently, or there are more than two priority levels. You should take from the first queue first and if empty take *one* item from the second queue: rinse and repeat. A PQ will also lose order of insertion if there are only two priorities. – user207421 Mar 10 '18 at 02:10

1 Answers1

1

The live datacenter will be assigned priority 1, and the other datacenters priority 2. The queue will process the lowest priority first.

public final class SocketHolder implements Comparable<SocketHolder> {
   private final Socket socket;
   private final Datacenter dc;
   private final Context context;
   private final int priority;

   public SocketHolder(Socket socket, Datacenter dc, Context context) {
      super();
      this.socket = socket;
      this.dc = dc;
      this.context = context;
      this.priority = this.dc.getName().equals(Utils.CURRENT_DATACENTER.get().name()) ? 1 : 2;
   }

   @Override
   public int compareTo(SocketHolder o) {
       return Integer.compare(this.priority, o.priority);
   }
}
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
  • Instead of giving each dc a number why can't we do this basis on current datacenter that we already know of and datacenter for which `SocketHolder` we are adding? – user1950349 Mar 09 '18 at 23:54
  • I mean just check current datacenter (code is already there in my compareTo method but it is commented) with datacenter of SocketHolder we are adding and basis on that do the ordering? Will this not work? – user1950349 Mar 09 '18 at 23:56
  • Yes - How does the SocketHolder object know which is the current datacenter? – Ian Mc Mar 09 '18 at 23:57
  • By just making this call `String currentDC = Utils.CURRENT_DATACENTER.get().name();` This will tell what's the current datacenter where the code is running so we can use that and compare it and that's what I mentioned I believe in my question. – user1950349 Mar 10 '18 at 00:00
  • I don't know the method to get the DC name ... assuming it is getName() – Ian Mc Mar 10 '18 at 00:04