2015-10-05 01:26:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <time.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <cstdlib>
|
2017-04-17 16:16:04 +00:00
|
|
|
#include <climits>
|
2017-02-07 15:38:57 +00:00
|
|
|
#include <random>
|
2017-06-12 23:34:01 +00:00
|
|
|
#include <functional>
|
2020-03-19 10:38:34 +00:00
|
|
|
#include <common/types.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/scope_guard.h>
|
2017-04-17 16:16:04 +00:00
|
|
|
#include <Core/Types.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/PoolBase.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/NetException.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/randomSeed.h>
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2015-10-05 01:26:43 +00:00
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int ALL_CONNECTION_TRIES_FAILED;
|
2017-04-17 16:16:04 +00:00
|
|
|
extern const int ALL_REPLICAS_ARE_STALE;
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event DistributedConnectionFailTry;
|
|
|
|
extern const Event DistributedConnectionFailAtAll;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
/// This class provides a pool with fault tolerance. It is used for pooling of connections to replicated DB.
|
|
|
|
/// Initialized by several PoolBase objects.
|
|
|
|
/// When a connection is requested, tries to create or choose an alive connection from one of the nested pools.
|
|
|
|
/// Pools are tried in the order consistent with lexicographical order of (error count, priority, random number) tuples.
|
|
|
|
/// Number of tries for a single pool is limited by max_tries parameter.
|
|
|
|
/// The client can set nested pool priority by passing a GetPriority functor.
|
|
|
|
///
|
2017-04-20 14:04:38 +00:00
|
|
|
/// NOTE: if one of the nested pools blocks because it is empty, this pool will also block.
|
2017-04-17 16:16:04 +00:00
|
|
|
///
|
|
|
|
/// The client must provide a TryGetEntryFunc functor, which should perform a single try to get a connection from a nested pool.
|
|
|
|
/// This functor can also check if the connection satisfies some eligibility criterion (e.g. check if
|
|
|
|
/// the replica is up-to-date).
|
2015-10-05 01:26:43 +00:00
|
|
|
|
|
|
|
template <typename TNestedPool>
|
|
|
|
class PoolWithFailoverBase : private boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using NestedPool = TNestedPool;
|
|
|
|
using NestedPoolPtr = std::shared_ptr<NestedPool>;
|
|
|
|
using Entry = typename NestedPool::Entry;
|
|
|
|
using NestedPools = std::vector<NestedPoolPtr>;
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
PoolWithFailoverBase(
|
|
|
|
NestedPools nested_pools_,
|
|
|
|
time_t decrease_error_period_,
|
2019-08-06 04:36:38 +00:00
|
|
|
size_t max_error_cap_,
|
2020-05-30 21:57:37 +00:00
|
|
|
Poco::Logger * log_)
|
2017-04-17 16:16:04 +00:00
|
|
|
: nested_pools(std::move(nested_pools_))
|
|
|
|
, decrease_error_period(decrease_error_period_)
|
2019-08-06 04:36:38 +00:00
|
|
|
, max_error_cap(max_error_cap_)
|
2017-04-17 16:16:04 +00:00
|
|
|
, shared_pool_states(nested_pools.size())
|
|
|
|
, log(log_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
struct TryResult
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-17 16:16:04 +00:00
|
|
|
TryResult() = default;
|
2017-04-20 16:19:30 +00:00
|
|
|
|
|
|
|
explicit TryResult(Entry entry_)
|
2017-08-18 12:38:43 +00:00
|
|
|
: entry(std::move(entry_))
|
2017-04-20 16:19:30 +00:00
|
|
|
, is_usable(true)
|
|
|
|
, is_up_to_date(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
entry = Entry();
|
|
|
|
is_usable = false;
|
|
|
|
is_up_to_date = false;
|
|
|
|
staleness = 0.0;
|
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Entry entry;
|
2017-04-20 16:19:30 +00:00
|
|
|
bool is_usable = false; /// If false, the entry is unusable for current request
|
|
|
|
/// (but may be usable for other requests, so error counts are not incremented)
|
2017-04-18 13:37:00 +00:00
|
|
|
bool is_up_to_date = false; /// If true, the entry is a connection to up-to-date replica.
|
2017-04-17 16:16:04 +00:00
|
|
|
double staleness = 0.0; /// Helps choosing the "least stale" option when all replicas are stale.
|
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// This functor must be provided by a client. It must perform a single try that takes a connection
|
|
|
|
/// from the provided pool and checks that it is good.
|
2017-04-17 16:16:04 +00:00
|
|
|
using TryGetEntryFunc = std::function<TryResult(NestedPool & pool, std::string & fail_message)>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// The client can provide this functor to affect load balancing - the index of a pool is passed to
|
|
|
|
/// this functor. The pools with lower result value will be tried first.
|
2017-04-17 16:16:04 +00:00
|
|
|
using GetPriorityFunc = std::function<size_t(size_t index)>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
/// Returns a single connection.
|
|
|
|
Entry get(const TryGetEntryFunc & try_get_entry, const GetPriorityFunc & get_priority = GetPriorityFunc());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
/// Returns at least min_entries and at most max_entries connections (at most one connection per nested pool).
|
|
|
|
/// The method will throw if it is unable to get min_entries alive connections or
|
|
|
|
/// if fallback_to_stale_replicas is false and it is unable to get min_entries connections to up-to-date replicas.
|
2017-07-28 19:34:25 +00:00
|
|
|
std::vector<TryResult> getMany(
|
2019-03-01 23:03:41 +00:00
|
|
|
size_t min_entries, size_t max_entries, size_t max_tries,
|
2017-04-17 16:16:04 +00:00
|
|
|
const TryGetEntryFunc & try_get_entry,
|
|
|
|
const GetPriorityFunc & get_priority = GetPriorityFunc(),
|
|
|
|
bool fallback_to_stale_replicas = true);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
protected:
|
|
|
|
struct PoolState;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
using PoolStates = std::vector<PoolState>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
/// This function returns a copy of pool states to avoid race conditions when modifying shared pool states.
|
|
|
|
PoolStates updatePoolStates();
|
2019-08-06 04:36:38 +00:00
|
|
|
PoolStates getPoolStates() const;
|
2015-10-05 01:26:43 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
NestedPools nested_pools;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
const time_t decrease_error_period;
|
2019-08-06 04:36:38 +00:00
|
|
|
const size_t max_error_cap;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-08-06 04:36:38 +00:00
|
|
|
mutable std::mutex pool_states_mutex;
|
2017-04-17 16:16:04 +00:00
|
|
|
PoolStates shared_pool_states;
|
|
|
|
/// The time when error counts were last decreased.
|
|
|
|
time_t last_error_decrease_time = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-05-30 21:57:37 +00:00
|
|
|
Poco::Logger * log;
|
2017-04-17 16:16:04 +00:00
|
|
|
};
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename TNestedPool>
|
2017-04-17 16:16:04 +00:00
|
|
|
typename TNestedPool::Entry
|
|
|
|
PoolWithFailoverBase<TNestedPool>::get(const TryGetEntryFunc & try_get_entry, const GetPriorityFunc & get_priority)
|
|
|
|
{
|
2019-03-01 23:03:41 +00:00
|
|
|
std::vector<TryResult> results = getMany(1, 1, 1, try_get_entry, get_priority);
|
2017-07-28 19:34:25 +00:00
|
|
|
if (results.empty() || results[0].entry.isNull())
|
2017-04-17 16:16:04 +00:00
|
|
|
throw DB::Exception(
|
|
|
|
"PoolWithFailoverBase::getMany() returned less than min_entries entries.",
|
|
|
|
DB::ErrorCodes::LOGICAL_ERROR);
|
2017-07-28 19:34:25 +00:00
|
|
|
return results[0].entry;
|
2017-04-17 16:16:04 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename TNestedPool>
|
2017-07-28 19:34:25 +00:00
|
|
|
std::vector<typename PoolWithFailoverBase<TNestedPool>::TryResult>
|
2017-04-17 16:16:04 +00:00
|
|
|
PoolWithFailoverBase<TNestedPool>::getMany(
|
2019-03-01 23:03:41 +00:00
|
|
|
size_t min_entries, size_t max_entries, size_t max_tries,
|
2017-04-17 16:16:04 +00:00
|
|
|
const TryGetEntryFunc & try_get_entry,
|
|
|
|
const GetPriorityFunc & get_priority,
|
|
|
|
bool fallback_to_stale_replicas)
|
|
|
|
{
|
|
|
|
/// Update random numbers and error counts.
|
|
|
|
PoolStates pool_states = updatePoolStates();
|
|
|
|
if (get_priority)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < pool_states.size(); ++i)
|
|
|
|
pool_states[i].priority = get_priority(i);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
struct ShuffledPool
|
|
|
|
{
|
2019-06-16 18:12:14 +00:00
|
|
|
NestedPool * pool{};
|
|
|
|
const PoolState * state{};
|
|
|
|
size_t index = 0;
|
2017-04-17 16:16:04 +00:00
|
|
|
size_t error_count = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// Sort the pools into order in which they will be tried (based on respective PoolStates).
|
2017-04-17 16:16:04 +00:00
|
|
|
std::vector<ShuffledPool> shuffled_pools;
|
|
|
|
shuffled_pools.reserve(nested_pools.size());
|
|
|
|
for (size_t i = 0; i < nested_pools.size(); ++i)
|
|
|
|
shuffled_pools.push_back(ShuffledPool{nested_pools[i].get(), &pool_states[i], i, 0});
|
|
|
|
std::sort(
|
|
|
|
shuffled_pools.begin(), shuffled_pools.end(),
|
|
|
|
[](const ShuffledPool & lhs, const ShuffledPool & rhs)
|
|
|
|
{
|
|
|
|
return PoolState::compare(*lhs.state, *rhs.state);
|
|
|
|
});
|
|
|
|
|
2017-04-19 18:29:50 +00:00
|
|
|
/// We will try to get a connection from each pool until a connection is produced or max_tries is reached.
|
2017-04-17 16:16:04 +00:00
|
|
|
std::vector<TryResult> try_results(shuffled_pools.size());
|
|
|
|
size_t entries_count = 0;
|
2017-04-20 16:19:30 +00:00
|
|
|
size_t usable_count = 0;
|
2017-04-17 16:16:04 +00:00
|
|
|
size_t up_to_date_count = 0;
|
|
|
|
size_t failed_pools_count = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-01 23:03:41 +00:00
|
|
|
/// At exit update shared error counts with error counts occurred during this call.
|
2017-04-17 16:16:04 +00:00
|
|
|
SCOPE_EXIT(
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(pool_states_mutex);
|
2017-04-17 16:16:04 +00:00
|
|
|
for (const ShuffledPool & pool: shuffled_pools)
|
2019-08-06 04:36:38 +00:00
|
|
|
{
|
|
|
|
auto & pool_state = shared_pool_states[pool.index];
|
2019-09-15 10:35:12 +00:00
|
|
|
pool_state.error_count = std::min<UInt64>(max_error_cap, pool_state.error_count + pool.error_count);
|
2019-08-06 04:36:38 +00:00
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
std::string fail_messages;
|
|
|
|
bool finished = false;
|
|
|
|
while (!finished)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < shuffled_pools.size(); ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-19 18:29:50 +00:00
|
|
|
if (up_to_date_count >= max_entries /// Already enough good entries.
|
|
|
|
|| entries_count + failed_pools_count >= nested_pools.size()) /// No more good entries will be produced.
|
2017-04-17 16:16:04 +00:00
|
|
|
{
|
|
|
|
finished = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
ShuffledPool & shuffled_pool = shuffled_pools[i];
|
|
|
|
TryResult & result = try_results[i];
|
|
|
|
if (shuffled_pool.error_count >= max_tries || !result.entry.isNull())
|
|
|
|
continue;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
std::string fail_message;
|
|
|
|
result = try_get_entry(*shuffled_pool.pool, fail_message);
|
2017-04-20 16:19:30 +00:00
|
|
|
|
|
|
|
if (!fail_message.empty())
|
|
|
|
fail_messages += fail_message + '\n';
|
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
if (!result.entry.isNull())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-17 16:16:04 +00:00
|
|
|
++entries_count;
|
2017-04-20 16:19:30 +00:00
|
|
|
if (result.is_usable)
|
|
|
|
{
|
|
|
|
++usable_count;
|
|
|
|
if (result.is_up_to_date)
|
|
|
|
++up_to_date_count;
|
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_WARNING(log, "Connection failed at try №{}, reason: {}", (shuffled_pool.error_count + 1), fail_message);
|
2017-04-20 16:19:30 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::DistributedConnectionFailTry);
|
|
|
|
|
2019-08-06 04:36:38 +00:00
|
|
|
shuffled_pool.error_count = std::min(max_error_cap, shuffled_pool.error_count + 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
if (shuffled_pool.error_count >= max_tries)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-04-17 16:16:04 +00:00
|
|
|
++failed_pools_count;
|
|
|
|
ProfileEvents::increment(ProfileEvents::DistributedConnectionFailAtAll);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-20 16:19:30 +00:00
|
|
|
if (usable_count < min_entries)
|
2017-04-17 16:16:04 +00:00
|
|
|
throw DB::NetException(
|
|
|
|
"All connection tries failed. Log: \n\n" + fail_messages + "\n",
|
|
|
|
DB::ErrorCodes::ALL_CONNECTION_TRIES_FAILED);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-18 13:37:00 +00:00
|
|
|
try_results.erase(
|
|
|
|
std::remove_if(
|
|
|
|
try_results.begin(), try_results.end(),
|
2017-04-20 16:19:30 +00:00
|
|
|
[](const TryResult & r) { return r.entry.isNull() || !r.is_usable; }),
|
2017-04-18 13:37:00 +00:00
|
|
|
try_results.end());
|
|
|
|
|
2017-07-28 19:34:25 +00:00
|
|
|
/// Sort so that preferred items are near the beginning.
|
|
|
|
std::stable_sort(
|
|
|
|
try_results.begin(), try_results.end(),
|
|
|
|
[](const TryResult & left, const TryResult & right)
|
|
|
|
{
|
|
|
|
return std::forward_as_tuple(!left.is_up_to_date, left.staleness)
|
|
|
|
< std::forward_as_tuple(!right.is_up_to_date, right.staleness);
|
|
|
|
});
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-21 22:41:40 +00:00
|
|
|
if (fallback_to_stale_replicas)
|
2017-04-17 16:16:04 +00:00
|
|
|
{
|
2017-04-19 18:29:50 +00:00
|
|
|
/// There is not enough up-to-date entries but we are allowed to return stale entries.
|
|
|
|
/// Gather all up-to-date ones and least-bad stale ones.
|
2017-04-17 16:16:04 +00:00
|
|
|
|
|
|
|
size_t size = std::min(try_results.size(), max_entries);
|
2017-07-28 19:34:25 +00:00
|
|
|
try_results.resize(size);
|
2017-04-17 16:16:04 +00:00
|
|
|
}
|
2020-04-21 22:41:40 +00:00
|
|
|
else if (up_to_date_count >= min_entries)
|
|
|
|
{
|
|
|
|
/// There is enough up-to-date entries.
|
|
|
|
try_results.resize(up_to_date_count);
|
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
else
|
|
|
|
throw DB::Exception(
|
|
|
|
"Could not find enough connections to up-to-date replicas. Got: " + std::to_string(up_to_date_count)
|
|
|
|
+ ", needed: " + std::to_string(min_entries),
|
|
|
|
DB::ErrorCodes::ALL_REPLICAS_ARE_STALE);
|
2015-10-05 01:26:43 +00:00
|
|
|
|
2017-07-28 19:34:25 +00:00
|
|
|
return try_results;
|
2017-04-17 16:16:04 +00:00
|
|
|
}
|
2015-10-05 01:26:43 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename TNestedPool>
|
2017-04-17 16:16:04 +00:00
|
|
|
struct PoolWithFailoverBase<TNestedPool>::PoolState
|
|
|
|
{
|
|
|
|
UInt64 error_count = 0;
|
|
|
|
Int64 priority = 0;
|
|
|
|
UInt32 random = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
void randomize()
|
|
|
|
{
|
|
|
|
random = rng();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
static bool compare(const PoolState & lhs, const PoolState & rhs)
|
|
|
|
{
|
|
|
|
return std::forward_as_tuple(lhs.error_count, lhs.priority, lhs.random)
|
|
|
|
< std::forward_as_tuple(rhs.error_count, rhs.priority, rhs.random);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
private:
|
|
|
|
std::minstd_rand rng = std::minstd_rand(randomSeed());
|
|
|
|
};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename TNestedPool>
|
2017-04-17 16:16:04 +00:00
|
|
|
typename PoolWithFailoverBase<TNestedPool>::PoolStates
|
|
|
|
PoolWithFailoverBase<TNestedPool>::updatePoolStates()
|
|
|
|
{
|
|
|
|
PoolStates result;
|
|
|
|
result.reserve(nested_pools.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(pool_states_mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
for (auto & state : shared_pool_states)
|
|
|
|
state.randomize();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-08-04 14:00:26 +00:00
|
|
|
time_t current_time = time(nullptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
if (last_error_decrease_time)
|
|
|
|
{
|
|
|
|
time_t delta = current_time - last_error_decrease_time;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
if (delta >= 0)
|
|
|
|
{
|
2020-06-13 13:39:21 +00:00
|
|
|
const UInt64 MAX_BITS = sizeof(UInt64) * CHAR_BIT;
|
|
|
|
size_t shift_amount = MAX_BITS;
|
2017-04-17 16:16:04 +00:00
|
|
|
/// Divide error counts by 2 every decrease_error_period seconds.
|
2020-06-13 13:39:21 +00:00
|
|
|
if (decrease_error_period)
|
|
|
|
shift_amount = delta / decrease_error_period;
|
2017-04-17 16:16:04 +00:00
|
|
|
/// Update time but don't do it more often than once a period.
|
|
|
|
/// Else if the function is called often enough, error count will never decrease.
|
|
|
|
if (shift_amount)
|
|
|
|
last_error_decrease_time = current_time;
|
|
|
|
|
2020-06-13 13:39:21 +00:00
|
|
|
if (shift_amount >= MAX_BITS)
|
2017-04-17 16:16:04 +00:00
|
|
|
{
|
|
|
|
for (auto & state : shared_pool_states)
|
|
|
|
state.error_count = 0;
|
|
|
|
}
|
|
|
|
else if (shift_amount)
|
|
|
|
{
|
|
|
|
for (auto & state : shared_pool_states)
|
|
|
|
state.error_count >>= shift_amount;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
else
|
|
|
|
last_error_decrease_time = current_time;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-17 16:16:04 +00:00
|
|
|
result.assign(shared_pool_states.begin(), shared_pool_states.end());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-04-17 16:16:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-08-06 04:36:38 +00:00
|
|
|
|
|
|
|
template <typename TNestedPool>
|
|
|
|
typename PoolWithFailoverBase<TNestedPool>::PoolStates
|
|
|
|
PoolWithFailoverBase<TNestedPool>::getPoolStates() const
|
|
|
|
{
|
|
|
|
std::lock_guard lock(pool_states_mutex);
|
|
|
|
return shared_pool_states;
|
|
|
|
}
|