-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Added database slave failover #15553
Changes from all commits
4a839cf
9f8dfee
ae4b2f9
bd9acdf
c9125f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Illuminate\Database\Connectors; | ||
|
||
use PDO; | ||
use PDOException; | ||
use Illuminate\Support\Arr; | ||
use InvalidArgumentException; | ||
use Illuminate\Database\MySqlConnection; | ||
|
@@ -95,6 +96,58 @@ protected function createReadPdo(array $config) | |
* @return \Closure | ||
*/ | ||
protected function createPdoResolver(array $config) | ||
{ | ||
if (array_key_exists('host', $config)) { | ||
return $this->createPdoResolverWithHosts($config); | ||
} | ||
|
||
return $this->createPdoResolverWithoutHosts($config); | ||
} | ||
|
||
/** | ||
* Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts. | ||
* | ||
* @param array $config | ||
* @return \Closure | ||
*/ | ||
protected function createPdoResolverWithHosts(array $config) | ||
{ | ||
return function () use ($config) { | ||
if (! is_array($config['host'])) { | ||
$hosts = [$config['host']]; | ||
} else { | ||
$hosts = $config['host']; | ||
shuffle($hosts); | ||
} | ||
|
||
$lastHost = end($hosts); | ||
foreach ($hosts as $host) { | ||
$config['host'] = $host; | ||
|
||
try { | ||
return $this->createConnector($config)->connect($config); | ||
} catch (PDOException $e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this suppress any other exceptions? Such as the user not having access to the database or the database not existing? If so I'd think we would want to be much more narrow in scope and report all but the actual connection error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it could suppress other exceptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exceptions definitely need to bubble up to at lease the handler so we can report them to things like sentry. Otherwise your primary db could be failing and you would have no idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an excellent point. |
||
if ($host !== $lastHost) { | ||
$this->container->make('Illuminate\Contracts\Debug\ExceptionHandler')->report($e); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty comment should be added. } catch(PDOException $e) {
//
} |
||
} | ||
|
||
if (empty($hosts)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not making this check before the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I move the check above the |
||
throw new InvalidArgumentException('Database hosts array cannot be empty'); | ||
} | ||
|
||
throw $e; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are you throwing here? You're not even in a try catch block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only way for the code to reach that throw statement is if all hosts have failed. If so, the |
||
}; | ||
} | ||
|
||
/** | ||
* Create a new Closure that resolves to a PDO instance where there is no configured host. | ||
* | ||
* @param array $config | ||
* @return \Closure | ||
*/ | ||
protected function createPdoResolverWithoutHosts(array $config) | ||
{ | ||
return function () use ($config) { | ||
return $this->createConnector($config)->connect($config); | ||
|
@@ -111,12 +164,6 @@ protected function getReadConfig(array $config) | |
{ | ||
$readConfig = $this->getReadWriteConfig($config, 'read'); | ||
|
||
if (isset($readConfig['host']) && is_array($readConfig['host'])) { | ||
$readConfig['host'] = count($readConfig['host']) > 1 | ||
? $readConfig['host'][array_rand($readConfig['host'])] | ||
: $readConfig['host'][0]; | ||
} | ||
|
||
return $this->mergeReadWriteConfig($config, $readConfig); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would a database configuration ever not have a
host
property?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I could see, sqlite configurations don't have a
host
property, hence the two different methods.