Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
php_utils
Commits
1368a385
Commit
1368a385
authored
Jun 22, 2021
by
yujiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update:mongodb add mon打点
parent
ccd5ae20
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
25 deletions
+62
-25
MonUtil.php
src/Mon/MonUtil.php
+17
-0
MongoBase.php
src/Mongo/MongoBase.php
+45
-25
No files found.
src/Mon/MonUtil.php
View file @
1368a385
...
...
@@ -84,6 +84,23 @@ class MonUtil{
return
$result
;
}
public
static
function
mongoMon
(
$mysql_host
,
$operator
,
$request_time
=
-
799
)
{
if
(
empty
(
$mysql_host
)
||
empty
(
$operator
))
{
return
;
}
//记录请求量
self
::
counting
(
'mongodb.'
.
$mysql_host
,
(
string
)
$operator
,
0
)
.
"
\n
"
;
//耗时打点
if
(
!
empty
(
$request_time
)
&&
is_numeric
(
$request_time
)
&&
$request_time
!=
-
799
){
$result
=
self
::
timing
((
string
)(
'mongodb.'
.
$mysql_host
.
$operator
),
"TotalTime"
,
$request_time
);
}
}
/**
* @param string $module
* @param string $index
...
...
src/Mongo/MongoBase.php
View file @
1368a385
...
...
@@ -7,12 +7,13 @@ use MongoDB\Driver\Manager;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
Api\PhpUtils\Mon\MonUtil
;
/**
* MongoDB基类
*
*
* 1. MongoDB配置信息在yaconf的mongo.ini中,configIndex, databaseName, collectionName 在子类中设置
*
*
* 2. writeConcern, readConcern, readPreference, typeMap 是可以从MongoDB\Driver\Manager继承的,
* 可以参考扩展API文档修改
* $options = [
...
...
@@ -22,16 +23,16 @@ use MongoDB\Driver\WriteConcern;
* 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
* ];
* @see https://www.php.net/manual/zh/book.mongodb.php
*
*
* 3. 提供常用的CURD方法,共他方法会调用 __call()方法
*
*
* 4. MongoDB建议对重要的数据使用 {w: “marjority”} 的选项 (WriteConcern::MAJORITY)。
* 可以保证数据在复制到多数节点后才返回成功结果。使用该机制可以有效防止数据回滚的发生。
* 另外你可以使用 {j:1} (可以和 w:”majrotiy” 结合使用)来指定数据必须在写入WAL日志之后才向应用返回成功确认。这个会导致写入性能有所下降,但是对于重要的数据可以考虑使用
*
*
* 5. 异常的捕获与处理 及 getMatchedCount(), getModifiedCount(), getUpsertedCount() 在业务中按需获取,写入retry(1s,2s,4s...)
*
* 6. 根据镜像中php的monogodb扩展版本选择使用1.4版本
*
* 6. 根据镜像中php的monogodb扩展版本选择使用1.4版本
* @see https://docs.mongodb.com/drivers/php/ 驱动兼容对照表
* @see https://docs.mongodb.com/php-library/v1.4/ 官方文档
*/
...
...
@@ -123,7 +124,7 @@ abstract class MongoBase
if
(
!
$this
->
collection
)
{
return
false
;
}
return
call_user_func_array
([
$this
->
collection
,
$method
],
$args
);
}
...
...
@@ -150,7 +151,7 @@ abstract class MongoBase
*
* @return object
*/
public
function
getClient
()
public
function
getClient
()
{
return
$this
->
client
;
}
...
...
@@ -160,7 +161,7 @@ abstract class MongoBase
*
* @return object
*/
public
function
getDatabase
()
public
function
getDatabase
()
{
return
$this
->
database
;
}
...
...
@@ -170,7 +171,7 @@ abstract class MongoBase
*
* @return object
*/
public
function
getCollection
()
public
function
getCollection
()
{
return
$this
->
collection
;
}
...
...
@@ -179,11 +180,11 @@ abstract class MongoBase
* 集合的初始化参数
*
* 可以继承初始化配置文件中,默认的,或都子类中的
*
*
* @param array $collectionOptions
* @return array
*/
protected
function
getCollectionOptions
(
array
$collectionOptions
=
[])
protected
function
getCollectionOptions
(
array
$collectionOptions
=
[])
{
$collectionOptions
+=
[
'readConcern'
=>
$this
->
client
->
getreadConcern
()
?:
new
ReadConcern
(
ReadConcern
::
LOCAL
),
//3.2之前的服务器版本不支持此功能
...
...
@@ -191,7 +192,7 @@ abstract class MongoBase
'typeMap'
=>
[
'root'
=>
'array'
,
'document'
=>
'array'
],
'writeConcern'
=>
$this
->
client
->
getWriteConcern
()
?:
new
WriteConcern
(
self
::
W
,
3
*
self
::
WTIMEOUTMS
),
];
return
$collectionOptions
;
}
...
...
@@ -201,7 +202,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertOne/
* @param array|object $document The document to insert
* @param array $options Command options
* @return InsertOneResult
* @return
\MongoDB\
InsertOneResult
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
...
...
@@ -210,7 +211,13 @@ abstract class MongoBase
$options
+=
[
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
3
*
self
::
WTIMEOUTMS
),
// W=1要求进行写入确认, 超时时间3000毫秒
];
return
$this
->
collection
->
insertOne
(
$document
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
insertOne
(
$document
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'insertOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
...
...
@@ -219,7 +226,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertMany/
* @param array[]|object[] $documents The documents to insert
* @param array $options Command options
* @return InsertManyResult
* @return
\MongoDB\
InsertManyResult
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
...
...
@@ -229,16 +236,21 @@ abstract class MongoBase
'ordered'
=>
true
,
// 默认值为true, 当单个写入失败时,该操作将停止而不执行剩余的写入操作并引发异常
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
5
*
self
::
WTIMEOUTMS
),
];
return
$this
->
collection
->
insertMany
(
$documents
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
insertMany
(
$documents
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'insertMany'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
* Deletes at most one document matching the filter.
*
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-deleteOne/
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
* @return DeleteResult
* @return
\MongoDB\
DeleteResult
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
...
...
@@ -248,9 +260,13 @@ abstract class MongoBase
$options
+=
[
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
3
*
self
::
WTIMEOUTMS
),
];
return
$this
->
collection
->
deleteOne
(
$filter
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
deleteOne
(
$filter
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'deleteOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
* Deletes all documents matching the filter.
*
...
...
@@ -286,9 +302,13 @@ abstract class MongoBase
$options
+=
[
'maxTimeMS'
=>
3
*
self
::
MAXTIMEMS
,
];
return
$this
->
collection
->
findOne
(
$filter
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
findOne
(
$filter
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'findOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
* Finds documents matching the query.
*
...
...
@@ -382,7 +402,7 @@ abstract class MongoBase
];
return
$this
->
collection
->
updateOne
(
$filter
,
$update
,
$options
);
}
/**
* Updates all documents matching the filter.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment