Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjuSI-0000d3-6m for pgsql-jdbc@arkaria.postgresql.org; Sat, 15 Dec 2012 16:19:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TjuSG-0000jX-M3 for pgsql-jdbc@arkaria.postgresql.org; Sat, 15 Dec 2012 16:19:12 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjuSG-0000jS-1L for pgsql-jdbc@postgresql.org; Sat, 15 Dec 2012 16:19:12 +0000 Received: from nyc15.sns-usa.com ([173.231.131.23]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjuSD-000178-BP for pgsql-jdbc@postgresql.org; Sat, 15 Dec 2012 16:19:10 +0000 Received: from turks.localdomain (rrcs-24-199-145-40.midsouth.biz.rr.com [24.199.145.40]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by nyc15.sns-usa.com (Postfix) with ESMTP id 107D637004B for ; Sat, 15 Dec 2012 11:19:08 -0500 (EST) Date: Sat, 15 Dec 2012 11:19:07 -0500 (Eastern Standard Time) From: Scott Harrington To: pgsql-jdbc@postgresql.org Subject: Re: performance problem of Failover Datasource? In-Reply-To: <50CC2279.3090301@cn.fujitsu.com> Message-ID: References: <4DFA5A86.9030301@acm.org> <4DFA671F.2050005@acm.org> <4DFA9704.4000209@hogranch.com> <4DFAB403.5010309@acm.org> <4DFAB5B8.5010603@hogranch.com> <4DFAB9E2.4070905@acm.org> <1334056170219-5629626.post@n5.nabble.com> <1355303340886-5736270.post@n5.nabble.com> <50C9B102.2060208@cn.fujitsu.com> <50CA959E.4060603@cn.fujitsu.com> <50CB29BD.6090903@cn.fujitsu.com> <50CC2279.3090301@cn.fujitsu.com> User-Agent: Alpine 2.00 (WNT 1167 2008-08-23) X-X-Sender: scotth@mx7.forexster.com MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org 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 { Iterator iterator(); void reportSuccessfulConnection(HOstSpec hostSpec); } public class SingleHostChooser implements HostChooser { private final HostSpec hostSpec; public SingleHostChooser(HostSpec hostSpec) { this.hostSpec = hostSpec; } public Iterator 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 iterator() { int first = lastKnownGood; if (first <= 0) { return Arrays.asList(hostSpecs).iterator(); } ArrayList reordered = new ArrayList(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); } } -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc