Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a62My-0001Mf-52 for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Dec 2015 20:26:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a62Mx-0003BP-Mu for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Dec 2015 20:26:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a62Mw-0003BG-4F for pgsql-hackers@postgresql.org; Mon, 07 Dec 2015 20:26:46 +0000 Received: from mail-qg0-x22c.google.com ([2607:f8b0:400d:c04::22c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1a62Mo-0000eI-83 for pgsql-hackers@postgresql.org; Mon, 07 Dec 2015 20:26:45 +0000 Received: by qgeb1 with SMTP id b1so155208655qge.1 for ; Mon, 07 Dec 2015 12:26:35 -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=Xe/M/yRSYDnDcEuKPFPwlSKA+G8+atWqNo/IxTK39JM=; b=XCrRMB1Er4pfDRm0iFgNHFCBiZ4WHvZPKvH0+N1WA0VswAbvixrKikCDDgjXuwQ3aK Fduxj/W30N8A1ohNm25OR0VsanT3SvbCvCiwTQ/f3SExV48xpD5c5ZY3VZG/oVvF9kEq 1iWZMikGoQ7+a0gWeKPgPSPZ2z8TXOnWcxay6YkNxCM2qecsNT7qxGrzqi086aebhl5i zXzdIDh9p6V24PKwjW+GuJvKJLV7nFRoChyg6CRhXuI6Cx1gMNij8PipTx1PS2T4E+ct Hja2d9/GVnrkNqGjLs/f9Na0m8yBrbmHIGEE5pSHtRAZY/KsjcTF7DecSUEPGru8SETz 59Kw== 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=Xe/M/yRSYDnDcEuKPFPwlSKA+G8+atWqNo/IxTK39JM=; b=jDRlx7j6DjD32vwrRddl6HeAvK10QhTXuDKsL4xtZkmyLxFoWAEswMb4DoQPBDJJd0 Et5G9cg3TNdxaDbAC+d4mh/hXnn3MEMJOunXZiHA0PKNZVzM9uDh323N4+dbmLLe1kZZ lUMp9Rlz5Xlxsf/cF/UWi0cM87W33Lgu81HxK71e07z+BU/iO6jADNLaiM7vOaxzcytG /bLNqwiercI2+nxyqGdkDGwkB+VCzybXwC6LKzKOBS/FY11gMdl09TUrT4zriZzpH6Zc MBo06VhEjR0qolP7zx4rCHUsxMRbPmHs8ln0xFjWoUa+Rx0y34Wqjt9yPZjt1UQCDAqr 5zZQ== X-Gm-Message-State: ALoCoQkreH8ainVyvojSAgqbrc0pEc5J5qxLfYpaQcmt5XKkFtz6FsafOqyxLmaAW44I3v27EXfB X-Received: by 10.140.134.135 with SMTP id 129mr42344023qhg.58.1449519995783; Mon, 07 Dec 2015 12:26:35 -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 64sm12315259qhh.19.2015.12.07.12.26.33 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 07 Dec 2015 12:26:34 -0800 (PST) Message-ID: <5665EB79.9070805@enterprisedb.com> Date: Mon, 07 Dec 2015 15:26:33 -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> In-Reply-To: <20151109124404.1af6ba0c@fafnir> 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. And one comment about the code itself - in connectDBStart(), you've added quite a bit of code to parse multiple hosts/ports. I would recommend adding a comment that shows the expected format, and then choosing better variable names (better than 'p', 'q', and 'r'); perhaps the variable names could refer to components of the connection string that you are parsing (like 'host', 'port', 'delimiter', ...). That would make the code much easier to read/maintain. Thanks. -- Korry -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers