Perl example (multiple simultaneous connections)
To create a remote mode program that can handle multiple simultaneous connections, each connection must be handled independently to avoid blocking. The Perl AE framework performs this task. By default, the Perl AE framework runs single-threaded while handling each request. It is possible to override this functionality and have the framework run using forking to handle multiple simultaneous requests.
When run() is called on the class, the class calls getRequestHandlingStyle(). By default it references the class variable, DEFAULT_REQUEST_HANDLING_STYLE. There are two ways to override AE execution. First, you can set the class variable DEFAULT_REQUEST_HANDLING_STYLE using the setDefaultRequestHandlingStyle(), as shown in the example below. Or, the function getRequestHandlingStyle() can be overloaded when more intricate behavior is desired. The example shown uses the simple method to force the framework to fork and show that the internal handler is not being run in the same process as the running remote AE by returning the current and the parent process IDs.
This example uses the following file name:
ForkAe.pm
Code
package ForkAe;
use strict;
use English;
use autodie;
use nzae::Ae;
our @ISA = qw(nzae::Ae);
my $ae = ForkAe->new();
my $parentProcessId = $PID;
$ae->setDefaultRequestHandlingStyle($ae->getRequestHandlingStyleFork());
$ae->run();
sub _getFunctionResult($@)
{
my $self = shift;
return ($parentProcessId, $PID);
}
1;
Deployment
Deploy the script:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language perl --version 3 \
--template deploy ForkAe.pm
Registration
Register both the forking AE and the launcher AE:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language perl --version 3 \
--template udtf --exe ForkAe.pm --sig "remote_fork(int4)" \
--return "table(parent_id int4, child_id int4)" --remote \
--rname forkRemote
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language perl --version 3 \
--template udtf --exe ForkAe.pm --sig "launch_remote_fork(int4)" \
--return "table(aeresult varchar(255))" --remote --launch \
--rname forkRemote
Running
Run the remote AE server on the system database:
SELECT * FROM TABLE WITH FINAL(launch_remote_fork(0));
AERESULT
-------------------------------------------------------------------------
tran: 8592 session: 16283 DATA slc: 0 hardware: 0 machine: TT4-R045 process:
32082 thread: 32082
(1 row)
Now run the forking AE:
SELECT * FROM TABLE WITH FINAL(remote_fork(0));
PARENT_ID | CHILD_ID
----------+----------
32082 | 32154
(1 row)
The Perl framework is performing the forking. If the DEFAULT_REQUEST_HANDLING_STYLE variable were not overridden, the numbers would match, as no new process is created to handle the request.
The AE can now be terminated using the following SQL command:
SELECT aeresult FROM TABLE WITH FINAL(inza..nzaejobcontrol('stop', 0,
'forkRemote',false, NULL, NULL));
AERESULT
---------------------------------------------------------------------
vnairsim 25227 (forkRemote dataslc:-1 sess:-1 trans:-1) AE stopped
(1 row)
Although in these examples the /nz/export path is hard-coded, when creating AEs for release, this path should be retrieved through the command nzenv using the NZ_EXPORT_DIR variable.