Function calculation php runtime - how to load and unload built-in extensions

Keywords: PHP SDK Google Session

In this article, we explain how to load and unload built-in extensions in php runtime based on the throwing and solving process of a specific problem.

problem

According to official documents php execution environment built-in Library As you can see, tablestore php sdk is built into the function computing environment, but the following code is written using the sdk to write the following code:

<?php

use Aliyun\OTS\Consts\ColumnTypeConst;
use Aliyun\OTS\Consts\PrimaryKeyTypeConst;
use Aliyun\OTS\Consts\RowExistenceExpectationConst;
use Aliyun\OTS\OTSClient as OTSClient;

function handler($event, $context) {
  $logger = $GLOBALS['fcLogger'];
  $logger->info('FC recv:'.$event);
  
  $accessKeyId = $context["credentials"]["accessKeyId"];
  $accessKeySecret = $context["credentials"]["accessKeySecret"];
  $securityToken = $context["credentials"]["securityToken"];
  $endpoint = "cn-shanghai.ots.aliyuncs.com";
  $otsClient = new OTSClient (array (
    'EndPoint' => $endpoint,
    'AccessKeyID' => $accessKeyId,
    'AccessKeySecret' => $accessKeySecret,
    'InstanceName' => 'iot-qyt',
    'StsToken' => $securityToken
  ));
  
  $request = array (
    'table_name' => 'gateway',
    'primary_key' => array ( //Primary key
        array('id', 'abcd')
    ),
    "max_versions" => 1
  );
  $response = $otsClient->getRow ($request);
  print json_encode ($response);

  return;
}

When you click to run, the following error will occur:

 {
   "errorMessage": "Call to undefined method Google\\Protobuf\\Internal\\DescriptorPool::getDescriptorByClassName()",
   "errorType": "Error",
   "stackTrace": {
      "file": "/var/fc/runtime/php7.2/builtIn/vendor/aliyun/aliyun-tablestore-sdk-php/src/OTS/ProtoBuffer/Protocol/Message.php",
      "line": 46,
      "traceString": ""
   }
} 

Reason

At present, the php of function calculation installs protobuf extension, while the tablestore relies on protobuf using php script version. At this time, the tablestore sdk prefers to use the extension, which leads to the above error.

solution

In the php runtime of function calculation, in order to support user-defined add-in and delete built-in extensions, the extensions do not take the form of build-in php, which gives users free space to operate. In the official course, there are Use custom extensions In this article, we discuss how to delete or replace built-in extensions.

First, let's look at what extensions function computing has:

extension=session.so
extension=ftp.so
extension=shmop.so
extension=bcmath.so
extension=gettext.so
extension=pcntl.so
extension=simplexml.so
extension=xmlreader.so
extension=bz2.so
extension=gmp.so
extension=pdo.so
extension=soap.so
extension=xmlrpc.so
extension=calendar.so
extension=iconv.so
extension=pdo_mysql.so
extension=sockets.so
extension=xmlwriter.so
extension=ctype.so
extension=imagick.so
extension=phar.so
extension=sysvmsg.so
extension=dom.so
extension=json.so
extension=posix.so
extension=sysvsem.so
extension=exif.so
extension=mbstring.so
extension=protobuf.so
extension=sysvshm.so
extension=fileinfo.so
extension=mysqli.so
extension=redis.so
extension=tokenizer.so
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.so
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so

In this case, we do not want to introduce protobuf, an extension, to affect the use of php version of google/protobuf. The specific actions are as follows:

  • Create an extension directory in the same directory as the function entry file. The directory is as follows:
.
|____extension
| |____my_ext.ini
|____index.php
  • Edit my_ext.ini and comment out protobuf.

Note: Other extensions and unnecessary extensions can be added here to optimize the start-up speed of php runtime.

extension=session.so
extension=ftp.so
extension=shmop.so
extension=bcmath.so
extension=gettext.so
extension=pcntl.so
extension=simplexml.so
extension=xmlreader.so
extension=bz2.so
extension=gmp.so
extension=pdo.so
extension=soap.so
extension=xmlrpc.so
extension=calendar.so
extension=iconv.so
extension=pdo_mysql.so
extension=sockets.so
extension=xmlwriter.so
extension=ctype.so
extension=imagick.so
extension=phar.so
extension=sysvmsg.so
extension=dom.so
extension=json.so
extension=posix.so
extension=sysvsem.so
extension=exif.so
extension=mbstring.so
;extension=protobuf.so
extension=sysvshm.so
extension=fileinfo.so
extension=mysqli.so
extension=redis.so
extension=tokenizer.so
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.so
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
  • Code package creation function based on the above directory
  • Setting the environment variable of the function, PHP_INI_SCAN_DIR points to my_ext.ini under the code directory, when php does not load the protobuf extension.

At this point, it's no problem to perform the above operation on tablestore.

summary

In this paper, by solving a problem, the principle and mechanism of how to load and unload extensions in function computing environment are thrown out. The php runtime built-in groups of function computing are extended to users conveniently, at the same time, they also give users more advanced user-defined operations for extensions. I wish you all a happy time in the world's first language environment.

Posted by ericburnard on Thu, 09 May 2019 11:56:38 -0700