The same GraphQL API request, sometimes responds to 500 in the browser, and has no response data, sometimes responds to 200, and has response data
1. For the same GraphQL API request, it sometimes responds to 500 in the browser, and there is no response data, and sometimes the response is 200, and there is a response data. as shown in Figure 1
2. When the response is 500, there is no response data. Decide to set the error level to output a more detailed error log. In the entry file index.php, write the following implementation. as shown in Figure 2
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('error_log', '/tmp/php_errors.log');
3. After the response is 500, check the file /tmp/php_errors.log. PHP fatal error: allow memory size of 134217728 bytes exhausted. The content is as follows. as shown in Figure 3
/var/www # cd /tmp
/tmp # ls
php_errors.log
/tmp # cat php_errors.log
[19-Apr-2023 17:12:22 Asia/Shanghai] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 12288 bytes) in /var/www/object/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 262
[19-Apr-2023 17:12:22 Asia/Shanghai] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in /var/www/object/vendor/composer/ClassLoader.php on line 571
4. In the ingress file index.php, the adjustment memory is limited to 1000M, and the response is 200. Confirm that the memory usage is too large somewhere in the program.
ini_set('memory_limit', '1000M');
5. Edit /var/www/object/vendor/doctrine/dbal/lib/doctrine/dbal/driver/pdoStatement.php , in 262 Near the line, print out the corresponding data and output it to the log file. as shown in Figure 4
file_put_contents(storage_path() . '/logs/doFetchAll-slice.txt', print_r($slice, true), FILE_APPEND | LOCK_EX);
file_put_contents(storage_path() . '/logs/doFetchAll-queryString.txt', print_r($this->queryString, true), FILE_APPEND | LOCK_EX);
$data = parent::fetchAll(...$slice);
file_put_contents(storage_path() . '/logs/doFetchAll-data.txt', print_r($data, true), FILE_APPEND | LOCK_EX);
/var/www/object/storage/logs # ls -l
total 65604
-rw-r--r-- 1 www www 65711812 Apr 20 01:43 doFetchAll-data.txt
-rw-r--r-- 1 www www 50958 Apr 20 01:43 doFetchAll-queryString.txt
-rw-r--r-- 1 www www 4820 Apr 20 01:43 doFetchAll-slice.txt
-rw-r--r-- 1 www www 4530 Apr 20 01:43 explicabo78.log
/var/www/object/storage/logs #
6. Based on the analysis of the log file, the reason is obtained. When executing the SQL query, the limit constraint is not added, and then all the data of the entire table is queried and put into memory, resulting in the memory exceeding the limit.



