Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjVMC-0001gS-7T for pgsql-jdbc@arkaria.postgresql.org; Fri, 14 Dec 2012 13:31:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TjVMB-000298-HG for pgsql-jdbc@arkaria.postgresql.org; Fri, 14 Dec 2012 13:31:15 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjVMA-000292-JT for pgsql-jdbc@postgresql.org; Fri, 14 Dec 2012 13:31:14 +0000 Received: from [222.73.24.84] (helo=song.cn.fujitsu.com) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjVM4-0005jY-G3 for pgsql-jdbc@postgresql.org; Fri, 14 Dec 2012 13:31:12 +0000 X-IronPort-AV: E=Sophos;i="4.84,281,1355068800"; d="scan'208";a="6400278" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 14 Dec 2012 21:29:07 +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 qBEDV176018024; Fri, 14 Dec 2012 21:31:01 +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 2012121421300569-429197 ; Fri, 14 Dec 2012 21:30:05 +0800 Message-ID: <50CB29BD.6090903@cn.fujitsu.com> Date: Fri, 14 Dec 2012 21:29:33 +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: Dave Cramer CC: kaprikorn07 , List 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> In-Reply-To: <50CA959E.4060603@cn.fujitsu.com> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/12/14 21:30:05, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/12/14 21:30:06 Content-Type: multipart/mixed; boundary="------------090900060401050202090604" 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 --------------090900060401050202090604 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=UTF-8; format=flowed Hi I had make the patch,Please check it. and I had run the testcases,the result is same as before modified. -- Best Regards, Chen Huajun --------------090900060401050202090604 Content-Type: text/plain; name="pgjdbc_optimization_for_mutiServerUrl.patch" Content-Disposition: attachment; filename="pgjdbc_optimization_for_mutiServerUrl.patch" Content-Transfer-Encoding: quoted-printable diff --git a/org/postgresql/core/ConnectionFactory.java b/org/postgresql/co= re/ConnectionFactory.java index a351619..e2ba588 100644 --- a/org/postgresql/core/ConnectionFactory.java +++ b/org/postgresql/core/ConnectionFactory.java @@ -8,7 +8,10 @@ */ package org.postgresql.core; =20 +import java.util.Collections; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; import java.sql.SQLException; =20 import org.postgresql.util.HostSpec; @@ -32,6 +35,11 @@ { "3", new org.postgresql.core.v3.ConnectionFactoryImpl() = }, { "2", new org.postgresql.core.v2.ConnectionFactoryImpl() = }, }; +=20=20=20=20 + /** + * List of alive servers. + */ + private static final Set aliveServers =3D Collections.synchron= izedSet(new HashSet()); =20 /** * Establishes and initializes a new connection. @@ -54,6 +62,12 @@ public static ProtocolConnection openConnection(HostSpec[] hostSpecs, = String user, String database, Properties info, Logger logger) throws SQLExc= eption { String protoName =3D info.getProperty("protocolVersion"); =20 + // Adjust the order of hostSpecs to try the most suitable server f= irst. + if(hostSpecs.length > 1) + { + hostSpecs =3D adjustHostSpecsOrder(hostSpecs); + } +=20=20=20=20=20=20=20=20=09 for (int i =3D 0; i < versions.length; ++i) { String versionProtoName =3D (String) versions[i][0]; @@ -62,10 +76,30 @@ =20 ConnectionFactory factory =3D (ConnectionFactory) versions[i][= 1]; ProtocolConnection connection =3D factory.openConnectionImpl(h= ostSpecs, user, database, info, logger); - if (connection !=3D null) - return connection; + if (connection !=3D null) { + if (hostSpecs.length > 1) { + String hostSpecKey =3D connection.getHostSpec().toString(); + + // Remove servers which can not be connected from the list of alive = servers + for (int j =3D 0; j < hostSpecs.length; j++) { + if (hostSpecKey.equals(hostSpecs[j].toString())) + break; + + aliveServers.remove(hostSpecs[j].toString()); + } + // Add the current server to the list of alive servers + aliveServers.add(hostSpecKey); + } + return connection; + } } =20 + if (hostSpecs.length > 1) { + // Remove servers which can not be connected from the list of alive s= ervers + for (int j =3D 0; j < hostSpecs.length; j++) { + aliveServers.remove(hostSpecs[j].toString()); + } + } throw new PSQLException(GT.tr("A connection could not be made usin= g the requested protocol {0}.", protoName), PSQLState.CONNECTION_UNABLE_TO_CONNECT); } @@ -86,4 +120,26 @@ * than protocol version incompatibility. */ public abstract ProtocolConnection openConnectionImpl(HostSpec[] hostS= pecs, String user, String database, Properties info, Logger logger) throws = SQLException; +=20=20=20=20 +=20=20=20=20 + /** + * Adjust the order of servers in hostSpecs, internally move first fou= nd alive server to head. + */ + private static HostSpec[] adjustHostSpecsOrder(HostSpec[] hostSpecs) { + if (aliveServers.isEmpty() + || aliveServers.contains(hostSpecs[0].toString())) + return hostSpecs; + + HostSpec[] result =3D hostSpecs.clone(); + + for (int i =3D 1; i < hostSpecs.length; i++) { + if (aliveServers.contains(hostSpecs[i].toString())) { + result[0] =3D hostSpecs[i]; + result[i] =3D hostSpecs[0]; + break; + } + } + + return result; + } }= --------------090900060401050202090604 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc --------------090900060401050202090604--