Commit 895a02f5 authored by guozhiyuan's avatar guozhiyuan

update:add skip locked func

parents 621c41cc fe60e9d5
生活圈商户系统工具类库,配合yaf框架使用的基础库
\ No newline at end of file
生活圈商户系统工具类库,配合yaf框架使用的基础库
负责人:崔伟峰
\ No newline at end of file
<?php
namespace Api\PhpUtils\Common;
class GoodsSkuId
{
/**
* 生成加密后的 sku_id
* id 非雪花算法生成的sku_id
* category_1_id 一级分类的id,两位,不足两位在十位补零
* category_2_id 二级分类的id,两位,不足三位在百位补零
* spu 商品类型,1虚拟商品,2实体商品
*
* @param $id
* @param $category1Id
* @param $category2Id
* @param $spuType
* @return string
*/
public static function generateGoodSkuId($id, $category1Id, $category2Id, $spuType)
{
if (strlen($category1Id) == 1) {
$category1Id = "0" . $category1Id;
}
if (strlen($category2Id) == 2) {
$category2Id = "0" . $category2Id;
}
$idStr = $id . $category1Id . $category2Id . $spuType;
//转62进制
$tmp = BaseConvert::decToOther($idStr);
$len = strlen($tmp);
$len = $len < 6 ? 6 : $len;
if ($len % 2 == 1) {
$tmp = '0' . $tmp;
$len += 1;
}
$tmp = str_pad(strval($tmp), $len < 6 ? 6 : $len, '0', STR_PAD_LEFT);
$sum = 0;
for ($i = 0; $i < $len / 2; $i++) {
$sum *= 137;
$sum += ord($tmp[$i]) ^ ord($tmp[$len - $i - 1]);
}
$code = '';
$base = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
for ($i = 2; $i > 0; $i--) {
$idx = $sum % 62;
$sum = ($sum - $idx) / 62;
$code = $base[$idx] . $code;
}
return $tmp . $code;
}
}
\ No newline at end of file
......@@ -73,7 +73,7 @@ class Request
$options['on_stats'] = function (TransferStats $stats) use ($use_mon, $url) {
$this->result['http_code'] = $stats->getHandlerStat('http_code');
if (!empty($use_mon)) {
//MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), round($stats->getHandlerStat('total_time'),4) * 1000);
MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), '',round($stats->getHandlerStat('total_time'),4) * 1000);
}
};
//异步get请求
......
......@@ -58,13 +58,15 @@ class MonUtil{
//截取请求第三方服务的uri
$parse_url = parse_url($url);
$preg_uri = $parse_url['path'] ?? $url;
$host = $parse_url['host'] ?? 'other_depent';
//替换$uri中的"."为"_", ":"为"_", "/"为"-"
$request_uri = str_replace(array(".", ":", "/"),array("_", "_", "-"), $preg_uri);
$request_module = str_replace(array(".", ":", "/"),array("_", "_", "-"), $parse_url['host']);
$request_module = str_replace(array(".", ":", "/"),array("_", "_", "-"), $host);
if (empty($module)) {
//默认取url 中的域名作为module
$module = $request_module ?? 'other_depent';
$domain = $_SERVER['SERVER_NAME'] ?? 'unknow_domain';
$module = $domain.($request_module ?? 'other_depent');
}
//接口返回状态码打点
......@@ -82,6 +84,33 @@ class MonUtil{
return $result;
}
/**
* @param $db_host string 链接数据的集群
* @param $operator string 数据库操作
* @param $db_name string 数据库名字 比如mysql ,mongodb
* @param int $request_time
*/
public static function dbMon($db_host,$operator,$db_name,$request_time = -799)
{
if ( empty($operator) || !is_string($db_host) || empty($db_name)) {
return ;
}
if (empty($db_host)) {
$host = 'unknow';
} else {
$host = str_replace(array(".", ":", "/",','),array("_", "_", "-",'_'), $db_host);
}
//记录请求量
self::counting($db_name.'.'.$host,(string)$operator,1);
//耗时打点
if(!empty($request_time) && is_numeric($request_time) && $request_time != -799){
$result = self::timing(($db_name.'.'.$host.$operator), "TotalTime", $request_time,'s');
}
}
/**
* @param string $module
* @param string $index
......@@ -190,12 +219,13 @@ class MonUtil{
* @param string $module 子模块名字
* @param string $index 指标
* @param int $value
* @param string $type 单位
* @return mixed
*/
public static function timing($module = 'api', $index = '', $value = 0){
public static function timing($module = 'api', $index = '', $value = 0,$type='ms'){
$result = "";
$send_msg = self::getMsg($module, $index, $value, "ms");
$send_msg = self::getMsg($module, $index, $value, $type);
if($send_msg){
$result = self::base($send_msg);
}
......
......@@ -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/ 官方文档
*/
......@@ -41,6 +42,7 @@ abstract class MongoBase
private $client = null;
private $database = null;
private $collection = null;
private $host = 'unknow';
const W = 1; // 要求进行写入确认
const WTIMEOUTMS = 1000; // 写入关注超时时间
const MAXTIMEMS = 100; //最大执行时间,毫秒
......@@ -64,6 +66,7 @@ abstract class MongoBase
{
$conf = config('mongo', $this->getConfigIndex());
if (!empty($conf)) {
$this->host = $conf['host'];
$uriOptions = [];
$driverOptions = [];
if (isset($conf['uriOptions'])) {
......@@ -96,6 +99,21 @@ abstract class MongoBase
);
}
$this->database = $this->client->selectDatabase($this->getDatabaseName());
// xhprof 需要 显示的创建固定大小的集合
$db_collection = [];
if ($this->getDatabaseName() === 'xhprof') {
foreach ($this->database->listCollections() as $item){
$db_collection[] = $item['name'];
}
if (!in_array($this->getCollectionName(), $db_collection, true)) {
//默认创建固定20GB大小的集合
$this->database->createCollection($this->getCollectionName(),['capped' => true,'size' => 21474836480]);
}
}
$this->collection = $this->database->selectCollection($this->getCollectionName(), $this->getCollectionOptions());
}
}
......@@ -123,7 +141,7 @@ abstract class MongoBase
if (!$this->collection) {
return false;
}
return call_user_func_array([$this->collection, $method], $args);
}
......@@ -150,7 +168,7 @@ abstract class MongoBase
*
* @return object
*/
public function getClient()
public function getClient()
{
return $this->client;
}
......@@ -160,7 +178,7 @@ abstract class MongoBase
*
* @return object
*/
public function getDatabase()
public function getDatabase()
{
return $this->database;
}
......@@ -170,7 +188,7 @@ abstract class MongoBase
*
* @return object
*/
public function getCollection()
public function getCollection()
{
return $this->collection;
}
......@@ -179,11 +197,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 +209,7 @@ abstract class MongoBase
'typeMap' => ['root' => 'array', 'document' => 'array'],
'writeConcern' => $this->client->getWriteConcern() ?: new WriteConcern(self::W, 3 * self::WTIMEOUTMS),
];
return $collectionOptions;
}
......@@ -201,7 +219,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 +228,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::dbMon($this->host ?? 'unknow','insertOne','mongodb',$end_time-$start_time);
return $res;
}
/**
......@@ -219,7 +243,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 +253,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::dbMon($this->host ?? 'unknow','insertMany','mongodb',$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 +277,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::dbMon($this->host ?? 'unknow','deleteOne','mongodb',$end_time-$start_time);
return $res;
}
/**
* Deletes all documents matching the filter.
*
......@@ -286,9 +319,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::dbMon($this->host ?? 'unknow','findOne','mongodb',$end_time-$start_time);
return $res;
}
/**
* Finds documents matching the query.
*
......@@ -306,11 +343,11 @@ abstract class MongoBase
'limit' => 1000,
'maxTimeMS' => 10 * self::MAXTIMEMS,
];
$cursor = $this->collection->find($filter, $options);
return $cursor->toArray();
}
/**
* Finds a single document and updates it, returning either the original or
* the updated document.
......@@ -337,7 +374,7 @@ abstract class MongoBase
'maxTimeMS' => 5 * self::MAXTIMEMS,
'returnDocument' => \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_BEFORE, //返回更新之前文档
];
return $this->collection->findOneAndUpdate($filter, $update, $options);
}
......@@ -382,7 +419,7 @@ abstract class MongoBase
];
return $this->collection->updateOne($filter, $update, $options);
}
/**
* Updates all documents matching the filter.
*
......
......@@ -208,6 +208,7 @@ abstract class MysqlBase
\PDO::ATTR_STRINGIFY_FETCHES => false,
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_TO_STRING,
\PDO::ATTR_TIMEOUT => intval(self::$iniConf['connect_timeout'] ?? 10)
], $options)
];
......
......@@ -2,6 +2,7 @@
namespace Api\PhpUtils\Mysql;
use Api\PhpUtils\Mon\MonUtil;
use Api\PhpUtils\Mysql\Medoo;
use Api\PhpUtils\Log\FileLog;
......@@ -183,6 +184,9 @@ abstract class MysqlClusterBase
}
self::$dbCurrentConnect = self::$dbConnect[$type.':'.$dbShardingIndex];
//增加mon 打点
$start_time = microtime(true);
self::$sqlResut = call_user_func_array([self::$dbConnect[$type.':'.$dbShardingIndex], $method], $arguments);
if (!self::catchError($method, $arguments)) {
......@@ -193,6 +197,9 @@ abstract class MysqlClusterBase
return false;
}
}
$end_time = microtime(true);
MonUtil::dbMon(self::dbCurrentConnect['server'].'_'.self::dbCurrentConnect['port'] ?? 'unknow_mysql',$method,'mysql',$end_time-$start_time);
return self::formatResult($method, $arguments);
}
......@@ -201,7 +208,7 @@ abstract class MysqlClusterBase
*
* @param [string] $type 读或写
* @param boolean $flush 是否强制重新连接,重试等场景使用
* @return void
* @return
*/
public static function getConnection($type, $dbShardingIndex, $flush = false)
{
......@@ -235,6 +242,7 @@ abstract class MysqlClusterBase
\PDO::ATTR_STRINGIFY_FETCHES => false,
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_TO_STRING,
\PDO::ATTR_TIMEOUT => intval(self::$iniConf['connect_timeout'] ?? 10)
], $options)
];
......
......@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Redis;
use Api\PhpUtils\Http\Request;
use Api\PhpUtils\Cache\ApcuUtil;
use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Mon\MonUtil;
use Redis;
class RedisUtil
......@@ -653,6 +654,12 @@ class RedisUtil
if (!$this->redis) {
return false;
}
return call_user_func_array([$this->redis, $method], $args);
//增加mon打点
$start_time = microtime(true);
$res = call_user_func_array([$this->redis, $method], $args);
$end_time = microtime(true);
MonUtil::dbMon($this->connectServer ?? 'unknow_redis',$method,'redis',$end_time-$start_time);
return $res;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment