~drizzle-trunk/drizzle/development

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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Original author: Paul McCullagh
 * Continued development: Barry Leslie
 *
 * 2007-07-03
 *
 * H&G2JCtL
 *
 * Network interface.
 *
 */

#include "cslib/CSConfig.h"

#include <stddef.h>

#include "defs_ms.h"

#include "cslib/CSGlobal.h"
#include "cslib/CSStrUtil.h"
#include "cslib/CSStorage.h"

#include "temp_log_ms.h"
#include "open_table_ms.h"
#include "trans_log_ms.h"
#include "transaction_ms.h"
#include "parameters_ms.h"


// Search the transaction log for a MS_ReferenceTxn record for the given BLOB.
// Just search the log file and not the cache. Seaching the cache may be faster but
// it would require locks that could block the writers or reader threads and in the worse
// case it will still require the reading of the log anyway.
//
// This search doesn't distinguish between transactions that are still running and
// transactions that are rolled back.
class SearchTXNLog : ReadTXNLog {
	public:
	SearchTXNLog(uint32_t db_id, MSTrans *log): ReadTXNLog(log), st_db_id(db_id) {}
	
	bool	st_found;
	bool	st_terminated;
	bool	st_commited;
	uint32_t st_tid; 
	uint32_t st_db_id; 
	uint32_t st_tab_id; 
	uint64_t st_blob_id;
	
	virtual bool rl_CanContinue() { return ((!st_found) || !st_terminated);}
	virtual void rl_Load(uint64_t log_position, MSTransPtr rec) 
	{
		(void) log_position;
		
		if ( !st_found && (TRANS_TYPE(rec->tr_type) != MS_ReferenceTxn))
			return;
		
		if (!st_found) {
			if  ((rec->tr_db_id == st_db_id) && (rec->tr_tab_id == st_tab_id) && (rec->tr_blob_id == st_blob_id)) {
				st_found = true;
				st_tid = rec->tr_id;
			} else
				return;
		}
		st_terminated = TRANS_IS_TERMINATED(rec->tr_type);
		if (st_terminated)
			st_commited = (TRANS_IS_AUTOCOMMIT(rec->tr_type) || (TRANS_TYPE(rec->tr_type) == MS_CommitTxn));
	}
	
	bool st_FindBlobRef(bool *committed, uint32_t tab_id, uint64_t blob_id)
	{
		enter_();
		st_found = st_terminated = st_commited = false;
		st_tab_id = tab_id;
		st_blob_id = blob_id;	
		
		rl_ReadLog(rl_log->txn_GetStartPosition(), false);
		*committed = st_commited;
		return_(st_found);
	}
};

MSTempLogFile::MSTempLogFile():
CSReadBufferedFile(),
myTempLogID(0),
myTempLog(NULL)
{
}

MSTempLogFile::~MSTempLogFile()
{
	close();
	if (myTempLog)
		myTempLog->release();
}

MSTempLogFile *MSTempLogFile::newTempLogFile(uint32_t id, MSTempLog *temp_log, CSFile *file)
{
	MSTempLogFile *f;
	enter_();
	
	push_(temp_log);
	push_(file);
	
	if (!(f = new MSTempLogFile())) 
		CSException::throwOSError(CS_CONTEXT, ENOMEM);

	f->myTempLogID = id;
	
	pop_(file);
	f->setFile(file);
	
	pop_(temp_log);
	f->myTempLog = temp_log;
	return_(f);
}

MSTempLog::MSTempLog(uint32_t id, MSDatabase *db, off64_t file_size):
CSRefObject(),
myLogID(id),
myTempLogSize(file_size),
myTemplogRecSize(0),
myTempLogHeadSize(0),
iLogDatabase(db),
iDeleteLog(false)
{
}

MSTempLog::~MSTempLog()
{
	enter_();
	if (iDeleteLog) {
		CSPath *path;

		path = getLogPath();
		push_(path);
		path->removeFile();
		release_(path);
	}
	exit_();
}

void MSTempLog::deleteLog()
{
	iDeleteLog = true;
}

CSPath *MSTempLog::getLogPath()
{
	char file_name[120];

	cs_strcpy(120, file_name, "bs-logs");
	cs_add_dir_char(120, file_name);
	cs_strcat(120, file_name, "temp-");
	cs_strcat(120, file_name, myLogID);
	cs_strcat(120, file_name, ".bs");
	return CSPath::newPath(RETAIN(iLogDatabase->myDatabasePath), file_name);
}

MSTempLogFile *MSTempLog::openTempLog()
{
	CSPath			*path;
	MSTempLogFile	*fh;

	enter_();
	path = getLogPath();
	retain();
	fh = MSTempLogFile::newTempLogFile(myLogID, this, CSFile::newFile(path));
	push_(fh);
	if (myTempLogSize)
		fh->open(CSFile::DEFAULT);
	else
		fh->open(CSFile::CREATE);
	if (!myTempLogHeadSize) {
		MSTempLogHeadRec	head;

		lock_(iLogDatabase->myTempLogArray);
		/* Check again after locking: */
		if (!myTempLogHeadSize) {
			size_t rem;

			if (fh->read(&head, 0, offsetof(MSTempLogHeadRec, th_reserved_4), 0) < offsetof(MSTempLogHeadRec, th_reserved_4)) {
				CS_SET_DISK_4(head.th_magic_4, MS_TEMP_LOG_MAGIC);
				CS_SET_DISK_2(head.th_version_2, MS_TEMP_LOG_VERSION);
				CS_SET_DISK_2(head.th_head_size_2, MS_TEMP_LOG_HEAD_SIZE);
				CS_SET_DISK_2(head.th_rec_size_2, sizeof(MSTempLogItemRec));
				CS_SET_DISK_4(head.th_reserved_4, 0);
				fh->write(&head, 0, sizeof(MSTempLogHeadRec));
				fh->flush();
			}
			
			/* Check the file header: */
			if (CS_GET_DISK_4(head.th_magic_4) != MS_TEMP_LOG_MAGIC)
				CSException::throwFileError(CS_CONTEXT, fh->getPathString(), CS_ERR_BAD_HEADER_MAGIC);
			if (CS_GET_DISK_2(head.th_version_2) > MS_TEMP_LOG_VERSION)
				CSException::throwFileError(CS_CONTEXT, fh->getPathString(), CS_ERR_VERSION_TOO_NEW);

			/* Load the header details: */
			myTempLogHeadSize = CS_GET_DISK_2(head.th_head_size_2);
			myTemplogRecSize = CS_GET_DISK_2(head.th_rec_size_2);

			/* File size, cannot be less than header size, adjust to correct offset: */
			if (myTempLogSize < myTempLogHeadSize)
				myTempLogSize = myTempLogHeadSize;
			if ((rem = (myTempLogSize - myTempLogHeadSize) % myTemplogRecSize))
				myTempLogSize += myTemplogRecSize - rem;
		}
		unlock_(iLogDatabase->myTempLogArray);
	}
	pop_(fh);
	return_(fh);
}

time_t MSTempLog::adjustWaitTime(time_t then, time_t now)
{
	time_t wait;

	if (now < (time_t)(then + PBMSParameters::getTempBlobTimeout())) {
		wait = ((then + PBMSParameters::getTempBlobTimeout() - now) * 1000);
		if (wait < 2000)
			wait = 2000;
		else if (wait > 120 * 1000)
			wait = 120 * 1000;
	}
	else
		wait = 1;
			
	return wait;
}

/*
 * ---------------------------------------------------------------
 * TEMP LOG THREAD
 */

MSTempLogThread::MSTempLogThread(time_t wait_time, MSDatabase *db):
CSDaemon(wait_time, NULL),
iTempLogDatabase(db),
iTempLogFile(NULL),
iLogRecSize(0),
iLogOffset(0)
{
}


void MSTempLogThread::close()
{
	if (iTempLogFile) {
		iTempLogFile->release();
		iTempLogFile = NULL;
	}
}

bool MSTempLogThread::try_ReleaseBLOBReference(CSThread *self, CSStringBuffer *buffer, uint32_t tab_id, int type, uint64_t blob_id, uint32_t auth_code)
{
	volatile bool rtc = true;
	try_(a) {
		/* Release the BLOB reference. */
		MSOpenTable *otab;

		if (type == MS_TL_REPO_REF) {
			MSRepoFile	*repo_file;

			if ((repo_file = iTempLogDatabase->getRepoFileFromPool(tab_id, true))) {
				frompool_(repo_file);
				repo_file->checkBlob(buffer, blob_id, auth_code, iTempLogFile->myTempLogID, iLogOffset);
				backtopool_(repo_file);
			}
		}
		else {
			if ((otab = MSTableList::getOpenTableByID(iTempLogDatabase->myDatabaseID, tab_id))) {
				frompool_(otab);
				if (type == MS_TL_BLOB_REF) {
					otab->checkBlob(buffer, blob_id, auth_code, iTempLogFile->myTempLogID, iLogOffset);
					backtopool_(otab);
				}
				else {
					ASSERT(type == MS_TL_TABLE_REF);
					if ((type == MS_TL_TABLE_REF) && otab->deleteReferences(iTempLogFile->myTempLogID, iLogOffset, &myMustQuit)) {
						/* Delete the file now... */
						MSTable			*tab;
						CSPath			*from_path;
						MSOpenTablePool *tab_pool;

						tab = otab->getDBTable();
						from_path = otab->getDBTable()->getTableFile();

						pop_(otab);

						push_(from_path);
						tab->retain();
						push_(tab);

						tab_pool = MSTableList::lockTablePoolForDeletion(otab); // This returns otab to the pool.
						frompool_(tab_pool);

						from_path->removeFile();
						tab->myDatabase->removeTable(tab);

						backtopool_(tab_pool); // The will unlock and close the table pool freeing all tables in it.
						pop_(tab);				// Returning the pool will have released this. (YUK!)
						release_(from_path);
					}
					else 
						backtopool_(otab);
				}
			}
		}
		
		rtc = false;
	}
	
	catch_(a);
	cont_(a);
	return rtc;
}

bool MSTempLogThread::doWork()
{
	size_t				tfer;
	MSTempLogItemRec	log_item;
	CSStringBuffer		*buffer;
	SearchTXNLog		txn_log(iTempLogDatabase->myDatabaseID, MSTransactionManager::tm_Log);

	enter_();
	new_(buffer, CSStringBuffer(20));
	push_(buffer);
	while (!myMustQuit) {
		if (!iTempLogFile) {
			size_t head_size;
			if (!(iTempLogFile = iTempLogDatabase->openTempLogFile(0, &iLogRecSize, &head_size))) {
				release_(buffer);
				return_(true);
			}
			iLogOffset = head_size;
		}

		tfer = iTempLogFile->read(&log_item, iLogOffset, sizeof(MSTempLogItemRec), 0);
		if (tfer == 0) {
			/* No more data to be read: */

			/* Check to see if there is a log after this: */
			if (iTempLogDatabase->getTempLogCount() <= 1) {
				/* The next log does not yet exist. We wait for
				 * it to be created before we delete and
				 * close the current log.
				 */
				myWaitTime = PBMSParameters::getTempBlobTimeout() * 1000;
				break;
			}

			iTempLogFile->myTempLog->deleteLog();
			iTempLogDatabase->removeTempLog(iTempLogFile->myTempLogID);
			close();
		}
		else if (tfer == sizeof(MSTempLogItemRec)) {
			/* We have a record: */
			int		type;
			uint32_t tab_id;
			uint64_t blob_id= 0;
			uint32_t auth_code;
			uint32_t then;
			time_t	now;

			/*
			 * Items in the temp log are never updated.
			 * If a temp operation is canceled then the object 
			 * records this itself and when the temp operation 
			 * is attempted it will recognize by the templog
			 * id and offset that it is no longer a valid 
			 * operation.
			 */
			tab_id = CS_GET_DISK_4(log_item.ti_table_id_4);
				
			type = CS_GET_DISK_1(log_item.ti_type_1);
			blob_id = CS_GET_DISK_6(log_item.ti_blob_id_6);
			auth_code = CS_GET_DISK_4(log_item.ti_auth_code_4);
			then = CS_GET_DISK_4(log_item.ti_time_4);

			now = time(NULL);
			if (now < (time_t)(then + PBMSParameters::getTempBlobTimeout())) {
				/* Time has not yet exired, adjust wait time: */
				myWaitTime = MSTempLog::adjustWaitTime(then, now);
				break;
			}
		
			if (try_ReleaseBLOBReference(self, buffer, tab_id, type, blob_id, auth_code)) {
				int err = self->myException.getErrorCode();
				
				if (err == MS_ERR_TABLE_LOCKED) {
					throw_();
				}
				else if (err == MS_ERR_REMOVING_REPO) {
					/* Wait for the compactor to finish: */
					myWaitTime = 2 * 1000;
					release_(buffer);
					return_(true);
				}
				else if ((err == MS_ERR_UNKNOWN_TABLE) || (err == MS_ERR_DATABASE_DELETED))
					;
				else
					self->myException.log(NULL);
			}

		}
		else {
			// Only part of the data read, don't wait very long to try again:
			myWaitTime = 2 * 1000;
			break;
		}
		iLogOffset += iLogRecSize;
	}

	release_(buffer);
	return_(true);
}

void *MSTempLogThread::completeWork()
{
	close();
	return NULL;
}