From: Victor Wagner <vitus@wagner.pp.ru>
To: pgsql-hackers@postgresql.org
Subject: Re: Patch: Implement failover on libpq connect level.
Date: Fri, 9 Sep 2016 11:49:53 +0300
Message-ID: <20160909114953.10e702e4@fafnir.local.vm> (raw)
In-Reply-To: <CAD__Oui5DGk1oJnhPNCd_+4WNKgJTb2ni44k3urxxO+s5esw2A@mail.gmail.com>
References: <56E0AD90.5090409@pgmasters.net>
<CANP8+jKPORaK2Xhuwc6KJ6qwfL8KiVZjxDw16sS0XoDe0Kh7qw@mail.gmail.com>
<56E9E978.5090103@pgmasters.net>
<CAD__OugniD9nz6weXCvnEELKX2iTK3xCmVWttVjLH0k5rn_NOA@mail.gmail.com>
<CAD__Ouh53ttKfZ-439R9OGDOx=1xbBHS34SKOgTLqu73=gcp7Q@mail.gmail.com>
<20160831111330.53a018ed@fafnir.local.vm>
<CAD__OuimXSFUNCXXfcXXGEceuH8ik+Hr9qSYU0BidZw4gSxdig@mail.gmail.com>
<CAD__Oui-3m6W83HyQNQLczrGRDQr4R67jv76DGwf1tT764zQ5A@mail.gmail.com>
<CAD__OuhfOL-TQz-BBrFMj_tu8DqTc+ccxbwLkj+tUHAzvH14Pw@mail.gmail.com>
<20160905072215.4efd7b8a@wagner.wagner.home>
<20160905110311.GA11351@e733>
<20160907165634.5f8e9675@fafnir.local.vm>
<CAD__Oui5DGk1oJnhPNCd_+4WNKgJTb2ni44k3urxxO+s5esw2A@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
On Fri, 9 Sep 2016 11:20:59 +0530
Mithun Cy <mithun.cy@enterprisedb.com> wrote:
> If concern is only about excluding address which was tried
> previously. Then I think we can try this way, randomly permute given
> address list (for example fisher-yates shuffle) before trying any of
> those address in it. After that you can treat the new list exactly
> like we do for sequential connections. I think this makes code less
> complex.
Random permutation is much more computationally complex than random
picking.
Alexander suggests to use other random pick algorithm than I use.
I use algorithm which works with lists of unknown length and chooses
line of such list with equal probability. This algorithm requires one
random number for each element of the list, but we are using C library
rand function which uses quite cheap linear congruental pseudo random
numbers, so random number generation is infinitely cheap than network
connection attempt.
As this algorithm doesn't need beforehand knowledge of the list length,
it is easy to wrap this algorithm into loop, which modifies the list,
moving already tried elements out of scope of next picking.
(it is really quite alike fisher-yates algorithm).
Alexander suggests to store somewhere number of elements of the list,
than generate one random number and pick element with this number.
Unfortunately, it doesn't save much effort in the linked list.
At average we'll need to scan half of the list to find desired element.
So, it is O(N) anyway.
Now, the main point is that we have two different timeframes of
operation.
1. Attempt to connect. It is short period, and we can assume that state
of servers doesn't change. So if, server is down, we should ignore it
until the end of this attempt to connect.
2. Attempt to failover. If no good candidate was found, we might need
to retry connection until one of standbys would be promoted to master
(with targetServerType=master) or some host comes up. See documentation
of failover_timeout configuration parameter.
It means that we cannot just throw away those hosts from list which
didn't respond during this connect attempt.
So, using random permutation instead of random pick wouln't help.
And keeping somewhere number of elements in the list wouldn't help
either unless we change linked list to completely different data
structure which allows random access.
--
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: vitus@wagner.pp.ru, pgsql-hackers@postgresql.org
Subject: Re: Patch: Implement failover on libpq connect level.
In-Reply-To: <20160909114953.10e702e4@fafnir.local.vm>
References: <56E0AD90.5090409@pgmasters.net> <CANP8+jKPORaK2Xhuwc6KJ6qwfL8KiVZjxDw16sS0XoDe0Kh7qw@mail.gmail.com> <56E9E978.5090103@pgmasters.net> <CAD__OugniD9nz6weXCvnEELKX2iTK3xCmVWttVjLH0k5rn_NOA@mail.gmail.com> <CAD__Ouh53ttKfZ-439R9OGDOx=1xbBHS34SKOgTLqu73=gcp7Q@mail.gmail.com> <20160831111330.53a018ed@fafnir.local.vm> <CAD__OuimXSFUNCXXfcXXGEceuH8ik+Hr9qSYU0BidZw4gSxdig@mail.gmail.com> <CAD__Oui-3m6W83HyQNQLczrGRDQr4R67jv76DGwf1tT764zQ5A@mail.gmail.com> <CAD__OuhfOL-TQz-BBrFMj_tu8DqTc+ccxbwLkj+tUHAzvH14Pw@mail.gmail.com> <20160905072215.4efd7b8a@wagner.wagner.home> <20160905110311.GA11351@e733> <20160907165634.5f8e9675@fafnir.local.vm> <CAD__Oui5DGk1oJnhPNCd_+4WNKgJTb2ni44k3urxxO+s5esw2A@mail.gmail.com> <20160909114953.10e702e4@fafnir.local.vm>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox