Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a636o-00038B-Vx for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Dec 2015 21:14:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a636o-0000hG-EC for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Dec 2015 21:14:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a635U-0007ef-Hh for pgsql-hackers@postgresql.org; Mon, 07 Dec 2015 21:12:48 +0000 Received: from mail-qg0-x22e.google.com ([2607:f8b0:400d:c04::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1a635R-0000r8-Ju for pgsql-hackers@postgresql.org; Mon, 07 Dec 2015 21:12:47 +0000 Received: by qgcc31 with SMTP id c31so154421886qgc.3 for ; Mon, 07 Dec 2015 13:12:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=+jqRgcd4KxNgD0O/pC1HUJYvfWSRyEjnyiWXUY38EHE=; b=jF2aBWcA9Cz8T8gNOB8Yy/ZtrPni3BBQY8RnapncX6Fiejd9ErJn+3OJdrGjpHkuJm Z/elVbkl4KtdoZQBESVkKFarAtpBkN89aQk4UckZGis3x8rYrNXXBE6J8jN7oysuTrrP 5WFA0BLNWlrCSamJRXndaibQDO0gRiiHuui1wz0vDkK0IDemV87FamjYI5xBTskgIRVV 65ujsQawxV1CTodHLB9Sr+30MsjW8xw3U+lId93pK8mpQXLUhmDMY8d8OCS1AJgE5zjQ IXhfUa5IN3IocricjvWaAvhxF4WglPQqAlZEX6xx0554DZ9npb2loN3glJUb+ODNY60i Amvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=+jqRgcd4KxNgD0O/pC1HUJYvfWSRyEjnyiWXUY38EHE=; b=UcyzHeP+nCZ6JfRMbpIe63B/EexuV/T0HrZUu+PDN1iymFaVw0byXFvMQZGyTDCryb czi6+hVxPUjOTqJ6j8/0ChRZFrDeca2gLuhcIYHZP8TJqla9bFaTzFCAr3VZCQcuF+/j eByJL+cqtEt9/7OtV5/Gu7k3XyGTkGtrCuHmdwVEEpK847rWdaA0N91aC7CNDVv2uPnf cLI9sl0579poKwR5WdqMbRRn+qmrEjS13St2uUWQ07B10ugdGrbs4Flmlu7fAn6WkrrM FY6RjCh+zTk4aNTvENJkm56AOoMfQRlOFTaBCoj4nYTu+oXMV2YTzgsxf/AaS+A9CWVw R8ZA== X-Gm-Message-State: ALoCoQmC2sPulTGBcUnfe56ReWw2TBM464dMiBSg4TVJ/7VUiPmeEiRpyLZy/d+4d+JUDXhqU0Ty X-Received: by 10.140.196.69 with SMTP id r66mr41758095qha.40.1449522764430; Mon, 07 Dec 2015 13:12:44 -0800 (PST) Received: from MegaMac.local (va-76-4-65-131.sta.embarqhsd.net. [76.4.65.131]) by smtp.gmail.com with ESMTPSA id v202sm12507522qka.38.2015.12.07.13.12.41 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 07 Dec 2015 13:12:42 -0800 (PST) Message-ID: <5665F649.1040206@enterprisedb.com> Date: Mon, 07 Dec 2015 16:12:41 -0500 From: Korry Douglas User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: Victor Wagner CC: pgsql-hackers@postgresql.org Subject: Re: Patch: Implement failover on libpq connect level. References: <20150818041850.GA5092@wagner.pp.ru> <20151014104151.GA6744@wagner.pp.ru> <562E8C55.9030409@gmx.net> <20151028111839.6e73c68a@fafnir> <56327381.5040108@gmx.net> <56379A44.9080609@gmx.net> <20151105151235.GA21434@wagner.pp.ru> <20151106153807.GA6104@alvherre.pgsql> <20151109124404.1af6ba0c@fafnir> <5665EB79.9070805@enterprisedb.com> In-Reply-To: <5665EB79.9070805@enterprisedb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org > >> I've tried to deal with some of these problems. >> >> My patch have support for following things: >> >> 1. Check whether database instance is in the recovery/standby mode and >> try to find another one if so. >> 2. Let cluster management software to have some time to promote one of >> the standbys to master. I.e. there can be failover timeout specified to >> allow retry after some time if no working master found. >> >> Really there is room for some improvements in handling of connect >> timeout (which became much more important thing when ability to try >> next host appears). Now it is handled only by blocking-mode connect >> functions, not by async state machine. But I decided to publish patch >> without these improvements to get feedback from community. > A bit of testing on this turns up a problem. > > Consider a connection string that specifies two hosts and a read/write > connection: > > postgresql://korry@127.0.0.1:5301,127.0.0.1:5300/edb?readonly=0 > > If the first host is a healthy standby (meaning that I can connect to > it but pg_is_in_recovery() returns 't'), the state machine will never > move on to the second host. > > The problem seems to be in PQconnectPoll() in the case for > CONNECTION_AUTH_OK, specifically this code: > > /* We can release the address list now. */ > pg_freeaddrinfo_all(conn->addrlist_family, conn->addrlist); > conn->addrlist = NULL; > conn->addr_cur = NULL; > > That frees up the list of alternative host addresses. The state > machine then progresses to CONNECTION_CHECK_RO (which invokes > pg_is_in_recovery()), then CONNECTION_CHECK_RW (waiting for the > response from the server). Since we just connected to a standby > replica, pg_is_in_recovery() returns 't' and the state changes to > CONNECTION_NEEDED. The next call to try_next_address() will fail to > find a next address because we freed the list in the case for > CONNECTION_AUTH_OK. > > A related issue: when the above sequence occurs, no error message is > returned (because the case for CONNECTION_NEEDED thinks "an > appropriate error message is already set up"). > > In short, if you successfully connect to standby replica (and specify > readonly=0), the remaining hosts are ignored, even though one of those > hosts is a master. A follow-up - the conn->addrlist is also freed when the case for CONNECTION_CHECK_RW decides that conn->status != CONNECTION_OK and calls closePGConn(). -- Korry -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers