Daemon Example, First Pass, MySQL User's Conference

« previous entry | next entry »
Jan. 24th, 2007 | 07:06 pm

Many moons ago I pushed in an example "daemon plugin" to the server. This week I have Calvin Sun, our team lead for Storage Engines, visiting me. I've been going over the plugin architecture and other internal pieces of the server with him. I'll be posting more about our joint work in a later post.

While going over the plugin structure with Calvin I noticed that the daemon plugin example doesn't give much of an example of what you can do. So I hacked a little more on it :)

You can find it here:
http://download.tangent.org/daemon_example-0.1.tgz

What it now demonstrates is how to create a "heart beat" file. Daemon plugins are raw plugins inside of MySQL, you can literally do anything with them. For hacking the server they are an excellent way of making use of your full imagination.

The Example creates a thread when it is first initialized:
static int daemon_example_plugin_init(void *p)
{
...
if (pthread_create(&con->heartbeat_thread, &attr, mysql_heartbeat, (void *)con) != 0)

This thread will then write a time stamp to a heartbeat file every five seconds.

When the server is shutdown:
static int daemon_example_plugin_deinit(void *p)
{
...
pthread_cancel(con->heartbeat_thread);

We will kill off the heartbeat thread. Then a final message is written to the file before we exit the plugin's finalize function.

This is a simple example of what can be done but it highlights how to create your own worker threads in the server. Daemon plugins are in the similar vein of prometheus stealing fire from the gods. You can do incredible things with them, but at the same time you can burn yourself badly.

Hopefully tomorrow I can put up a couple more examples that I have to show off some of the other functionally that they provide.

I suspect more then a few of them will twist your mind a bit, and hopefully kick start more people into thinking about what else can be done :)

If you are curious about the internals to MySQL I'll be giving a Tutorial at the MySQL User's Conference coming up in April on the "stuff we forget to document" on the Monday of the conference. I'll also be teaching a three hour tutorial on the internals of the storage engine architecture later that day.

You can register for the conference here:
http://www.mysqlconf.com/

Link | Leave a comment | Share

Comments {0}