Skip to content

Commit b1fc06f

Browse files
author
luofei614@gmail.com
committed
增加Mysql超额自动切换到备用数据库的功能
git-svn-id: http://thinkphp.googlecode.com/svn/trunk@2984 539fd3ec-2725-0410-b7ab-251e373a8e33
1 parent 399b225 commit b1fc06f

File tree

5 files changed

+105
-49
lines changed

5 files changed

+105
-49
lines changed

Extend/Engine/Sae/Common/common.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -524,47 +524,4 @@ function sae_storage_root($domain){
524524
return '~sae_storage_root("'.$domain.'")';
525525
$s=Think::instance('SaeStorage');
526526
return rtrim($s->getUrl('Public',''),'/');
527-
}
528-
//[sae] 将halt函数一到common文件中
529-
// 错误输出
530-
function halt($error) {
531-
$e = array();
532-
if (APP_DEBUG) {
533-
//调试模式下输出错误信息
534-
if (!is_array($error)) {
535-
$trace = debug_backtrace();
536-
$e['message'] = $error;
537-
$e['file'] = $trace[0]['file'];
538-
$e['class'] = $trace[0]['class'];
539-
$e['function'] = $trace[0]['function'];
540-
$e['line'] = $trace[0]['line'];
541-
$traceInfo = '';
542-
$time = date('y-m-d H:i:m');
543-
foreach ($trace as $t) {
544-
$traceInfo .= '[' . $time . '] ' . $t['file'] . ' (' . $t['line'] . ') ';
545-
$traceInfo .= $t['class'] . $t['type'] . $t['function'] . '(';
546-
$traceInfo .= implode(', ', $t['args']);
547-
$traceInfo .=')<br/>';
548-
}
549-
$e['trace'] = $traceInfo;
550-
} else {
551-
$e = $error;
552-
}
553-
// 包含异常页面模板
554-
include C('TMPL_EXCEPTION_FILE');
555-
} else {
556-
//否则定向到错误页面
557-
$error_page = C('ERROR_PAGE');
558-
if (!empty($error_page)) {
559-
redirect($error_page);
560-
} else {
561-
if (C('SHOW_ERROR_MSG'))
562-
$e['message'] = is_array($error) ? $error['message'] : $error;
563-
else
564-
$e['message'] = C('ERROR_MESSAGE');
565-
// 包含异常页面模板
566-
include C('TMPL_EXCEPTION_FILE');
567-
}
568-
}
569-
exit;
570527
}

Extend/Engine/Sae/Common/functions.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,49 @@
2121
+------------------------------------------------------------------------------
2222
*/
2323

24+
// 错误输出
25+
function halt($error) {
26+
$e = array();
27+
if (APP_DEBUG) {
28+
//调试模式下输出错误信息
29+
if (!is_array($error)) {
30+
$trace = debug_backtrace();
31+
$e['message'] = $error;
32+
$e['file'] = $trace[0]['file'];
33+
$e['class'] = isset($trace[0]['class'])?$trace[0]['class']:'';
34+
$e['function'] = isset($trace[0]['function'])?$trace[0]['function']:'';
35+
$e['line'] = $trace[0]['line'];
36+
$traceInfo = '';
37+
$time = date('y-m-d H:i:m');
38+
foreach ($trace as $t) {
39+
$traceInfo .= '[' . $time . '] ' . $t['file'] . ' (' . $t['line'] . ') ';
40+
$traceInfo .= $t['class'] . $t['type'] . $t['function'] . '(';
41+
$traceInfo .= implode(', ', $t['args']);
42+
$traceInfo .=')<br/>';
43+
}
44+
$e['trace'] = $traceInfo;
45+
} else {
46+
$e = $error;
47+
}
48+
// 包含异常页面模板
49+
include C('TMPL_EXCEPTION_FILE');
50+
} else {
51+
//否则定向到错误页面
52+
$error_page = C('ERROR_PAGE');
53+
if (!empty($error_page)) {
54+
redirect($error_page);
55+
} else {
56+
if (C('SHOW_ERROR_MSG'))
57+
$e['message'] = is_array($error) ? $error['message'] : $error;
58+
else
59+
$e['message'] = C('ERROR_MESSAGE');
60+
// 包含异常页面模板
61+
include C('TMPL_EXCEPTION_FILE');
62+
}
63+
}
64+
exit;
65+
}
66+
2467
// 自定义异常处理
2568
function throw_exception($msg, $type='ThinkException', $code=0) {
2669
if (class_exists($type, false))

Extend/Engine/Sae/Conf/convention_sae.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
'DB_RW_SEPARATE'=>true,
1111
'DATA_CACHE_TYPE'=>'Memcache',//SAE下,缓存类型改为Memcache
1212
'DB_DEPLOY_TYPE'=> 1, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
13+
//备用数据库配置,mysql被禁用时启用
14+
'SPARE_DB_HOST'=>'',
15+
'SPARE_DB_NAME'=>'',
16+
'SPARE_DB_USER'=>'',
17+
'SPARE_DB_PWD'=>'',
18+
'SPARE_DB_PORT'=>'',
19+
'SPARE_DB_WRITEABLE'=>false,//备用数据库是否允许写入数据
20+
'SPARE_INFO_FUNCTION'=>'',//给用户显示提示信息的函数
1321
//短信预警设置
1422
'SMS_ON'=>false,//短信预警开关
1523
'SMS_MOBILE'=>'',//接收短信的手机号

Extend/Engine/Sae/Lib/Driver/Db/DbMysql.class.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DbMysql extends Db{
3434
* @param array $config 数据库配置数组
3535
+----------------------------------------------------------
3636
*/
37+
private $is_spare=false;//[sae]是否连接的为备用数据库
3738
public function __construct($config=''){
3839
if ( !extension_loaded('mysql') ) {
3940
throw_exception(L('_NOT_SUPPERT_').':mysql');
@@ -69,9 +70,26 @@ public function connect($config='',$linkNum=0,$force=false) {
6970
}
7071
if ( !$this->linkID[$linkNum] || (!empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) ) {
7172
$errStr=mysql_error();
72-
//[sae] 短信预警
73-
if(C('SMS_ON')) Sms::send('数据库连接时出错,请在SAE日志中心查看详情', $errStr,Sms::MYSQL_ERROR);
74-
throw_exception($errStr);
73+
$errno=mysql_errno();
74+
if($errno==13047){
75+
if(C('SMS_ON')) Sms::send('mysql超额被禁用,请在SAE日志中心查看详情', $errStr,Sms::MYSQL_ERROR);
76+
//[sae]启动备用数据库
77+
if(C('SPARE_DB_HOST')){
78+
$this->linkID[$linkNum]=mysql_connect( C('SPARE_DB_HOST').(C('SPARE_DB_PORT')?':'.C('SPARE_DB_PORT'):''), C('SPARE_DB_USER'), C('SPARE_DB_PWD'),true,CLIENT_MULTI_RESULTS);
79+
mysql_select_db(C('SPARE_DB_NAME'), $this->linkID[$linkNum]);
80+
if(mysql_errno($this->linkID[$linkNum])){
81+
throw_exception(mysql_error($this->linkID[$linkNum]));
82+
}
83+
$this->is_spare=true;
84+
}else{
85+
throw_exception($errStr);
86+
}
87+
//标记使用备用数据库状态
88+
}else{
89+
//[sae] 短信预警
90+
if(C('SMS_ON')) Sms::send('数据库连接时出错,请在SAE日志中心查看详情', $errStr,Sms::MYSQL_ERROR);
91+
throw_exception($errStr);
92+
}
7593
}
7694
$dbVersion = mysql_get_server_info($this->linkID[$linkNum]);
7795
if ($dbVersion >= '4.1') {
@@ -152,6 +170,13 @@ public function query($str) {
152170
+----------------------------------------------------------
153171
*/
154172
public function execute($str) {
173+
//[sae] 判断是否开启了备用数据库
174+
if($this->is_spare && !C('SPARE_DB_WRITEABLE')){
175+
$this->error='mysql out of quota and spare db not writeable';
176+
if(C('SPARE_INFO_FUNCTION'))
177+
call_user_func(C('SPARE_INFO_FUNCTION'));
178+
return false;
179+
}
155180
$this->initConnect(true);
156181
if ( !$this->_linkID ) return false;
157182
$this->queryStr = $str;

Extend/Engine/Sae/Lib/Driver/Db/DbMysqli.class.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,25 @@ public function connect($config='',$linkNum=0) {
6060
$this->linkID[$linkNum] = new mysqli($config['hostname'],$config['username'],$config['password'],$config['database'],$config['hostport']?intval($config['hostport']):3306);
6161
if (mysqli_connect_errno()){
6262
$errStr=mysqli_connect_error();
63-
//[sae] 短信预警
64-
if(C('SMS_ON')) Sms::send('连接数据库时出错,请在SAE日志中心查看详情',$errStr,Sms::MYSQL_ERROR);
65-
throw_exception($errStr);
63+
$errno=mysqli_connect_errno();
64+
if($errno==13047){
65+
if(C('SMS_ON')) Sms::send('mysql超额被禁用,请在SAE日志中心查看详情', $errStr,Sms::MYSQL_ERROR);
66+
//[sae]启动备用数据库
67+
if(C('SPARE_DB_HOST')){
68+
$this->linkID[$linkNum]=new mysqli(C('SPARE_DB_HOST'),C('SPARE_DB_USER'),C('SPARE_DB_PWD'),C('SPARE_DB_NAME'),C('SPARE_DB_PORT')?intval(C('SPARE_DB_PORT')):3306);
69+
if(mysqli_connect_error()){
70+
throw_exception(mysqli_connect_errno());
71+
}
72+
$this->is_spare=true;
73+
}else{
74+
throw_exception($errStr);
75+
}
76+
//标记使用备用数据库状态
77+
}else{
78+
//[sae] 短信预警
79+
if(C('SMS_ON')) Sms::send('数据库连接时出错,请在SAE日志中心查看详情', $errStr,Sms::MYSQL_ERROR);
80+
throw_exception($errStr);
81+
}
6682
}
6783
$dbVersion = $this->linkID[$linkNum]->server_version;
6884
if ($dbVersion >= '4.1') {
@@ -147,6 +163,13 @@ public function query($str) {
147163
+----------------------------------------------------------
148164
*/
149165
public function execute($str) {
166+
//[sae] 判断是否开启了备用数据库
167+
if($this->is_spare && !C('SPARE_DB_WRITEABLE')){
168+
$this->error='mysql out of quota and spare db not writeable';
169+
if(C('SPARE_INFO_FUNCTION'))
170+
call_user_func(C('SPARE_INFO_FUNCTION'));
171+
return false;
172+
}
150173
$this->initConnect(true);
151174
if ( !$this->_linkID ) return false;
152175
$this->queryStr = $str;

0 commit comments

Comments
 (0)