File: Derby3625Test.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (269 lines) | stat: -rw-r--r-- 10,723 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.apache.derbyTesting.functionTests.tests.store;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Assert;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;


/*
Class org.apache.derbyTesting.functionTests.tests.jdbc4.Derby3650Test

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/


/**

Test to reproduce DERBY-3625, failure in inline compress, in some 
circumstances depending on exact size of data and state of pages during
the defragment phase.  

Would throw following error:

ERROR XSDA3: Limitation: Record cannot be updated or inserted due to lack of 
space on the page. Use the parameters derby.storage.pageSize and/or 
derby.storage.pageReservedSpace to work around this limitation.^M

**/

public class Derby3625Test extends StoreBaseTest
{
    /**************************************************************************
     * Fields of the class
     **************************************************************************
     */

    /**************************************************************************
     * Constructors for This class:
     **************************************************************************
     */

    /**************************************************************************
     * Private/Protected methods of This class:
     **************************************************************************
     */

    /**************************************************************************
     * Public Methods of This class:
     **************************************************************************
     */

    /**************************************************************************
     * Public Methods of XXXX class:
     **************************************************************************
     */

    public Derby3625Test(String name) 
    {
        super(name);
    }
    
    /**
     * DERBY-3625 test case
     * <p>
     * Derby 3625 is caused by a bug where compress calculates the space
     * needed to fit a moved row from page A to B, and assumes that the space
     * required on page B is the same on page A.  The problem is that in 
     * some cases due to the stored format of the changing record id the space
     * required on B may be more than A.  In the case where there is exactly
     * enough space by the initial calculation the move fails because one or
     * 3 more bytes may be necessary to make the move and the compress fails.
     * <p>
     * To test:
     *   fill page 1 with dummy rows, page 1 has a special control row on it
     *       so it can't ever be empty so use page 2 instead.
     *   fill page 2 with dummy rows such and empty it such that the 
     *       next row id on it is greater that 64 which takes 2 bytes to store 
     *       vs. 1 for rowid's less * that 64.
     *   fill page 3 and 4 with some dummy rows which will be deleted to give
     *       compress table room to work during defragment.
     *   fill page 4 with 2 rows which fit on page 2 with 1 byte stored record
     *        id's but will not fit with 2 byte stored record id's.  
     *        These will not be deleted and the bug is exercised as 
     *        defragment tries to move these rows to page 2 after it has 
     *        been reclaimed as a free page.
     **/
    public void testTwoToOneByteCase()
        throws SQLException
    {
        PreparedStatement insert_stmt = 
            prepareStatement("INSERT INTO testCompress VALUES(?, ?, ?)");

        // page 0 - container info/bit map, does not affect test

        // page 1 - fill it up and leave rows on it.  page 1 has a special
        // row on it that can never be deleted so this page never can be
        // made free.

        // insert one blob padded row that will fill page 1
        byte[] pad_blob = new byte[32630];
        insert_stmt.setInt(     1, 1);
        insert_stmt.setBytes(   2, pad_blob);
        insert_stmt.setString(  3, "page 1");
        insert_stmt.executeUpdate();

        // page 2 - fill it completely with enough rows such that future
        // rows will force a 2 byte row id, ie. more than 64 rows.  Later
        // in this test all the rows will be deleted from this page so that
        // the page is on the free list for compress defragment to use it.

        pad_blob = new byte[302];
        insert_stmt.setInt(     1, 2);
        insert_stmt.setBytes(   2, pad_blob);
        insert_stmt.setString(  3, "page 2");
        for (int i = 0; i < 98; i++)
        {
            insert_stmt.executeUpdate();
        }

        // page 3 - fill it for another free page.
        insert_stmt.setInt(     1, 3);
        insert_stmt.setBytes(   2, pad_blob);
        insert_stmt.setString(  3, "page 3");
        for (int i = 0; i < 98; i++)
        {
            insert_stmt.executeUpdate();
        }

        // page 4 -  2 rows, with one byte free.  When these are moved to 
        // a free page with bigger rowid's they will take 2 more bytes and
        // will not both fit on the page.
        //
        // I didn't track it down, but for some reason I could not fill a page
        // completely if there was only one row on the page, it kept turning
        // the blob column into a long row.  I was just picking magic numbers
        // for the blob column to make it fit.
        //
        // With 2 rows I was able to fill the page up to one empty byte.  
        // Then with the bug the first row would move to page 2 which is
        // now free but take one more byte than it did on this page.  And
        // finally when the second row was moved it would think it would fit
        // but throw an exception when the rowid compressed version would
        // cause it to be one byte bigger than the original row.
        pad_blob = new byte[100];
        insert_stmt.setInt(     1, 4);
        insert_stmt.setBytes(   2, pad_blob);
        insert_stmt.setString(  3, "page 4");
        insert_stmt.executeUpdate();
        pad_blob = new byte[32534];
        insert_stmt.setInt(     1, 4);
        insert_stmt.setBytes(   2, pad_blob);
        insert_stmt.setString(  3, "page 4");
        insert_stmt.executeUpdate();

        commit();

        int space_info[] = getSpaceInfo("APP", "TESTCOMPRESS", true);

        // space after initial insert setup should be 4 pages
        // 0 - container info - not reflected in allocated page count, 
        // 1 - dummy data left on the page, 
        // 2 - bunch of short records to be deleted to make free page
        // 3 - bunch of short records to be deleted to make free page
        // 4 - short and long record to exercise bug.
        Assert.assertEquals(
            "wrong allocated page count in test setup", 
            4, space_info[SPACE_INFO_NUM_ALLOC]);

        Statement stmt = createStatement();

        // Delete rows on page 2 and 3 to allow defragment to try and move
        // the page 4 row up.
        stmt.executeUpdate("DELETE FROM testCompress where id = 2 or id = 3");
        commit();

        // Before fixing the bug, this compress call would throw the 
        // following exception:
        //
        // ERROR XSDA3: Limitation: Record cannot be updated or inserted due 
        // to lack of space on the page. Use the parameters 
        // derby.storage.pageSize and/or derby.storage.pageReservedSpace to 
        // work around this limitation.

        CallableStatement call_compress = 
            prepareCall(
                "CALL SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE(?, ?, 1, 1, 1)");

        call_compress.setString(1, "APP");
        call_compress.setString(2, "TESTCOMPRESS");
        call_compress.executeUpdate();

        commit();

        space_info = getSpaceInfo("APP", "TESTCOMPRESS", true);

        // space after the test should be 3 pages: 
        // 0 - container info - not reflected in allocated page count, 
        // 1 - dummy data left on the page, 
        // 2 - one short record, but long record did not fit
        // 3 - long record on an empty page.
        Assert.assertEquals(
            "wrong allocated page count", 3, space_info[SPACE_INFO_NUM_ALLOC]);

        insert_stmt.close();
    }
    
    protected static Test baseSuite(String name) 
    {
        BaseTestSuite suite = new BaseTestSuite(name);
        suite.addTestSuite(Derby3625Test.class);
        return new CleanDatabaseTestSetup(
                DatabasePropertyTestSetup.setLockTimeouts(suite, 2, 4)) 
        {
            /**
             * Creates the tables used in the test cases.
             * @exception SQLException if a database error occurs
             */
            protected void decorateSQL(Statement stmt) throws SQLException
            {
                Connection conn = stmt.getConnection();

                CallableStatement set_dbprop =  conn.prepareCall(
                    "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
                set_dbprop.setString(1,"derby.storage.pageReservedSpace");
                set_dbprop.setString(2,"0");
                set_dbprop.executeUpdate();
                
                // create a table, with blob it will be 32k page size
                stmt.executeUpdate(
                    "CREATE TABLE testCompress " +
                        "(id int, padcol blob(1M), c varchar(200))");

                set_dbprop.setString(2, null);
                set_dbprop.executeUpdate();

                set_dbprop.close();

                conn.setAutoCommit(false);
            }
        };
    }

    public static Test suite() 
    {
        BaseTestSuite suite = new BaseTestSuite("Derby3625Test");
        suite.addTest(baseSuite("Derby36625Test:embedded"));
        return suite;
    }
}