summaryrefslogtreecommitdiff
path: root/ndb/src/old_files/rep/adapters/AppNDB.cpp
blob: 05f6d52807fa758d46e888c78d83b3334934cbdd (plain)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* Copyright (C) 2003 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "AppNDB.hpp"
#include <ConfigRetriever.hpp>
#include <AttributeHeader.hpp>
#include <NdbOperation.hpp>
#include <NdbDictionaryImpl.hpp>

#include <signaldata/RepImpl.hpp>
#include <TransporterFacade.hpp>
#include <trigger_definitions.h>
#include <rep/storage/GCIPage.hpp>
#include <rep/storage/GCIBuffer.hpp>
#include <rep/rep_version.hpp>

/*****************************************************************************
 * Constructor / Destructor / Init
 *****************************************************************************/

AppNDB::~AppNDB() 
{
  delete m_tableInfoPs;
  delete m_ndb;
  m_tableInfoPs = 0;
}

AppNDB::AppNDB(GCIContainer * gciContainer, RepState * repState)
{
  m_gciContainer = gciContainer;
  m_repState = repState;
  m_cond = NdbCondition_Create();
  m_started = true;
}

void 
AppNDB::init(const char* connectString) {

  //  NdbThread_SetConcurrencyLevel(1+ 2);
  m_ndb = new Ndb("");

  m_ndb->useFullyQualifiedNames(false);

  m_ndb->setConnectString(connectString);
  /**
   * @todo  Set proper max no of transactions?? needed?? Default 12??
   */
  m_ndb->init(2048);
  m_dict = m_ndb->getDictionary();

  m_ownNodeId = m_ndb->getNodeId();

  ndbout << "-- NDB Cluster -- REP node " << m_ownNodeId << " -- Version " 
	 << REP_VERSION_ID << " --" << endl;
  ndbout_c("Connecting to NDB Cluster...");
  if (m_ndb->waitUntilReady() != 0){
    REPABORT("NDB Cluster not ready for connections");
  }
  ndbout_c("Phase 1 (AppNDB): Connection 1 to NDB Cluster opened (Applier)");
  
  m_tableInfoPs = new TableInfoPs();

  m_applierThread = NdbThread_Create(runAppNDB_C,
				     (void**)this,
				     32768,
				     "AppNDBThread",
				     NDB_THREAD_PRIO_LOW);  
}


/*****************************************************************************
 * Threads
 *****************************************************************************/

extern "C" 
void* 
runAppNDB_C(void * me)
{
  ((AppNDB *) me)->threadMainAppNDB();
  NdbThread_Exit(0);
  return me;
}

void 
AppNDB::threadMainAppNDB() {
  MetaRecord * mr;
  LogRecord * lr;
  GCIBuffer::iterator * itBuffer;
  GCIPage::iterator * itPage;
  GCIBuffer * buffer;
  GCIPage * page;
  Uint32 gci=0;

  bool force;
  while(true){

    m_gciBufferList.lock();
    if(m_gciBufferList.size()==0)
      NdbCondition_Wait(m_cond, m_gciBufferList.getMutex());
    m_gciBufferList.unlock();
    
    /**
     * Do nothing if we are not started!
     */
    if(!m_started)
      continue;
    
    if(m_gciBufferList.size()>0) {
      m_gciBufferList.lock();
      buffer = m_gciBufferList[0];
      assert(buffer!=0);
      if(buffer==0) {
	m_gciBufferList.unlock();      
//	stopApplier(GrepError::REP_APPLY_NULL_GCIBUFFER);
	return;
      }
      m_gciBufferList.unlock();      

      RLOG(("Applying %d:[%d]", buffer->getId(), buffer->getGCI()));
      gci = buffer->getGCI();
      /**
       * Do stuff with buffer
       */
     
      force = buffer->m_force;
      itBuffer = new GCIBuffer::iterator(buffer);
      page = itBuffer->first();
      
      Record * record;     
      while(page!=0 && m_started) {
  
	itPage = new GCIPage::iterator(page);
	record = itPage->first();
	
	while(record!=0 && m_started) {
	  switch(Record::RecordType(record->recordType)) {
	  case Record::META:
	    mr  = (MetaRecord*)record;
	    if(applyMetaRecord(mr, gci) < 0){
	      /**
	       * If we fail with a meta record then 
	       * we should fail the replication!
 	       */
	      //stopApplier(GrepError::REP_APPLY_METARECORD_FAILED);
	    }
	  break;
	  case Record::LOG:
	    lr  = (LogRecord*)record;
	    if(applyLogRecord(lr, force, gci) < 0) {
	      /**
	       * If we fail to apply a log record AND
	       * we have sent a ref to repstate event,
	       * then we should not try to apply another one!
	       */
//	      stopApplier(GrepError::REP_APPLY_LOGRECORD_FAILED);
	    }
	    break;
	  default:
	    REPABORT("Illegal record type");
	  };
	  record = itPage->next();
	}
	delete itPage;
	itPage = 0;
	page = itBuffer->next();
      }

      m_gciBufferList.erase(0, true);
      /**
       * "callback" to RepState to send REP_INSERT_GCIBUFFER_CONF
       */
      m_repState->eventInsertConf(buffer->getGCI(), buffer->getId());
      delete itBuffer;
      itBuffer = 0;
      mr = 0;
      lr = 0;
      page = 0;
      buffer = 0;
    }
  }

 
}

void AppNDB::startApplier(){
  m_started = true;
}


void AppNDB::stopApplier(GrepError::Code err){
  m_started = false;
  m_repState->eventInsertRef(0,0,0, err);
}


GrepError::Code
AppNDB::applyBuffer(Uint32 nodeGrp, Uint32 epoch, Uint32 force)
{
  m_gciBufferList.lock();

  GCIBuffer * buffer = m_gciContainer->getGCIBuffer(epoch, nodeGrp);
  if (buffer == NULL) {
    RLOG(("WARNING! Request to apply NULL buffer %d[%d]. Force %d", 
	  nodeGrp, epoch, force));
    return GrepError::NO_ERROR;
  }
  if (!buffer->isComplete()) {
    RLOG(("WARNING! Request to apply non-complete buffer %d[%d]. Force %d",
	  nodeGrp, epoch, force));
    return GrepError::REP_APPLY_NONCOMPLETE_GCIBUFFER;
  }
  buffer->m_force = force;

  assert(buffer!=0);
  m_gciBufferList.push_back(buffer, false);
  NdbCondition_Broadcast(m_cond);
  m_gciBufferList.unlock();
  return GrepError::NO_ERROR;
}

int
AppNDB::applyLogRecord(LogRecord*  lr, bool force, Uint32 gci) 
{
#if 0
  RLOG(("Applying log record (force %d, Op %d, GCI %d)", 
	force, lr->operation, gci));
#endif
  
  int  retries =0;
 retry:
  if(retries == 10) {
    m_repState->eventInsertRef(gci, 0, lr->tableId,     
			       GrepError::REP_APPLIER_EXECUTE_TRANSACTION);
    return -1;
  }
  NdbConnection * trans = m_ndb->startTransaction();
  if (trans == NULL) {
    /**
     * Transaction could not be started
     * @todo Handle the error by:
     *       1. Return error code
     *       2. Print log message
     *       3. On higher level indicate that DB has been tainted
     */
    ndbout_c("AppNDB: Send the following error msg to NDB Cluster support");
    reportNdbError("Cannot start transaction!", trans->getNdbError());
    m_repState->eventInsertRef(gci, 0, 0, 
			       GrepError::REP_APPLIER_START_TRANSACTION);
    REPABORT("Can not start transaction");
  }
  
  /**
   * Resolve table name based on table id
   */
  const Uint32 tableId = lr->tableId;
  const char * tableName = m_tableInfoPs->getTableName(tableId);
  
  /**
   * Close trans and return if it is systab_0.
   */
  if (tableId == 0) {
    RLOG(("WARNING! System table log record received"));
    m_ndb->closeTransaction(trans);    
    return -1;
  }
  
  if (tableName==0) {
    /**
     * Table probably does not exist
     * (Under normal operation this should not happen 
     * since log records should not appear unless the 
     * table has been created.)
     *
     * @todo Perhaps the table is not cached due to a restart,
     *       so let's check in the dictionary if it exists.
     */
    m_ndb->closeTransaction(trans);
    m_repState->eventInsertRef(gci, 0, tableId, 
			       GrepError::REP_APPLIER_NO_TABLE);
    return -1;
  }
  
  const NdbDictionary::Table * table  = m_dict->getTable(tableName);
  
  NdbOperation * op = trans->getNdbOperation(tableName);
  if (op == NULL) {
    ndbout_c("AppNDB: Send the following error msg to NDB Cluster support");
    reportNdbError("Cannot get NdbOperation record",
		   trans->getNdbError());
    m_repState->eventInsertRef(gci,0,tableId,
			       GrepError::REP_APPLIER_NO_OPERATION);
    REPABORT("Can not get NdbOperation record");
  }
  
  int check=0;
  switch(lr->operation) {
  case TriggerEvent::TE_INSERT: // INSERT
    check = op->insertTuple();
    break;
  case TriggerEvent::TE_DELETE: // DELETE
    check = op->deleteTuple();    
    break;
  case TriggerEvent::TE_UPDATE: // UPDATE
    if (force) {
      check = op->writeTuple();
    } else {
      check = op->updateTuple();
    }
    break;
  case TriggerEvent::TE_CUSTOM: //SCAN
    check = op->writeTuple();
    break;
  default:
    m_ndb->closeTransaction(trans);
    return -1;
  };

  if (check<0) {
    ndbout_c("AppNDB: Something is weird");
  }
  
  /**
   * @todo index inside LogRecord struct somewhat prettier
   * Now it 4 (sizeof(Uint32)), and 9 the position inside the struct 
   * where the data starts.
   */
  AttributeHeader * ah=(AttributeHeader *)((char *)lr + sizeof(Uint32) * 9);
  AttributeHeader *end = (AttributeHeader *)(ah + lr->attributeHeaderWSize); 
  Uint32 * dataPtr = (Uint32 *)(end);

  /**
   *  @note attributeheader for operaration insert includes a duplicate
   *  p.k.  The quick fix for this problem/bug is to skip the first set of 
   *  of p.k, and start from the other set of P.Ks. Data is duplicated for
   *  the p.k.
   */
  if (lr->operation == 0) {
    for(int i = 0; i< table->getNoOfPrimaryKeys(); i++) {
      ah+=ah->getHeaderSize();
      dataPtr = dataPtr + ah->getDataSize();
    }
  }

  while (ah < end) {
    const NdbDictionary::Column * column = 
      table->getColumn(ah->getAttributeId());
    /**
     * @todo: Here is a limitation. I don't care if it is a tuplekey 
     * that is autogenerated or an ordinary pk. I just whack it in.
     * However, this must be examined.
     */
    if(column->getPrimaryKey()) {
      if(op->equal(ah->getAttributeId(), (const char *)dataPtr) < 0) {
	ndbout_c("AppNDB: Equal failed id %d op %d name %s, gci %d force %d", 
		 ah->getAttributeId(),
		 lr->operation,
		 column->getName(), gci, force);
	reportNdbError("Equal!", trans->getNdbError());
	}
      
    } else {
      if(op->setValue(ah->getAttributeId(), (const char *)dataPtr) < 0)
       ndbout_c("AppNDB: setvalue failed id %d op %d name %s, gci %d force %d",
		ah->getAttributeId(),
		lr->operation,
		column->getName(), gci, force);
    }
    
    dataPtr = dataPtr + ah->getDataSize();
    ah = ah + ah->getHeaderSize() ;
  }
  
  if(trans->execute(Commit) != 0) {
    /**
     * Transaction commit failure
     */
      const NdbError err = trans->getNdbError();
      m_ndb->closeTransaction(trans);      
      switch(err.status){
      case NdbError::Success:
	{
	  m_repState->eventInsertRef(gci, 0, tableId,     
				     GrepError::REP_APPLIER_EXECUTE_TRANSACTION);
	  return -1;
	}
        break;
      case NdbError::TemporaryError:      
	{
	  NdbSleep_MilliSleep(50);
	  retries++;	
	  goto retry;
	}
	break;
      case NdbError::UnknownResult:
	{
	  ndbout_c("AppNDB: Send the following error msg to NDB Cluster support");
	  reportNdbError("Execute transaction failed!",
			 trans->getNdbError());
	  m_repState->eventInsertRef(gci, 0, tableId,     
				     GrepError::REP_APPLIER_EXECUTE_TRANSACTION);
	  return -1;
	}
	break;
      case NdbError::PermanentError: 
	{
	  if(err.code == 626) {
	    if(force && lr->operation == TriggerEvent::TE_DELETE) /**delete*/ {
	      /**tuple was not found. Ignore this, since 
	       * we are trying to apply a "delete a tuple"-log record before 
	       * having applied the scan data.
	       */
	      return -1;
	    }
	  }

	  ndbout_c("AppNDB: Send the following error msg to NDB Cluster support");	  	  reportNdbError("Execute transaction failed!",
			 trans->getNdbError());
	  ndbout_c("\n\nAppNDB: RepNode will now crash.");
	  m_ndb->closeTransaction(trans);
	  m_repState->eventInsertRef(gci, 0, tableId,     
			     GrepError::REP_APPLIER_EXECUTE_TRANSACTION);
	  return -1;
	}	
      break;
      }
  }

  /**
   * No errors. Close transaction and continue in applierThread.
   */
  m_ndb->closeTransaction(trans); 
  return 1;
}


int
AppNDB::applyMetaRecord(MetaRecord*  mr, Uint32 gci) 
{
  /**
   * Validate table id
   */
  Uint32 tableId = mr->tableId;
  if (tableId==0) {
    RLOG(("WARNING! Meta record contained record with tableId 0"));
    return 0;
  }
  
  /**
   * Prepare meta record 
   */
  NdbDictionary::Table * table = prepareMetaRecord(mr);
  if(table == 0) {
    RLOG(("WARNING! Prepare table meta record failed for table %d", tableId));
    m_dict->getNdbError();
    m_repState->eventInsertRef(gci,0,tableId, 
			       GrepError::REP_APPLIER_PREPARE_TABLE);
    return -1;
  }
  
  /**
   * Table does not exist in TableInfoPs -> add it
   */
  if(m_tableInfoPs->getTableName(tableId)==0) {
    RLOG(("Table %d:%s added to m_tableInfoPs", tableId, table->getName()));
    m_tableInfoPs->insert(tableId,table->getName());
  }
  
  /**
   * Validate that table does not exist in Dict
   */

  const NdbDictionary::Table * tmpTable = m_dict->getTable(table->getName());
  if(tmpTable !=0) {
    /**
     * Oops, a table with the same name exists
     */
    if(tmpTable->getObjectVersion()!=table->getObjectVersion()) {
      char buf[100];
      sprintf(buf,"WARNING! Another version of table %d:%s already exists."
	      "Currently, we dont support versions, so will abort now!",
	       tableId, table->getName());
    
      REPABORT(buf);

    }
    RLOG(("WARNING! An identical table %d:%s already exists.", 
	  tableId, table->getName()));
    return -1;
  }


  /**
   * @todo WARNING! Should scan table MR for columns that are not supported
   */
  /*
  NdbDictionary::Column * column;
  
  for(int i=0; i<table->getNoOfColumns(); i++) {
    column = table->getColumn(i);
    if(column->getAutoIncrement()) {
      reportWarning(table->getName(), column->getName(),
		    "Uses AUTOINCREMENT of PK");   
    }
  }
  */
    
  
  /**
   * Create table
   */
  if(m_dict->createTable(*table)<0) {
    ndbout_c("AppNDB: Send the following error msg to NDB Cluster support");
    reportNdbError("Create table failed!", m_dict->getNdbError());
    m_repState->eventCreateTableRef(gci, 
				    tableId,
				    table->getName(),			  
				    GrepError::REP_APPLIER_CREATE_TABLE);
    return -1;
  }
  
  RLOG(("Table %d:%s created", tableId, table->getName()));
  return 0;
}

NdbDictionary::Table*
AppNDB::prepareMetaRecord(MetaRecord* mr) {
  NdbTableImpl * tmp = 0;
  NdbDictionary::Table * table =0;
  Uint32 * data =(Uint32*)( ((char*)mr + sizeof(Uint32)*6));
  int res = NdbDictInterface::parseTableInfo(&tmp, data, mr->dataLen,
					     m_ndb->usingFullyQualifiedNames());
  if(res == 0) {
    table = tmp;
    return table;
  } else{
    return 0;
  }
}

void 
AppNDB::reportNdbError(const char * msg, const NdbError & err) {
  ndbout_c("%s : Error code %d , error message %s", 
	   msg, err.code,
	   (err.message ? err.message : ""));  
}

void 
AppNDB::reportWarning(const char * tableName, const char * message) {
  ndbout_c("WARNING: Table %s, %s", tableName, message); 
}

void 
AppNDB::reportWarning(const char * tableName, const char * columnName,
		       const char * message) {
  ndbout_c("WARNING: Table %s, column %s, %s", tableName, columnName,message);
}

int 
AppNDB::dropTable(Uint32 tableId) 
{
  char * tableName = m_tableInfoPs->getTableName(tableId);
  if(tableName == 0) return -1;
  ndbout_c("AppNDB: Dropping table ");
  if(m_dict->dropTable(tableName) != 0) {
    reportNdbError("Failed dropping table",m_dict->getNdbError());
    return -1;
  }
  m_tableInfoPs->del(tableId);
  return 1;
}