~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/Compactor_ms.cc

Added the PBMS daemon plugin.

(Augen zu und durch!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-07-10
 
23
 *
 
24
 * H&G2JCtL
 
25
 *
 
26
 * Network interface.
 
27
 *
 
28
 */
 
29
 
 
30
#include "CSConfig.h"
 
31
#include "CSGlobal.h"
 
32
#include "CSStrUtil.h"
 
33
#include "CSStorage.h"
 
34
 
 
35
#include "Defs_ms.h"
 
36
#include "Compactor_ms.h"
 
37
#include "OpenTable_ms.h"
 
38
#include "Repository_ms.h"
 
39
 
 
40
/*
 
41
 * ---------------------------------------------------------------
 
42
 * COMPACTOR THREAD
 
43
 */
 
44
 
 
45
MSCompactorThread::MSCompactorThread(time_t wait_time, MSDatabase *db):
 
46
CSDaemon(wait_time, NULL),
 
47
iCompactorDatabase(db)
 
48
{
 
49
}
 
50
 
 
51
MSCompactorThread::~MSCompactorThread()
 
52
{
 
53
        close();
 
54
}
 
55
 
 
56
void MSCompactorThread::close()
 
57
{
 
58
}
 
59
 
 
60
bool MSCompactorThread::doWork()
 
61
{
 
62
        bool                            complete;
 
63
        MSRepository            *src_repo, *dst_repo;
 
64
        MSRepoFile                      *src_file, *dst_file;
 
65
        uint32_t                                src_repo_id;
 
66
        MSBlobHeadRec           blob;
 
67
        off64_t                         src_offset;
 
68
        uint16_t                                head_size;
 
69
        uint64_t                                blob_size, blob_data_size;
 
70
        CSStringBuffer          *head;
 
71
        MSRepoPointersRec       ptr;
 
72
        u_int                           table_ref_count;
 
73
        u_int                           blob_ref_count;
 
74
        int                                     ref_count;
 
75
        size_t                          ref_size;
 
76
        CSMutex                         *lock;
 
77
        uint32_t                                tab_id;
 
78
        uint64_t                                blob_id;
 
79
        MSOpenTable                     *otab;
 
80
        uint32_t                                repo_id;
 
81
        uint64_t                                repo_offset;
 
82
        uint64_t                                repo_blob_size;
 
83
        uint16_t                                repo_head_size;
 
84
        uint16_t                                tab_index;
 
85
        uint8_t                         status;
 
86
 
 
87
        enter_();
 
88
        retry:
 
89
        
 
90
#ifdef MS_COMPACTOR_POLLS
 
91
        if (!(src_repo = iCompactorDatabase->getRepoFullOfTrash(NULL)))
 
92
                return_(true);
 
93
#else
 
94
        myWaitTime = MS_DEFAULT_COMPACTOR_WAIT * 1000;  // Time in milli-seconds
 
95
        if (!(src_repo = iCompactorDatabase->getRepoFullOfTrash(&myWaitTime)))
 
96
                return_(true);
 
97
#endif
 
98
        frompool_(src_repo);
 
99
        src_file = src_repo->openRepoFile();
 
100
        push_(src_file);
 
101
 
 
102
        dst_repo = iCompactorDatabase->lockRepo(src_repo->myRepoFileSize - src_repo->myGarbageCount);
 
103
        frompool_(dst_repo);
 
104
        dst_file = dst_repo->openRepoFile();
 
105
        push_(dst_file);
 
106
 
 
107
        new_(head, CSStringBuffer(100));
 
108
        push_(head);
 
109
 
 
110
        complete = false;
 
111
        src_repo_id = src_repo->myRepoID;
 
112
        src_offset = src_repo->myRepoHeadSize;
 
113
        //printf("\nCompacting repo %"PRId32"\n\n", src_repo_id);
 
114
        // For testing:
 
115
        {
 
116
                int blockit = 0;
 
117
                if (blockit) {
 
118
                        release_(head);
 
119
                        release_(dst_file);
 
120
                        backtopool_(dst_repo);
 
121
                        release_(src_file);
 
122
                        backtopool_(src_repo);
 
123
 
 
124
                        myWaitTime = 5 * 1000;  // Time in milli-seconds
 
125
                        return_(true);
 
126
                }
 
127
        }
 
128
        while (src_offset < src_repo->myRepoFileSize) {                 
 
129
                retry_loop:
 
130
                suspended();
 
131
 
 
132
                if (myMustQuit)
 
133
                        goto quit;
 
134
                retry_read:
 
135
                
 
136
                // A lock is required here because references and dereferences to the
 
137
                // BLOBs can result in the repository record being updated while 
 
138
                // it is being copied.
 
139
                lock = &src_repo->myRepoLock[src_offset % CS_REPO_REC_LOCK_COUNT];
 
140
                lock_(lock);
 
141
                if (src_file->read(&blob, src_offset, src_repo->myRepoBlobHeadSize, 0) < src_repo->myRepoBlobHeadSize) {
 
142
                        unlock_(lock);
 
143
                        break;
 
144
                }
 
145
                ref_size = CS_GET_DISK_1(blob.rb_ref_size_1);
 
146
                ref_count = CS_GET_DISK_2(blob.rb_ref_count_2);
 
147
                head_size = CS_GET_DISK_2(blob.rb_head_size_2);
 
148
                blob_size = CS_GET_DISK_6(blob.rb_blob_repo_size_6);
 
149
                blob_data_size = CS_GET_DISK_6(blob.rb_blob_data_size_6);
 
150
                status = CS_GET_DISK_1(blob.rb_status_1);
 
151
                if ((blob_data_size == 0) || ref_count <= 0 || ref_size == 0 ||
 
152
                        head_size < src_repo->myRepoBlobHeadSize + ref_count * ref_size ||
 
153
                        !VALID_BLOB_STATUS(status)) {
 
154
                        /* Can't be true. Assume this is garbage! */
 
155
                        unlock_(lock);
 
156
                        src_offset++;
 
157
                        goto retry_read;
 
158
                }
 
159
                if (IN_USE_BLOB_STATUS(status)) {
 
160
                        head->setLength(head_size);
 
161
                        if (src_file->read(head->getBuffer(0), src_offset, head_size, 0) != head_size) {
 
162
                                unlock_(lock);
 
163
                                break;
 
164
                        }
 
165
 
 
166
                        table_ref_count = 0;
 
167
                        blob_ref_count = 0;
 
168
                        
 
169
                        ptr.rp_chars = head->getBuffer(0) + src_repo->myRepoBlobHeadSize;
 
170
                        for (int count = 0; count < ref_count; count++) {
 
171
                                switch (CS_GET_DISK_2(ptr.rp_ref->rr_type_2)) {
 
172
                                        case MS_BLOB_FREE_REF:
 
173
                                                break;
 
174
                                        case MS_BLOB_TABLE_REF:
 
175
                                                /* Check the reference: */
 
176
                                                tab_id = CS_GET_DISK_4(ptr.rp_tab_ref->tr_table_id_4);
 
177
                                                blob_id = CS_GET_DISK_6(ptr.rp_tab_ref->tr_blob_id_6);
 
178
 
 
179
                                                otab = MSTableList::getOpenTableByID(iCompactorDatabase->myDatabaseID, tab_id);
 
180
                                                if (otab) {
 
181
                                                        frompool_(otab);
 
182
                                                        /* Ignore the return value (it will fail because auth_code is wrong!)!! */
 
183
                                                        uint32_t auth_code = 0;
 
184
                                                        otab->getDBTable()->readBlobHandle(otab, blob_id, &auth_code, &repo_id, &repo_offset, &repo_blob_size, &repo_head_size, false);
 
185
                                                        backtopool_(otab);
 
186
                                                        if (repo_id == src_repo_id &&
 
187
                                                                repo_offset == src_offset &&
 
188
                                                                repo_blob_size == blob_data_size &&
 
189
                                                                repo_head_size == head_size)
 
190
                                                                table_ref_count++;
 
191
                                                        else
 
192
                                                                /* Remove the reference: */
 
193
                                                                CS_SET_DISK_2(ptr.rp_ref->rr_type_2, MS_BLOB_FREE_REF);
 
194
                                                }
 
195
                                                else
 
196
                                                        CS_SET_DISK_2(ptr.rp_ref->rr_type_2, MS_BLOB_FREE_REF);
 
197
                                                break;
 
198
                                        case MS_BLOB_DELETE_REF:
 
199
                                                /* These are temporary references from the TempLog file. */
 
200
                                                /* We try to prevent this from happening, but it can! */
 
201
                                                uint32_t                        temp_log_id;
 
202
                                                uint32_t                        temp_log_offset;
 
203
                                                MSTempLogFile   *temp_log;
 
204
 
 
205
                                                temp_log_id = CS_GET_DISK_4(ptr.rp_temp_ref->tp_log_id_4);
 
206
                                                temp_log_offset = CS_GET_DISK_4(ptr.rp_temp_ref->tp_offset_4);
 
207
                                                if ((temp_log = iCompactorDatabase->openTempLogFile(temp_log_id, NULL, NULL))) {
 
208
                                                        MSTempLogItemRec        log_item;
 
209
                                                        uint32_t                                then;
 
210
                                                        time_t                          now;
 
211
 
 
212
                                                        push_(temp_log);
 
213
                                                        if (temp_log->read(&log_item, temp_log_offset, sizeof(MSTempLogItemRec), 0) == sizeof(MSTempLogItemRec)) {
 
214
                                                                then = CS_GET_DISK_4(log_item.ti_time_4);
 
215
                                                                now = time(NULL);
 
216
                                                                if (now < then + MSTempLog::gTempBlobTimeout) {
 
217
                                                                        /* Wait for the BLOB to expire before we continue: */                                                                   
 
218
                                                                        release_(temp_log);
 
219
                                                                        unlock_(lock);
 
220
 
 
221
                                                                        /* Go to sleep until the problem has gone away! */
 
222
                                                                        lock_(this);
 
223
                                                                        suspendedWait(MSTempLog::adjustWaitTime(then, now));
 
224
                                                                        unlock_(this);
 
225
                                                                        goto retry_loop;
 
226
                                                                }
 
227
                                                        }
 
228
                                                        release_(temp_log);
 
229
                                                }
 
230
 
 
231
                                                /* Remove the temp reference: */
 
232
                                                CS_SET_DISK_2(ptr.rp_ref->rr_type_2, MS_BLOB_FREE_REF);
 
233
                                                break;
 
234
                                        default:
 
235
                                                tab_index = CS_GET_DISK_2(ptr.rp_blob_ref->er_table_2);
 
236
                                                if (tab_index > ref_count || !tab_index) {
 
237
                                                        /* Can't be true. Assume this is garbage! */
 
238
                                                        unlock_(lock);
 
239
                                                        src_offset++;
 
240
                                                        goto retry_read;
 
241
                                                }
 
242
                                                blob_ref_count++;
 
243
                                                break;
 
244
                                }
 
245
                                ptr.rp_chars += ref_size;
 
246
                        }
 
247
 
 
248
                        if (table_ref_count && blob_ref_count) {
 
249
                                /* Check the blob references again to make sure that they
 
250
                                 * refer to valid table references.
 
251
                                 */
 
252
                                MSRepoTableRefPtr       tab_ref;
 
253
 
 
254
                                blob_ref_count = 0;
 
255
                                ptr.rp_chars = head->getBuffer(0) + src_repo->myRepoBlobHeadSize;
 
256
                                for (int count = 0; count < ref_count; count++) {
 
257
                                        switch (CS_GET_DISK_2(ptr.rp_ref->rr_type_2)) {
 
258
                                                case MS_BLOB_FREE_REF:
 
259
                                                case MS_BLOB_TABLE_REF:
 
260
                                                case MS_BLOB_DELETE_REF:
 
261
                                                        break;
 
262
                                                default: // If it isn't one of the above we assume it is an blob ref. (er_table_2 can never have a value equal to one of the above REF type flags.)
 
263
                                                                // It was already verified above that the index was with in range.
 
264
                                                        tab_ref = (MSRepoTableRefPtr) (head->getBuffer(0) + src_repo->myRepoBlobHeadSize + (CS_GET_DISK_2(ptr.rp_blob_ref->er_table_2)-1) * ref_size);
 
265
                                                        if (CS_GET_DISK_2(tab_ref->rr_type_2) == MS_BLOB_TABLE_REF)
 
266
                                                                blob_ref_count++;
 
267
                                                        break;
 
268
                                        }
 
269
                                        ptr.rp_chars += ref_size;
 
270
                                }
 
271
                        }
 
272
 
 
273
                        if (blob_ref_count) {
 
274
                                off64_t dst_offset;
 
275
 
 
276
                                dst_offset = dst_repo->myRepoFileSize;
 
277
 
 
278
                                /* Write the header. */
 
279
                                dst_file->write(head->getBuffer(0), dst_offset, head_size);
 
280
 
 
281
                                /* We have an engine reference, copy the BLOB over: */
 
282
                                CSFile::transfer(dst_file, dst_offset + head_size, src_file, src_offset + head_size, blob_size, iCompactBuffer, MS_COMPACTOR_BUFFER_SIZE);
 
283
 
 
284
#ifdef HAVE_ALIAS_SUPPORT
 
285
                                /* If the BLOB has an alias update the alias index. */
 
286
                                if (CS_GET_DISK_2(blob.rb_alias_offset_2)) {
 
287
                                        iCompactorDatabase->moveBlobAlias( src_repo_id, src_offset, CS_GET_DISK_4(blob.rb_alias_hash_4), dst_repo->myRepoID, dst_offset);
 
288
                                }
 
289
#endif                          
 
290
                                /* Update the references: */
 
291
                                ptr.rp_chars = head->getBuffer(0) + src_repo->myRepoBlobHeadSize;
 
292
                                for (int count = 0; count < ref_count; count++) {
 
293
                                        switch (CS_GET_DISK_2(ptr.rp_ref->rr_type_2)) {
 
294
                                                case MS_BLOB_FREE_REF:
 
295
                                                        break;
 
296
                                                case MS_BLOB_TABLE_REF:
 
297
                                                        tab_id = CS_GET_DISK_4(ptr.rp_tab_ref->tr_table_id_4);
 
298
                                                        blob_id = CS_GET_DISK_6(ptr.rp_tab_ref->tr_blob_id_6);
 
299
 
 
300
                                                        if ((otab = MSTableList::getOpenTableByID(iCompactorDatabase->myDatabaseID, tab_id))) {
 
301
                                                                frompool_(otab);
 
302
                                                                otab->getDBTable()->updateBlobHandle(otab, blob_id, dst_repo->myRepoID, dst_offset, 0);
 
303
                                                                backtopool_(otab);
 
304
                                                        }
 
305
                                                        break;
 
306
                                                case MS_BLOB_DELETE_REF:
 
307
                                                        break;
 
308
                                                default:
 
309
                                                        break;
 
310
                                        }
 
311
                                        ptr.rp_chars += ref_size;
 
312
                                }
 
313
 
 
314
                                dst_repo->myRepoFileSize += head_size + blob_size;
 
315
                        }
 
316
                }
 
317
                
 
318
                unlock_(lock);
 
319
                src_offset += head_size + blob_size;
 
320
        }
 
321
 
 
322
        src_repo->mustBeDeleted = true;
 
323
        complete = true;
 
324
 
 
325
        quit:
 
326
        release_(head);
 
327
        release_(dst_file);
 
328
        backtopool_(dst_repo);
 
329
        release_(src_file);
 
330
        backtopool_(src_repo);
 
331
 
 
332
        if (complete)
 
333
                iCompactorDatabase->removeRepo(src_repo_id, &myMustQuit);
 
334
 
 
335
        if (!myMustQuit)
 
336
                goto retry;
 
337
        return_(true);
 
338
}
 
339
 
 
340
void *MSCompactorThread::finalize()
 
341
{
 
342
        close();
 
343
        return NULL;
 
344
};
 
345