<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Toru Maesaka &#187; mysql</title>
	<atom:link href="http://torum.net/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://torum.net</link>
	<description>Hackaholic and a Web Addict based in Tokyo</description>
	<lastBuildDate>Thu, 22 Jul 2010 09:59:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Speaking at the MySQL Conference 2010</title>
		<link>http://torum.net/2010/02/speaking-at-the-mysql-conference-2010/</link>
		<comments>http://torum.net/2010/02/speaking-at-the-mysql-conference-2010/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 09:31:30 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[travel]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2336</guid>
		<description><![CDATA[I&#8217;m a little behind in announcing this but I&#8217;m going to be speaking at O&#8217;Reilly&#8217;s MySQL Conference this year. My presentation is a three hour tutorial titled, Drizzle Storage Engine Development. Practical Example with BlitzDB. Three hours is a long time but I assure you that there will be a break. This session isn&#8217;t solely [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a little behind in announcing this but I&#8217;m going to be speaking at O&#8217;Reilly&#8217;s MySQL Conference this year. My presentation is a three hour tutorial titled, <a href="http://en.oreilly.com/mysql2010/public/schedule/detail/12409">Drizzle Storage Engine Development. Practical Example with BlitzDB</a>. Three hours is a long time but I assure you that there will be a break.</p>
<p>This session isn&#8217;t solely about going through Drizzle&#8217;s Storage Engine API. Various performance topics like B+Tree structure, memory handling and concurrency control will be covered. I will also go through BlitzDB&#8217;s design concept and it&#8217;s internal stuff. So, needless to say I&#8217;ll talk a lot about Tokyo Cabinet and it&#8217;s internals as well.</p>
<p>Hopefully those that come along will walk out of the tutorial standing far ahead of the start line. It will help you get started on reading the implementation of other storage engines in the MySQL ecosystem (MyISAM, InnoDB, PBXT, Federated and so forth). Better yet you will start writing one.</p>
<p>Looking forward to seeing you there :)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2010/02/speaking-at-the-mysql-conference-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Notes on HEAP/MyISAM Index Key Handling on WRITE</title>
		<link>http://torum.net/2010/01/notes-on-heap-myisam-key-generation/</link>
		<comments>http://torum.net/2010/01/notes-on-heap-myisam-key-generation/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 08:57:05 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2331</guid>
		<description><![CDATA[Disclaimer: This post is based on HEAP/MyISAM&#8217;s sourcecode in Drizzle. Here are my brief notes on investigating how index keys are generated in HEAP and MyISAM. I lurked through these because I&#8217;ve started preparing for decent index support in BlitzDB. I also wrote this to assist my biological memory for later grepping (I have terrible [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Disclaimer: This post is based on HEAP/MyISAM&#8217;s sourcecode in Drizzle.</strong></p>
<p>Here are my brief notes on investigating how index keys are generated in HEAP and MyISAM. I lurked through these because I&#8217;ve started preparing for decent index support in BlitzDB. I also wrote this to assist my biological memory for later grepping (I have terrible memory for names). I&#8217;m only going to cover key generation on write in this post. Otherwise this post is going to be massive.</p>
<h3>HEAP Engine</h3>
<p>The index structure of HEAP can be either BTREE or HASH (in MySQL doc terms). Like other engines HEAP has a structure for keeping Key definition (parts, type, logic and etc). This structure is called HP_KEYDEF and it contains function pointers for write, delete, and getting the length of the key. These function pointers are assigned to at table creation or when the table is opened. The assigned function depends on the data structure of the index and it can be either of the following:</p>
<h4>BTREE</h4>
<ul>
<li>hp_rb_write_key()</li>
<li>hp_rb_delete_key()</li>
</ul>
<h4>HASH</h4>
<ul>
<li>hp_write_key()</li>
<li>hp_delete_key()</li>
</ul>
<p>As for get_key_length(), either of the following functions are used for both data structures.</p>
<ul>
<li>hp_rb_var_key_length()</li>
<li>hp_rb_null_key_length()</li>
<li>hp_rb_key_length()</li>
</ul>
<p>When writing a row to the tree, HEAP writes to the index using a key generated by hp_rb_make_key(). Note that it does not use this for the hash index. The generated key is populated inside &#8216;recbuffer&#8217; in HEAP&#8217;s handler object (HP_INFO structure).</p>
<p>From my understanding, it loops through the key segments (I suspect it is similar the internal  KEY_PART_INFO structure) and appropriately copies each key field value to the output buffer. By meaning &#8220;appropriately&#8221; it respects the characteristics of the data type when packing the buffer. For example, for a variable length field, it will only copy the actual data and not the max possible size of it. The final byte that is copied to the buffer is the address of the chunk where the record lives.</p>
<h3>MyISAM Engine</h3>
<p>The upper layer of key handling in MyISAM looks somewhat similar to HEAP so you can really tell that it was written by the same people. Things are nicely wrapped together by the MYISAM_SHARE structure so it&#8217;s relatively easy to follow. BlitzDB has a class called BlitzShare for the same purpose (This is based off Archive Engine&#8217;s ArchiveShare class).</p>
<p>Like HEAP, MyISAM has a structure for individual key definition called MI_KEYDEF (it&#8217;s defined in myisam.h). There are more function pointers in this structure than HEAP.</p>
<ul>
<li>bin_search()</li>
<li>get_key()</li>
<li>pack_key()</li>
<li>store_key()</li>
<li>ck_insert()</li>
<li>ck_delete()</li>
</ul>
<p>In Drizzle, _mi_ck_write() is assigned to ck_insert() which is the entry point to writing a MyISAM index. The key that MyISAM uses to write to the index is generated by _mi_make_key(). Like HEAP, it will loop through the key segments and pack the relevant fields accordingly to the characteristic of the data type. The output buffer belongs to MyISAM&#8217;s hander (lastkey2).</p>
<h3>From Here</h3>
<p>I&#8217;ve actually written a naive key generator for BlitzDB already based on Drizzle/MySQL&#8217;s internal KEY_PART_INFO array. It seems to be working on EXACT MATCH but I still need to implement an index scanner which looks much harder to pull off than a table scanner. What I&#8217;m really worried about is supporting composite indexes (namely reading/searching on it) but hopefully I&#8217;ll understand how this area of the storage system works soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2010/01/notes-on-heap-myisam-key-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notes on changes made to the Drizzle Storage Subsystem</title>
		<link>http://torum.net/2009/07/changes-to-the-drizzle-storage-subsystem/</link>
		<comments>http://torum.net/2009/07/changes-to-the-drizzle-storage-subsystem/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 09:26:43 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2274</guid>
		<description><![CDATA[Yesterday I merged the BlitzDB tree with Drizzle&#8216;s trunk for the first time in a long time (yeah&#8230;) and discovered some interesting changes made to the storage subsystem while I was away. Previously all functions that caused an action to the storage engine was a member of the handler class but various things like table [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I merged the<a href="https://code.launchpad.net/~tmaesaka/blitzdb/trunk"> BlitzDB tree</a> with <a href="https://launchpad.net/drizzle">Drizzle</a>&#8216;s trunk for the first time in a long time (yeah&#8230;) and discovered some interesting changes made to the storage subsystem while I was away.</p>
<p>Previously all functions that caused an action to the storage engine was a member of the handler class but various things like table creation and transaction related functions have now moved to the StorageEngine class. These changes are somewhat drastic but makes good sense for Drizzle to grow further since it makes the subsystem easier to understand and frees Drizzle from the interface design that was strongly affected by MyISAM. For those that are interested, the StorageEngine class is located in &#8220;drizzled/plugin/storage_engine.h&#8221;. </p>
<p>For me it was pretty easy to update BlitzDB to work with the new subsystem since I don&#8217;t have anything special in the engine that required me to use my brain. I only had to move <a href="http://forge.mysql.com/wiki/MySQL_Internals_Custom_Engine#bas_ext">bas_ext()</a>, table creation and rename functions over to the StorageEngine class and adjust it to the new interface:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> createTableImpl<span style="color: #009900;">&#40;</span>Session <span style="color: #339933;">*</span>session<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>table_name<span style="color: #339933;">,</span> 
                    Table <span style="color: #339933;">*</span>table_arg<span style="color: #339933;">,</span> HA_CREATE_INFO <span style="color: #339933;">*</span>ha_create_info<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #993333;">int</span> renameTableImpl<span style="color: #009900;">&#40;</span>Session <span style="color: #339933;">*</span>session<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>to<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For a real example, I recommend comparing the old InnobaseEngine class declaration with the updated one. As for where this redesign is going, this is the answer I got on the Drizzle channel from <a href="http://www.flamingspork.com/blog/">Stewart</a> who did the actual work for all this.</p>
<blockquote><p>stewart: tmaesaka: the basic idea is that handler becomes a cursor. the StorageEngine is for actions on the engine.<br />
stewart: tmaesaka: and handler is a cursor on a table.</p></blockquote>
<p>Something to keep in mind if you&#8217;re thinking about creating or porting a storage engine to Drizzle :)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/07/changes-to-the-drizzle-storage-subsystem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing skyload: a libdrizzle based load emulator</title>
		<link>http://torum.net/2009/07/introducing-skyload/</link>
		<comments>http://torum.net/2009/07/introducing-skyload/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 02:38:00 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[skyload]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2272</guid>
		<description><![CDATA[Today, I would like to introduce &#8220;skyload&#8220;, a small project that I&#8217;ve been working on for the last couple of weeks. In brief, skyload is a libdrizzle based load emulation tool that is capable of running concurrent load tests against database instances that can speak Drizzle (and/or) the MySQL protocol. http://code.google.com/p/skyload/ Something I&#8217;d like to [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I would like to introduce &#8220;<strong>skyload</strong>&#8220;, a small project that I&#8217;ve been working on for the last couple of weeks. In brief, skyload is a <a href="https://launchpad.net/libdrizzle">libdrizzle</a> based load emulation tool that is capable of running concurrent load tests against database instances that can speak <a href="https://launchpad.net/drizzle">Drizzle</a> (and/or) the <a href="http://www.mysql.com">MySQL</a> protocol.</p>
<ul>
<li><a href="http://code.google.com/p/skyload/">http://code.google.com/p/skyload/</a></li>
</ul>
<p>Something I&#8217;d like to emphasize here is that, skyload is not a replacement for <a href="http://dev.mysql.com/doc/refman/5.4/en/mysqlslap.html">mysqlslap</a> or drizzleslap since it only provides a subset of what they can do. As I&#8217;ve stated on the project description, skyload is designed to do a good job at this subset of tasks by giving you more control over how you emulate the load in an intuitive way. For instructions on installing skyload and quickly getting up to speed, take a look at the following URL:</p>
<ul>
<li><a href="http://code.google.com/p/skyload/wiki/Tutorial_en">http://code.google.com/p/skyload/wiki/Tutorial_en</a></li>
</ul>
<p>As you will see, the first release only contains bare minimum specifications (only INSERT load emulation). The next step I want to take is to discuss features that other storage engine developers would actually find useful. This is because I started writing skyload for primarily myself and other storage engine developers (more on this next).</p>
<h3>Original Intentions</h3>
<p>I originally began writing skyload for BlitzDB development since I wanted to see the concurrent insertion performance of <a href="http://tokyocabinet.sourceforge.net">Tokyo Cabinet</a> based row storage mechanism that I wrote. I first tried benchmarking the write performance with drizzleslap but it turned out that drizzleslap&#8217;s original code (inherited from MySQL 6.0) is rather buggy and segfaulted quite easily (I&#8217;m planning on contributing a fix for this).</p>
<p>So I gave up on drizzleslap for the time being and started looking at the <a href="http://sysbench.sourceforge.net/">sysbench</a> port for Drizzle that Monty Taylor has been working on:</p>
<ul>
<li><a href="http://mysql-ha.com/post/62">sysbench: now with Drizzle</a></li>
</ul>
<p>Sysbench for Drizzle is a lovely piece of software but it couldn&#8217;t quite provide what I was looking for (concurrent insertion benchmark). After having a quick conversation with Monty about my requirements on the Drizzle IRC channel, I decided to write a libdrizzle based benchmark tool that can be used for both Drizzle and MySQL.</p>
<h3>Future Plans</h3>
<p>I don&#8217;t want to reinvent existing software that works (or those that can be fixed). The project positioning that I&#8217;m hoping for skyload is a good mix between (mysql|drizzle)slap and sysbench. Hopefully it will be useful to folks that works on Drizzle and MySQL related projects.</p>
<p>I&#8217;m totally open for ideas, patches, and contributors. If this project had caught your attention, please don&#8217;t hesitate to ping me or the Drizzle community :)</p>
<p>I haven&#8217;t setup a mailing list since I don&#8217;t see the need for it yet so if you&#8217;d like to share your thoughts I think either the <a href="https://launchpad.net/~drizzle-discuss">Drizzle mailing list</a> or IRC (#drizzle @ irc.freenode.net) is the quickest way for me to get back to you.</p>
<p>Happy Hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/07/introducing-skyload/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Storage Engine Dev Journal #3 : Supporting variable width tables</title>
		<link>http://torum.net/2009/06/supporting-variable-width-tables/</link>
		<comments>http://torum.net/2009/06/supporting-variable-width-tables/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 12:14:52 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2165</guid>
		<description><![CDATA[Something I&#8217;ve added to BlitzDB recently that was pretty high on my todo list is support for variable width tables. So what is a variable width table? it is a table that contains columns that can vary in size, namely BLOB and TEXT types. Going back to the basics, when a new row is to [...]]]></description>
			<content:encoded><![CDATA[<p>Something I&#8217;ve added to BlitzDB recently that was pretty high on my todo list is support for variable width tables. So what is a variable width table? it is a table that contains columns that can vary in size, namely <a href="http://dev.mysql.com/doc/refman/5.4/en/blob.html">BLOB and TEXT</a> types.</p>
<p>Going back to the basics, when a new row is to be written, a storage engine is given a pointer to the row data in MySQL format that it must somehow store for later lookup/retrieval. By meaning &#8220;somehow&#8221;, the storage engine is given the freedom to do whatever it likes with the row.</p>
<p>Writing a row for a fixed length table (a table with columns that are always the same size) is deadly easy. A storage engine can choose to not temper with the row and simply write or copy the data to it&#8217;s storage mechanism. This is because the storage engine is given a row that contains all the data. Rows for variable width tables however, are treated differently since things aren&#8217;t as simple (it&#8217;s variable!).</p>
<p>The difference is that columns for BLOB and TEXT types are represented by two parts inside a MySQL/Drizzle row:</p>
<ul>
<li>length of the data</li>
<li>pointer to the actual data</li>
</ul>
<p>This is simple to understand since we need to know the size of the data to copy it.</p>
<h4>Minor Complication</h4>
<p>The minor complication as you would expect here is that you can&#8217;t directly write the provided row to your engine like you can with fixed length tables. The data that you want to copy/write exists elsewhere (hence the pointer) so directly writing the row has no meaning (the data would have disappeared by your next access to that row). You need to make sure that the actual data for BLOB/TEXT column(s) are arranged appropriately on your engine&#8217;s row buffer and written out to it&#8217;s storage mechanism.</p>
<p>This process is commonly referred to as row packing (converting to your engine format) and unpacking (convert back to MySQL format). So how is this done? it&#8217;s actually pretty simple!</p>
<h4>The solution is actually simple</h4>
<p>As much as it sounds like a bother to support variable length rows, it&#8217;s actually not that bad. First you need to understand what a MySQL row looks like internally.</p>
<p>A MySQL row begins with a bitset that represents which fields are NULL. The length of this data obviously depends on the number of NULLable columns you have but this is easy to handle with Drizzle since we&#8217;re given all the relevant information by the TableShare object (same goes for MySQL from a different object).</p>
<p>After this data comes the actual column data in the order that appears in your CREATE TABLE statement. What you need to do to get packing working with this row is the not-so-obvious part that you really need an example to look at. Fortunately Tweeting about this attracted <a href="http://twitter.com/brianaker/status/2026228307">Brian&#8217;s attention</a> which helped me move forward.</p>
<h4>Loop the fields!</h4>
<p>So, let&#8217;s take row insertion to a variable width table as an example. Imagine this table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #66cc66;">&#40;</span>
  id int <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  description text<span style="color: #66cc66;">,</span>
  arbitrary_data blob
<span style="color: #66cc66;">&#41;</span> engine<span style="color: #66cc66;">=</span>your_engine;</pre></div></div>

<p>and let&#8217;s imagine that we need to process this query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;blobbbbb&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, the storage engine needs to &#8220;pack&#8221; the data for each column into it&#8217;s buffer in the <a href="http://forge.mysql.com/wiki/MySQL_Internals_Custom_Engine#Adding_Support_for_INSERT_to_a_Storage_Engine">write_row()</a> function. Conveniently, Drizzle/MySQL provides a pack() function for it&#8217;s column types (fields) that will do the data packing for you. That is, you do not have to inspect the provided row for pointers to the actual data and do the packing/copying yourself. </p>
<p>How? well, the table object (which is visible from your engine) conveniently holds a list of fields in the appropriate order. The actual pack() function is a member of these fields so you just need to call it as you loop over the list:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* make sure row_buffer has enough memory */</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>pos <span style="color: #339933;">=</span> row_buffer<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* copy NULL bits, &quot;table-&gt;s&quot; is the TableShare object */</span>
memcpy<span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">,</span> row<span style="color: #339933;">,</span> table<span style="color: #339933;">-&gt;</span>s<span style="color: #339933;">-&gt;</span>null_bytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
pos <span style="color: #339933;">+=</span> table<span style="color: #339933;">-&gt;</span>s<span style="color: #339933;">-&gt;</span>null_bytes<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* &quot;row&quot; is the MySQL formatted row given by the core */</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>Field <span style="color: #339933;">**</span>field <span style="color: #339933;">=</span> table<span style="color: #339933;">-&gt;</span>field<span style="color: #339933;">;</span> <span style="color: #339933;">*</span>field<span style="color: #339933;">;</span> field<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>is_null<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    pos <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>pack<span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">,</span> row <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>offset<span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code snippet will populate &#8220;row_buffer&#8221; with the actual data that you want to write to your storage mechanism. You do not have to forward the &#8220;pos&#8221; pointer because pack() returns a pointer at the end of where it had worked in the buffer (think Pascal Strings). This is precisely why we created the pos pointer, to avoid row_buffer from being forwarded.</p>
<p>For the opposite situation (when retrieving a row), an unpack() function is provided for each field so you just need to take advantage of it like we did with the pack() snippet above.</p>
<h4>Little bit more on fields</h4>
<p>The actual pack() function that gets called depends on the type of column since the Field class is an abstract base class for the sub classes that actually represents column types inside Drizzle/MySQL. If you want to know what a pack() function looks like for a BLOB type, grep for &#8220;Field_blob&#8221; in the source tree and there will be a pack() member function for it.</p>
<p>The code layout for field subsystem in MySQL is rather difficult to comprehend since everything is crammed in &#8220;sql/field.c&#8221; and &#8220;sql/field.h&#8221; files (at least as of 5.4). So, if you want to get a good grasp of how things are architectured, you should take a look at Drizzle. Field subclasses are located individually in the &#8220;drizzled/field/&#8221; directory and the base class is located in &#8220;drizzled/field.h&#8221;.</p>
<p>So, that&#8217;s about it! Hopefully this information will help other engine developers when they come across a need to support variable width tables :)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/06/supporting-variable-width-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Journal of Storage Engine Development on Drizzle</title>
		<link>http://torum.net/2009/05/storage-engine-development-on-drizzle/</link>
		<comments>http://torum.net/2009/05/storage-engine-development-on-drizzle/#comments</comments>
		<pubDate>Tue, 12 May 2009 08:04:01 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1581</guid>
		<description><![CDATA[I&#8217;ve decided to start a series of blog entries on not-so-obvious findings that I&#8217;ve found while working on my new project. By archiving the findings, I&#8217;m hoping that I can help those that are looking into developing a storage engine for the MySQL family in the future. Accumulating these mini-knowledge would also be useful for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to start a series of blog entries on not-so-obvious findings that I&#8217;ve found while working on my <a href="https://launchpad.net/blitzdb">new project</a>. By archiving the findings, I&#8217;m hoping that I can help those that are looking into developing a storage engine for the MySQL family in the future.</p>
<p>Accumulating these mini-knowledge would also be useful for me since I can refer back to it when I forget something. Also, once I write enough entries I&#8217;m planning on summarizing them and making it available on the <a href="http://drizzle.org/wiki/">Drizzle Wiki</a>. If <a href="http://www.mysql.com">MySQL</a> is interested in updating the engine documentation, I would be more than happy to help there too.</p>
<p>So to begin with, I&#8217;ll describe something trivial that I stumbled across while trying to catch an error on duplicate primary key insertion to the data table.</p>
<h4>Background</h4>
<p>In brief, the database kernel does not care if the INSERT query contains a duplicate primary key for a given table or not. It is the storage engine&#8217;s job to tell the kernel that the request was invalid due to key collision. If a storage engine fails to do this, the kernel will acknowledge that the query was successful (given that no other errors were thrown) and will keep doing what it needs to do.</p>
<h4>Mechanics</h4>
<p>Data insertion is handled inside the <a href="http://forge.mysql.com/wiki/MySQL_Internals_Custom_Engine#Adding_Support_for_INSERT_to_a_Storage_Engine">write_row()</a> function that your engine must implement. The return value of this function is an integer that represents the status of the work it had done. After looking through the possible error statuses in &#8220;drizzled/base.h&#8221;, I immediately found this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define HA_ERR_FOUND_DUPP_KEY 121 /* Dupplicate key on write */</span></pre></div></div>

<p>I also looked through <a href="http://en.wikipedia.org/wiki/MyISAM">MyISAM</a> and <a href="http://en.wikipedia.org/wiki/Innodb">InnoDB</a> to confirm that this was indeed the correct error status to return on duplicate primary key. Here is the snippet of my row insertion <strong>at the time</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* TC's tchdbputkeep will not insert a row to the table if there
   was a collision */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>tchdbputkeep<span style="color: #009900;">&#40;</span>data_table<span style="color: #339933;">,</span> primary_key<span style="color: #339933;">,</span> primary_key_length<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span>
                 table<span style="color: #339933;">-&gt;</span>s<span style="color: #339933;">-&gt;</span>reclength<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  my_errno <span style="color: #339933;">=</span> HA_ERR_GENERIC<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* check for primary key collision */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>tchdbecode<span style="color: #009900;">&#40;</span>data_table<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> TCEKEEP<span style="color: #009900;">&#41;</span>
    my_errno <span style="color: #339933;">=</span> HA_ERR_FOUND_DUPP_KEY<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> my_errno<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>On first glimpse, this seems right but the error I was getting from the command line prompt always differed with MyISAM and InnoDB despite returning the same error status. Specifically, this is what I was getting:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">ERROR 1022 (23000): Can't write; duplicate key in table 't1'</pre></div></div>

<p>whereas I was getting this error on other engines:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'</pre></div></div>

<p>At this stage I couldn&#8217;t make sense of what I was doing wrong but it turned out that the solution was pretty simple.</p>
<h4>Solution</h4>
<p>After talking to <a href="http://www.flamingspork.com/blog/">Stewart Smith</a> about my issue in #drizzle @ freenode, it turned out I am supposed to keep track of which key the duplication was found in write_row() and inform it to the kernel via the <a href="http://forge.mysql.com/wiki/MySQL_Internals_Custom_Engine#Implementing_the_info.28.29_Method">info()</a> function.</p>
<p>You can do this by setting the <em>errkey</em> integer variable to the key number that is used internally by the kernel. So, obtaining the internal primary key number with this call in write_row():</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">share<span style="color: #339933;">-&gt;</span>errkey <span style="color: #339933;">=</span> table<span style="color: #339933;">-&gt;</span>s<span style="color: #339933;">-&gt;</span>primary_key<span style="color: #339933;">;</span></pre></div></div>

<p>and adding the following code to info():</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>flag <span style="color: #339933;">&amp;</span> HA_STATUS_ERRKEY<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  errkey <span style="color: #339933;">=</span> share<span style="color: #339933;">-&gt;</span>errkey<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>happily fixed the issue I was experiencing. Yay.</p>
<p>I guess reading the section on info() in the document gives a hint that this is where you supply the key number on key-error but frankly, this is really easy to forget and miss since the importance isn&#8217;t so emphasized.</p>
<p>Anyhow, thats all I have to say in the first of this series and hopefully I&#8217;ll write something more interesting in the upcoming entries. Until then, happy hacking ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/05/storage-engine-development-on-drizzle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun times at MySQL UC and Drizzle Developer Day 09</title>
		<link>http://torum.net/2009/04/mysql-uc-and-drizzle-dev-day/</link>
		<comments>http://torum.net/2009/04/mysql-uc-and-drizzle-dev-day/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 09:10:56 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[travel]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1339</guid>
		<description><![CDATA[I&#8217;m writing this entry on my way back to Tokyo from Narita. So, I was in the US all week for MySQL UC, Percona Performance Conference and the Drizzle Developer Day. It was great to meet new people and also catch up with developer friends from all over the world. These events are great excuse [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing this entry on my way back to Tokyo from Narita. So, I was in the US all week for <a href="http://mysqlconf.com">MySQL UC</a>, <a href="http://conferences.percona.com/">Percona Performance Conference</a> and the <a href="http://drizzle.org/wiki/Drizzle_Developer_Day_2009">Drizzle Developer Day</a>. It was great to meet new people and also catch up with developer friends from all over the world. These events are great excuse to bring together folks that work together online and receive the free beers that were promised on IRC. Looking back, the week just flew! I can&#8217;t believe I&#8217;m back in Japan already.</p>
<p>What wasn&#8217;t pleasant however was Drizzle being introduced as &#8220;MySQL Drizzle&#8221; and described as MySQL&#8217;s technology incubator at the opening keynote. The truth is, Drizzle is a community driven project that is not affiliated with any commercial organization. The project is shepherded by the community and that is the whole point of our model. Jay and Baron has expressed this on their blogs too:</p>
<ul>
<li><a href="http://www.jpipes.com/index.php?/archives/294-Sorry,-but-Drizzle-is-Not....html">Sorry, but Drizzle is Not&#8230;</a></li>
<li><a href="http://www.xaprb.com/blog/2009/04/21/drizzle-is-a-mysql-technology-incubator/">Drizzle is a MySQL Technology Incubator?</a></li>
</ul>
<p>Guess there is no point in getting worked up about this so I will say no more.</p>
<p>Drizzle developer day turned out to be a great success with over 60 developers (including new developers) turning up. We covered wide variety of topics from &#8220;how to download bzr&#8221; for all levels of contributors which I thought was nice. Why? well this means that everyone has something to do and it creates a welcoming atmosphere. As a result, it makes the event active. Here are some photos <a href="http://www.flickr.com/photos/tmaesaka/sets/72157617243040525/">I uploaded to flickr</a> in the last few minutes I had in the US. I&#8217;ll get more up there asap.</p>
<p>Compared to these stimulating events and the discussions we had, Oracle&#8217;s acquisition tale that many people seemed to like talking about at the conference breakfast/lunch/boozing was uninteresting.</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/mysql-uc-and-drizzle-dev-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on tackling InnoDB&#8217;s auto increment</title>
		<link>http://torum.net/2009/04/tackling-innodb-auto-increment/</link>
		<comments>http://torum.net/2009/04/tackling-innodb-auto-increment/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 10:28:11 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1237</guid>
		<description><![CDATA[As I mentioned in my previous entry, InnoDB has it&#8217;s own auto increment counter which it uses to generate the next value for the database kernel (as we call it in Drizzle). At Drizzle project, we came across a suspicion that InnoDB doesn&#8217;t increment it&#8217;s internal counter on row updates. So what can this mean [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my <a href="http://torum.net/2009/04/understanding-autoinc-with-innodb/">previous entry</a>, <a href="http://www.innodb.com/innodb/">InnoDB</a> has it&#8217;s own auto increment counter which it uses to generate the next value for the database kernel (as we call it in <a href="https://launchpad.net/drizzle">Drizzle</a>). At Drizzle project, we came across a <a href="https://bugs.launchpad.net/drizzle/+bug/314570">suspicion</a> that InnoDB doesn&#8217;t increment it&#8217;s internal counter on row updates. So what can this mean to you as a database admin?</p>
<p>Well, consider this simple table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #66cc66;">&#40;</span>
    a INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
    val INT
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB;</pre></div></div>

<p>and the following statements:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> t1 <span style="color: #993333; font-weight: bold;">SET</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We should now have a table with one row where the <a href="http://en.wikipedia.org/wiki/Primary_key">primary key</a> value is 4:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>So far so good! Now remember our hypothesis that InnoDB doesn&#8217;t temper with the internal counter on an update. This would mean that our next row should have a primary key value of 2 since the counter variable should still be at 1 from our initial INSERT statement:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">2</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> LAST_INSERT_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span> LAST_INSERT_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span>                <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As expected, we have a new row with a primary key value of 2. But hey, what happens if we keep inserting into this table? specifically twice (assuming that you haven&#8217;t changed the auto-increment-increment system variable).</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.01</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
ERROR <span style="color: #cc66cc;">1062</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23000</span><span style="color: #66cc66;">&#41;</span>: Duplicate entry <span style="color: #ff0000;">'4'</span> <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">'PRIMARY'</span></pre></div></div>

<p>Yep, a duplicate key error! This isn&#8217;t really surprising since the increment was resumed from a value less than what we had updated the first value to. I&#8217;m not even sure if this sort of operation would be done in production but it shows how this behavior can be dangerous.</p>
<p>Something to note here is that, even on the INSERT error that we just experienced, InnoDB will update the internal count because get_auto_increment() was called when processing the query. So, this means that the next INSERT query will work fine since we&#8217;ve now passed the maximum value:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.02</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">4</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Great! now we can hopefully keep incrementing happily.</p>
<p>I&#8217;ve confirmed the internal behavior of this example with <a href="http://www.gnu.org/software/gdb/">GDB</a> and also verified the same behavior in MySQL 5.1.30. Whether this behavior is inappropriate or not deserves a discussion of it&#8217;s own and you could of course, absorb this problem in the application layer but the important question here is, what is the right behavior for Drizzle?</p>
<p>Well, take a look at how <a href="http://en.wikipedia.org/wiki/MyISAM">MyISAM</a> handles the same set of queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t2 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> t2 <span style="color: #993333; font-weight: bold;">SET</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
Rows matched: <span style="color: #cc66cc;">1</span>  Changed: <span style="color: #cc66cc;">1</span>  Warnings: <span style="color: #cc66cc;">0</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t2 <span style="color: #66cc66;">&#40;</span>val<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> row affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t2;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> a <span style="color: #66cc66;">|</span> val  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---+------+</span>
<span style="color: #cc66cc;">2</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
drizzle<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> last_insert_id<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span> last_insert_id<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #66cc66;">|</span>                <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As you can see above, MyISAM will resume incrementing from the updated primary key value. To me this is the correct behavior that should be consistently provided to DBAs. So the next question is, how can we make InnoDB behave that way?</p>
<h3>Ideas on tackling this issue</h3>
<p>A simple solution would be to make InnoDB update the internal count on updates (e.g. ha_innobase::update_row) when it deals with with auto increment. This approach, however can end up being complicated (code wise) and worst of all, it can create a minor overhead to the update operation on InnoDB which may not be favorable for our target users.</p>
<p>So after briefly discussing this issue with <a href="http://mysql-ha.com/">Monty Taylor</a>, seems the appropriate fix for this is to leave ha_innobase::update_row() as it is (don&#8217;t increment the internal count) and handle this on INSERT. Specifically speaking, on a row that doesn&#8217;t have a user specified auto increment value.</p>
<p>This means that we can recalculate the auto increment value on duplication/collision since the user is expecting an auto-generated discrete value. Needless to say, an error <strong>must</strong> be thrown if a collision occurs on a user-specified value.</p>
<p>So yeah, I&#8217;m flying to the US in couple of days so this should be something fun to hack on amongst other work related tasks on my boring flight :)</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/tackling-innodb-auto-increment/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Understanding how auto increment works with InnoDB</title>
		<link>http://torum.net/2009/04/understanding-autoinc-with-innodb/</link>
		<comments>http://torum.net/2009/04/understanding-autoinc-with-innodb/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 11:22:39 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://torum.net/?p=1090</guid>
		<description><![CDATA[Lately I&#8217;ve been having lots of fun going through Drizzle and InnoDB&#8216;s sourcecode to get a grasp of how auto increment is processed internally. I think I now have a fairly good grasp of what&#8217;s going on so I&#8217;m writing this entry as a note for myself. I&#8217;m also hoping that this will be helpful [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been having lots of fun going through <a href="https://launchpad.net/drizzle">Drizzle</a> and <a href="http://www.innodb.com/innodb/">InnoDB</a>&#8216;s sourcecode to get a grasp of how auto increment is processed internally. I think I now have a fairly good grasp of what&#8217;s going on so I&#8217;m writing this entry as a note for myself. I&#8217;m also hoping that this will be helpful to those that are interested in this topic too.</p>
<p>So in MySQL and Drizzle, the storage engine (in this case InnoDB) is responsible for computing the auto increment value. Here&#8217;s an <strong>abbreviated</strong> execution path for a simple INSERT statement to a table with an auto increment column:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">mysql_parse() -&gt; mysql_execute_command() -&gt; mysql_insert() -&gt;
write_record() -&gt; handler::ha_write_row() -&gt; ha_innobase::write_row() -&gt;
handler::update_auto_increment() -&gt; ha_innobase::get_auto_increment()</pre></div></div>

<p>As you may have noticed, &#8220;handler&#8221; is an <a href="http://en.wikipedia.org/wiki/Abstract_class">abstract class</a> so you can apply this execution path to other engines too. For example, if you look inside <a href="http://en.wikipedia.org/wiki/MyISAM">MyISAM</a> there is a <em>get_auto_increment()</em> in there too. </p>
<h3>InnoDB&#8217;s internal counter</h3>
<p>InnoDB keeps track of it&#8217;s own auto increment count (an unsigned 64bit integer) called &#8220;autoinc&#8221; inside a structure that represents a database table (dict_table_struct). As you would expect, this is what InnoDB uses to compute the auto increment value that the server will use for further processing.</p>
<h3>Diving in a little deeper</h3>
<p>As you can see in the execution path, <em>handler::update_auto_increment()</em> function asks InnoDB&#8217;s <em>get_auto_increment()</em> function for an incremented value. Looking slightly deeper, you will see that this function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">innobase_get_autoinc<span style="color: #009900;">&#40;</span>uint64_t <span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>is called inside <em>ha_innobase::get_auto_increment()</em> which will attempt to obtain the autoinc lock for that table (look at <em>ha_innobase::innobase_lock_autoinc()</em>), then given that the lock was successfully obtained, <em>innobase_get_autoinc()</em> will set the provided uint64_t variable with the next auto increment value:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #339933;">*</span>value <span style="color: #339933;">=</span> dict_table_autoinc_read<span style="color: #009900;">&#40;</span>prebuilt<span style="color: #339933;">-&gt;</span>table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you look inside <em>dict_table_autoinc_read()</em> you will notice that it returns <em>table->autoinc</em> which is InnoDB&#8217;s internal auto increment counter described in the previous section. We don&#8217;t increment table->autoinc before returning it since this value is going to be updated after it is returned. In other words, the next auto increment value is precomputed. Needless to say, this critical region is still protected by the autoinc mutex so you don&#8217;t have to worry about updated values to clash.</p>
<h3>After obtaining the value from InnoDB</h3>
<p>Once we return from <em>ha_innobase::get_auto_increment()</em>, the core server can now store the returned 64bit integer as binary to the next_number_field object for further processing.</p>
<p>If you wish to see the value of this data, you can print it as a primitive type (uint64_t in this case) by obtaining it with the val_int() function. So if you were <a href="http://drizzle.org/wiki/Debugging_Drizzle_with_GDB">going through the code with gdb</a>, performing this at an appropriate time (e.g. after the autoinc value is stored) will print the value that will be used to update the auto increment column of your table:</p>

<div class="wp_syntax"><div class="code"><pre class="gdb" style="font-family:monospace;">(gdb) print table-&gt;next_number_field-&gt;val_int()</pre></div></div>

<p>Finally at the end of <em>handler::update_auto_increment()</em>, the next auto increment value is computed so that the storage engine won&#8217;t have to do any work in case the request is a multi row statement (note that this is different to InnoDB&#8217;s autoinc precomputation). We then return back to <em>ha_innobase::write_row()</em>, where the row is actually inserted into the table with <em>row_insert_for_mysql()</em>.</p>
<p>The rest of the flow is what you would expect: log what just happened, cleanup the mess caused in generating the autoinc value, and respond to the client.</p>
<h3>Final words</h3>
<p>So, this is all I have to cover in this entry. Admittedly my explanation is really abbreviated and perhaps a little over simplified but I figured this is enough to get back on the sourcecode after I forget my findings from the past couple of days in the future (my memory sucks).</p>
<p>I haven&#8217;t covered the auto increment internals for UPDATE statements but covering this would make this entry longer than I want it to be so I will save it for another day. Nevertheless, this entry will hopefully help curious individuals like myself to stare at Drizzle and InnoDB&#8217;s source code and enjoy an hour or two there.</p>
<p>Oh yea, and as a warning, do note that this entry could be outdated by the time you read it!</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/understanding-autoinc-with-innodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Saying farewell to drizzleadmin</title>
		<link>http://torum.net/2009/04/saying-farewell-to-drizzleadmin/</link>
		<comments>http://torum.net/2009/04/saying-farewell-to-drizzleadmin/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 09:03:35 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[libdrizzle]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://torum.net/?p=969</guid>
		<description><![CDATA[For the last couple of days I&#8217;ve been working on removing drizzleadmin (formerly mysqladmin) from Drizzle&#8216;s source tree and I&#8217;m happy to announce that the code is now merged to the trunk. So why did we decide to throw out a program that&#8217;s been around for a long time? Well, the tool wasn&#8217;t really useful [...]]]></description>
			<content:encoded><![CDATA[<p>For the last couple of days I&#8217;ve been working on removing drizzleadmin (formerly <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqladmin.html">mysqladmin</a>) from <a href="https://launchpad.net/drizzle">Drizzle</a>&#8216;s source tree and I&#8217;m happy to announce that the code is now merged to the trunk.</p>
<p>So why did we decide to throw out a program that&#8217;s been around for a long time? Well, the tool wasn&#8217;t really useful to drizzle since mysqladmin is designed for MySQL and we wanted to factor out these old command line tools in the tree (explained later). With that in mind, I&#8217;ve been gradually removing code from drizzleadmin and by the end of it&#8217;s lifetime, only two commands remained: &#8220;shutdown&#8221; and &#8220;ping&#8221;. These commands are now moved to the drizzle command line tool.</p>
<p>Here&#8217;s the new standard way to shutdown drizzled:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">$ drizzle --shutdown --verbose
shutting down drizzled... done</pre></div></div>

<p>Shutting down drizzled that&#8217;s listening to a certain port:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">$ drizzle --shutdown --port=12321 --verbose 
shutting down drizzled on port 12321... done</pre></div></div>

<p>Check whether drizzled is still alive on a certain port:</p>

<div class="wp_syntax"><div class="code"><pre class="null" style="font-family:monospace;">$ drizzle --ping --port=9306
drizzled is alive</pre></div></div>

<p>As you can see above, it makes better sense for the user to use the standard CLI tool to perform such simple tasks. For anything more sophisticated, we&#8217;re encouraging new open source projects to arise. Now with <a href="https://launchpad.net/libdrizzle">libdrizzle</a> out in the open, <strong>anyone</strong> (yes, including you) can easily write a client program for Drizzle. You could even write one in your favorite programming language once the binding for that language comes out.</p>
<p>If you have any cool ideas for a project, please do share it with us on the <a href="https://launchpad.net/~drizzle-discuss">mailing list</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/04/saying-farewell-to-drizzleadmin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
