Re: performance problem of Failover Datasource? - Mailing list pgsql-jdbc
From | Chen Huajun |
---|---|
Subject | Re: performance problem of Failover Datasource? |
Date | |
Msg-id | 50CFB362.4080701@cn.fujitsu.com Whole thread Raw |
In response to | Re: performance problem of Failover Datasource? (Scott Harrington <scotth01@sns-usa.com>) |
Responses |
Re: performance problem of Failover Datasource?
|
List | pgsql-jdbc |
Thanks for you advise. I will try to made a new patch and add load balance supporting. -- Best Regards, Chen Huajun (2012/12/16 0:19), Scott Harrington wrote: > 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 operationis a litter frequent by Collections.synchronizedSet and may affect >> performance. I think using keword "synchronized" explicitly instead of Collections.synchronizedSet may reduce times oflocking.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 thehosts 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(canbe 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 theHostSpec[] array that currently gets passed around. > > Something like this, which doesn't require synchronization but rather stores a single volatile index of the "last knowngood" 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: