Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TkkiD-0002CD-WB for pgsql-jdbc@arkaria.postgresql.org; Tue, 18 Dec 2012 00:07:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TkkiD-0002jb-9z for pgsql-jdbc@arkaria.postgresql.org; Tue, 18 Dec 2012 00:07:09 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TkkiC-0002jV-5F for pgsql-jdbc@postgresql.org; Tue, 18 Dec 2012 00:07:08 +0000 Received: from [222.73.24.84] (helo=song.cn.fujitsu.com) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TkkiA-0007We-Ck for pgsql-jdbc@postgresql.org; Tue, 18 Dec 2012 00:07:07 +0000 X-IronPort-AV: E=Sophos;i="4.84,305,1355068800"; d="scan'208";a="6418640" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 18 Dec 2012 08:05:09 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id qBI07Mxn028581; Tue, 18 Dec 2012 08:07:22 +0800 Received: from [127.0.0.1] ([10.167.159.99]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2012121808060397-468703 ; Tue, 18 Dec 2012 08:06:03 +0800 Message-ID: <50CFB362.4080701@cn.fujitsu.com> Date: Tue, 18 Dec 2012 08:05:54 +0800 From: Chen Huajun User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.28) Gecko/20120306 Lightning/1.0b2 Thunderbird/3.1.20 MIME-Version: 1.0 To: Scott Harrington CC: pgsql-jdbc@postgresql.org Subject: Re: performance problem of Failover Datasource? 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> In-Reply-To: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/12/18 08:06:03, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/12/18 08:06:04 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 222.73.24.84 (failed) X-Pg-Spam-Score: -2.6 (--) 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 Thanks for you advise. I will try to made a new patch and add load balance supporting. --=20 Best Regards=EF=BC=8C 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 mu= lti-threads. But i worry about locking operation is a litter frequent by Co= llections.synchronizedSet and may affect >> performance. I think using keword "synchronized" explicitly instead of C= ollections.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 o= nly slave. For example: Basically pick up the >> server randomly.If one server had dead remove it from the candidates, an= d retry the next server. And after a while(can be configured) re-add the de= ad 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 curren= tly gets passed around. > > Something like this, which doesn't require synchronization but rather sto= res a single volatile index of the "last known good" server address (warnin= g: 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 =3D hostSpec;= } > public Iterator iterator() { return Collections.singletonList(h= ostSpec).iterator(); } > public void reportSuccessfulConnection(HostSpec ignored) {} > } > > public class LastKnownGoodHostChooser implements HostChooser > { > private final HostSpec[] hostSpecs; > private volatile int lastKnownGood =3D -1; > public LastKnownGoodHostChooser(HostSpec[] hostSpecs) { this.hostSpecs = =3D hostSpecs.clone(); } > > public Iterator iterator() { > int first =3D lastKnownGood; > if (first <=3D 0) { > return Arrays.asList(hostSpecs).iterator(); > } > ArrayList reordered =3D new ArrayList(hostSpecs.lengt= h); > for (int ii =3D first; ii < hostSpecs.length; ++ii) { > reordered.add(hostSpecs[ii]); > } > for (int ii =3D 0; ii < first; ++ii) { > reordered.add(hostSpecs[ii]); > } > return reordered.iterator(); > } > > public void reportSuccessfulConnection(HostSpec hostSpec) { > lastKnownGood =3D Arrays.asList(hostSpecs).indexOf(hostSpec); > } > } > > > > > --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc