Embedding Perl in MySQL
The structure is as follows:
typedef struct {
char *returnable;
size_t size;
PerlInterpreter *myperl;
size_t messagesize;
} myperl_passable;
The char pointer returnable is used to store the block of memory, and size and messagesize are used to track both the total size and the current size of the returnable data. Because creating and destroying memory is quite costly, it is important to keep this down to a minimum. The Perl interpreter also will be stored in this structure.
At this point, the work of setting up the Perl interpreter that will be used for the query must be done. Currently, MyPerl creates a new Perl interpreter for each request to keep from leaking memory and ensure the security of data between requests. The odds of this becoming a pool of Perl interpreters in the future is quite high:
if((my_perl = perl_alloc()) == NULL) {
strncpy(message, "Could not allocate perl",
MYSQL_ERRMSG_SIZE);
return 1;
}
perl_construct(my_perl);
exitstatus = perl_parse(my_perl, xs_init, 4,
embedding, environ);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
if (exitstatus) {
strncpy(message, "Error in creating perl parser",
MYSQL_ERRMSG_SIZE);
goto error;
}
exitstatus = perl_run(my_perl);
if (exitstatus) {
strncpy(message, "Error in parsing your perl",
MYSQL_ERRMSG_SIZE);
goto error;
}
The first function, perl_alloc(), allocates a new Perl interpreter that is then constructed with perl_construct(). Now it is simply a matter of starting up Perl. The embedding variable is used as parameters for the Perl interpreter. These are exactly the same parameters you would use on the command line. Errors must be checked for at every point in dealing with the Perl interpreter. In the current design, if an error occurs, MyPerl jumps to a collection of function calls that will clean up the memory that has been allocated.
Now that a good Perl interpreter exists, a few defaults in the pass structure need to be set; the interpreter must be stored, and the address of the structure must be stored in initid->ptr pointer so it can be used throughout the query:
pass->returnable = NULL; pass->size = 0; pass->messagesize = 0; pass->myperl = my_perl; initid->ptr = (char*)pass; return 0;
After all of this setup, MyPerl is ready to start taking requests:
char *
myperl(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error)
{
myperl_passable *pass =
(myperl_passable *)initid->ptr ;
char *returnable = NULL;
unsigned long x = 0;
size_t size = 0;
char *newspot = NULL;
char *string = NULL;
myperl_passable *pass =
(myperl_passable *)initid->ptr ;
STRLEN n_a; //Return strings length
PerlInterpreter *my_perl = pass->myperl;
It is important that the address of the interpreter is copied into a variable named my_perl. Much of the Perl internals are based on macros that expect you to use variables with certain names. STRLEN is a variable type that Perl uses to store the size of strings.
At this point the interpreter is called:
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
// Now we push the additional values into ARGV
for(x = 0; x < args->arg_count ; x++) {
XPUSHs(sv_2mortal(newSVpvn(args->args[x],
args->lengths[x])));
}
PUTBACK;
call_pv("MyPerl::Request::handler", G_SCALAR);
SPAGAIN;
string = POPpx;
size = (size_t)n_a;
XPUSHs is used to push all of the rows into an array of strings that will be passed to the Perl function handler() in the library MyPerl::Request(). This Perl module is similar to the Apache::Request module except where the Apache module uses a filename to track code it has eval'ed, MyPerl uses the code itself to determine this.
Because the variable named size now holds the size of the data that will be returned, space needs to be allocated for it:
if (size) {
if(pass->size < size) {
newspot = (char *)realloc(pass->returnable,
size);
if(!newspot) {
error[0] = '1';
returnable = NULL;
goto error;
}
pass->size = size;
pass->returnable = newspot;
}
// Always know the current size,
// it may be less than the full size
pass->messagesize = size;
memcpy(pass->returnable, string, size);
} else {
is_null[0] = '1';
}
error:
PUTBACK;
FREETMPS;
LEAVE;
*length = pass->messagesize;
return pass->returnable;
}
This is where the information that needs to be sent to the server is stored. The memory call realloc() is used if more memory needs to be allocated. If no data is received from the Perl interpreter, is_null is set to 1 so that MySQL knows a null result should be returned to the client. MyPerl also makes sure to clean up memory that might have been used for the call_pv() function.
The myperl() function is now called for each row. After MySQL returns its data to the client, it calls the deinit function to free the interpreter and release any allocated memory:
void myperl_deinit(UDF_INIT *initid)
{
myperl_passable *pass =
(myperl_passable *)initid->ptr ;
perl_destruct(pass->myperl);
perl_free(pass->myperl);
free(pass->returnable);
free(initid->ptr);
}
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Android's Limits
- Reachli - Amplifying your
1 hour 6 min ago - excellent
1 hour 55 min ago - good point!
1 hour 57 min ago - Varnish works!
2 hours 6 min ago - Reply to comment | Linux Journal
2 hours 36 min ago - Reply to comment | Linux Journal
5 hours 2 min ago - Reply to comment | Linux Journal
9 hours 2 min ago - Yeah, user namespaces are
10 hours 18 min ago - Cari Uang
13 hours 49 min ago - user namespaces
16 hours 43 min ago




Comments
Memory leak
Hi sir,
I'm Nguyen Ngoc Tam. After reading your this topic, i found that the memory of your MYSQL server would increase so that it could cause the memory leak, when it handled a lot of perl queries. Have you controlled this situation ?.
Regards
Ngoc Tam