Re: performance problem of Failover Datasource? - Mailing list pgsql-jdbc

From Scott Harrington
Subject Re: performance problem of Failover Datasource?
Date
Msg-id alpine.WNT.2.00.1212151055200.6544@turks
Whole thread Raw
In response to Re: performance problem of Failover Datasource?  (Chen Huajun <chenhj@cn.fujitsu.com>)
Responses Re: performance problem of Failover Datasource?
Re: performance problem of Failover Datasource?
List pgsql-jdbc
On Sat, 15 Dec 2012, Chen Huajun wrote:

> In this patch,I use Collections.synchronizedSet to synchronize within
> multi-threads. But i worry about locking operation is a litter frequent
> by Collections.synchronizedSet and may affect performance. I think using
> keword "synchronized" explicitly instead of Collections.synchronizedSet
> may reduce times of locking.Is there any better suggestion?
>
> In addition, I have a idea. By adjusting the order of hosts we also can
> implement a simple load balance while all of the hosts are master or
> read only slave. For example: Basically pick up the server randomly.If
> one server had dead remove it from the candidates, and retry the next
> server. And after a while(can be configured) re-add the dead host to the
> candidates because the dead server may had been repaired.
>
> What about that?

Perhaps instead you could abstract this logic into a
org.postgresql.util.HostChooser interface, which would replace the
HostSpec[] array that currently gets passed around.

Something like this, which doesn't require synchronization but rather
stores a single volatile index of the "last known good" server address
(warning: coding off the top of my head).

public interface HostChooser implements Iterable<HostSpec> {
   Iterator<HostSpec> iterator();
   void reportSuccessfulConnection(HOstSpec hostSpec);
}

public class SingleHostChooser implements HostChooser
{
   private final HostSpec hostSpec;
   public SingleHostChooser(HostSpec hostSpec) { this.hostSpec = hostSpec; }
   public Iterator<HostSpec> iterator() { return Collections.singletonList(hostSpec).iterator(); }
   public void reportSuccessfulConnection(HostSpec ignored) {}
}

public class LastKnownGoodHostChooser implements HostChooser
{
   private final HostSpec[] hostSpecs;
   private volatile int lastKnownGood = -1;
   public LastKnownGoodHostChooser(HostSpec[] hostSpecs) { this.hostSpecs = hostSpecs.clone(); }

   public Iterator<HostSpec> iterator() {
     int first = lastKnownGood;
     if (first <= 0) {
       return Arrays.asList(hostSpecs).iterator();
     }
     ArrayList<HostSpec> reordered = new ArrayList<HostSpec>(hostSpecs.length);
     for (int ii = first; ii < hostSpecs.length; ++ii) {
       reordered.add(hostSpecs[ii]);
     }
     for (int ii = 0; ii < first; ++ii) {
       reordered.add(hostSpecs[ii]);
     }
     return reordered.iterator();
   }

   public void reportSuccessfulConnection(HostSpec hostSpec) {
     lastKnownGood = Arrays.asList(hostSpecs).indexOf(hostSpec);
   }
}





pgsql-jdbc by date:

Previous
From: Chen Huajun
Date:
Subject: Re: performance problem of Failover Datasource?
Next
From: David Wall
Date:
Subject: JDBC with PG 9.2 and large object permissions as they relate to blobs (after upgrade from 8.4)