NdbEventOperationImpl.cpp 61.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 */


18 19
#include <ndb_global.h>
#include <kernel_types.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include "NdbDictionaryImpl.hpp"
#include "API.hpp"
#include <NdbOut.hpp>
#include "NdbApiSignal.hpp"
#include "TransporterFacade.hpp"
#include <signaldata/CreateEvnt.hpp>
#include <signaldata/SumaImpl.hpp>
#include <SimpleProperties.hpp>
#include <Bitmask.hpp>
#include <AttributeHeader.hpp>
#include <AttributeList.hpp>
#include <NdbError.hpp>
#include <BaseString.hpp>
#include <UtilBuffer.hpp>
#include <NdbDictionary.hpp>
#include <Ndb.hpp>
#include "NdbImpl.hpp"
#include "DictCache.hpp"
#include <portlib/NdbMem.h>
#include <NdbRecAttr.hpp>
41
#include <NdbBlob.hpp>
42 43
#include <NdbEventOperation.hpp>
#include "NdbEventOperationImpl.hpp"
44
#include <signaldata/AlterTable.hpp>
45

46 47 48 49 50 51 52
#include <EventLogger.hpp>
extern EventLogger g_eventLogger;

static Gci_container g_empty_gci_container;
static const Uint32 ACTIVE_GCI_DIRECTORY_SIZE = 4;
static const Uint32 ACTIVE_GCI_MASK = ACTIVE_GCI_DIRECTORY_SIZE - 1;

53 54 55 56 57 58 59 60 61 62 63 64 65 66
#ifdef VM_TRACE
static void
print_std(const SubTableData * sdata, LinearSectionPtr ptr[3])
{
  printf("addr=%p gci=%d op=%d\n", (void*)sdata, sdata->gci, sdata->operation);
  for (int i = 0; i <= 2; i++) {
    printf("sec=%d addr=%p sz=%d\n", i, (void*)ptr[i].p, ptr[i].sz);
    for (int j = 0; j < ptr[i].sz; j++)
      printf("%08x ", ptr[i].p[j]);
    printf("\n");
  }
}
#endif

67 68 69 70 71 72 73
/*
 * Class NdbEventOperationImpl
 *
 *
 */

//#define EVENT_DEBUG
74 75 76 77 78
#ifdef EVENT_DEBUG
#define DBUG_ENTER_EVENT(A) DBUG_ENTER(A)
#define DBUG_RETURN_EVENT(A) DBUG_RETURN(A)
#define DBUG_VOID_RETURN_EVENT DBUG_VOID_RETURN
#define DBUG_PRINT_EVENT(A,B) DBUG_PRINT(A,B)
79
#define DBUG_DUMP_EVENT(A,B,C) DBUG_DUMP(A,B,C)
80 81 82 83 84 85 86
#else
#define DBUG_ENTER_EVENT(A)
#define DBUG_RETURN_EVENT(A) return(A)
#define DBUG_VOID_RETURN_EVENT return
#define DBUG_PRINT_EVENT(A,B)
#define DBUG_DUMP_EVENT(A,B,C)
#endif
87

88 89
// todo handle several ndb objects
// todo free allocated data when closing NdbEventBuffer
90

91
NdbEventOperationImpl::NdbEventOperationImpl(NdbEventOperation &f,
92
					     Ndb *theNdb, 
93 94 95 96 97
					     const char* eventName) :
  NdbEventOperation(*this),
  m_facade(&f),
  m_ndb(theNdb),
  m_state(EO_ERROR)
98
{
99
  DBUG_ENTER("NdbEventOperationImpl::NdbEventOperationImpl");
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

  assert(m_ndb != NULL);
  NdbDictionary::Dictionary *myDict = m_ndb->getDictionary();
  assert(myDict != NULL);

  const NdbDictionary::Event *myEvnt = myDict->getEvent(eventName);
  if (!myEvnt) { m_error.code= myDict->getNdbError().code; DBUG_VOID_RETURN; }

  init(myEvnt->m_impl);
  DBUG_VOID_RETURN;
}

NdbEventOperationImpl::NdbEventOperationImpl(Ndb *theNdb,
                                             NdbEventImpl& evnt) :
  NdbEventOperation(*this),
  m_facade(this),
  m_ndb(theNdb),
  m_state(EO_ERROR)
{
  DBUG_ENTER("NdbEventOperationImpl::NdbEventOperationImpl [evnt]");
  init(evnt);
  DBUG_VOID_RETURN;
}

void
NdbEventOperationImpl::init(NdbEventImpl& evnt)
{
  DBUG_ENTER("NdbEventOperationImpl::init");

  m_magic_number = 0;
  mi_type = 0;
  m_oid = ~(Uint32)0;
  m_change_mask = 0;
#ifdef VM_TRACE
  m_data_done_count = 0;
  m_data_count = 0;
#endif
  m_next = 0;
  m_prev = 0;

140
  m_eventId = 0;
141 142 143 144 145 146 147 148
  theFirstPkAttrs[0] = NULL;
  theCurrentPkAttrs[0] = NULL;
  theFirstPkAttrs[1] = NULL;
  theCurrentPkAttrs[1] = NULL;
  theFirstDataAttrs[0] = NULL;
  theCurrentDataAttrs[0] = NULL;
  theFirstDataAttrs[1] = NULL;
  theCurrentDataAttrs[1] = NULL;
149 150 151 152 153

  theBlobList = NULL;
  theBlobOpList = NULL;
  theMainOp = NULL;

154 155 156 157 158
  m_data_item= NULL;
  m_eventImpl = NULL;

  m_custom_data= 0;
  m_has_error= 1;
159 160 161 162

  // we should lookup id in Dictionary, TODO
  // also make sure we only have one listener on each event

163
  m_eventImpl = &evnt;
164

165 166
  m_eventId = m_eventImpl->m_eventId;

167 168 169
  m_oid= m_ndb->theImpl->theNdbObjectIdMap.map(this);

  m_state= EO_CREATED;
170

171 172 173 174 175
#ifdef ndb_event_stores_merge_events_flag
  m_mergeEvents = m_eventImpl->m_mergeEvents;
#else
   m_mergeEvents = false;
#endif
176

177 178 179 180
  m_has_error= 0;

  DBUG_PRINT("exit",("this: 0x%x oid: %u", this, m_oid));
  DBUG_VOID_RETURN;
181 182 183 184
}

NdbEventOperationImpl::~NdbEventOperationImpl()
{
185 186 187 188 189 190 191 192
  DBUG_ENTER("NdbEventOperationImpl::~NdbEventOperationImpl");
  m_magic_number= 0;

  stop();
  // m_bufferHandle->dropSubscribeEvent(m_bufferId);
  ; // ToDo? We should send stop signal here
  
  m_ndb->theImpl->theNdbObjectIdMap.unmap(m_oid, this);
193 194
  DBUG_PRINT("exit",("this: %p/%p oid: %u main: %p",
             this, m_facade, m_oid, theMainOp));
195 196 197 198 199

  if (m_eventImpl)
  {
    delete m_eventImpl->m_facade;
    m_eventImpl= 0;
200
  }
201 202

  DBUG_VOID_RETURN;
203 204 205 206 207 208 209 210 211 212 213
}

NdbEventOperation::State
NdbEventOperationImpl::getState()
{
  return m_state;
}

NdbRecAttr*
NdbEventOperationImpl::getValue(const char *colName, char *aValue, int n)
{
214
  DBUG_ENTER("NdbEventOperationImpl::getValue");
215
  if (m_state != EO_CREATED) {
216 217
    ndbout_c("NdbEventOperationImpl::getValue may only be called between "
	     "instantiation and execute()");
218
    DBUG_RETURN(NULL);
219 220 221 222 223 224
  }

  NdbColumnImpl *tAttrInfo = m_eventImpl->m_tableImpl->getColumn(colName);

  if (tAttrInfo == NULL) {
    ndbout_c("NdbEventOperationImpl::getValue attribute %s not found",colName);
225
    DBUG_RETURN(NULL);
226 227
  }

228
  DBUG_RETURN(NdbEventOperationImpl::getValue(tAttrInfo, aValue, n));
229 230 231 232 233
}

NdbRecAttr*
NdbEventOperationImpl::getValue(const NdbColumnImpl *tAttrInfo, char *aValue, int n)
{
234
  DBUG_ENTER("NdbEventOperationImpl::getValue");
235
  // Insert Attribute Id into ATTRINFO part. 
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

  NdbRecAttr **theFirstAttr;
  NdbRecAttr **theCurrentAttr;

  if (tAttrInfo->getPrimaryKey())
  {
    theFirstAttr = &theFirstPkAttrs[n];
    theCurrentAttr = &theCurrentPkAttrs[n];
  }
  else
  {
    theFirstAttr = &theFirstDataAttrs[n];
    theCurrentAttr = &theCurrentDataAttrs[n];
  }

251 252 253
  /************************************************************************
   *	Get a Receive Attribute object and link it into the operation object.
   ************************************************************************/
254 255
  NdbRecAttr *tAttr = m_ndb->getRecAttr();
  if (tAttr == NULL) { 
256 257
    exit(-1);
    //setErrorCodeAbort(4000);
258
    DBUG_RETURN(NULL);
259 260 261 262 263 264 265
  }

  /**********************************************************************
   * Now set the attribute identity and the pointer to the data in 
   * the RecAttr object
   * Also set attribute size, array size and attribute type
   ********************************************************************/
266
  if (tAttr->setup(tAttrInfo, aValue)) {
267
    //setErrorCodeAbort(4000);
268
    m_ndb->releaseRecAttr(tAttr);
269
    exit(-1);
270
    DBUG_RETURN(NULL);
271 272 273
  }
  //theErrorLine++;

274
  tAttr->setUNDEFINED();
275 276
  
  // We want to keep the list sorted to make data insertion easier later
277 278 279 280 281

  if (*theFirstAttr == NULL) {
    *theFirstAttr = tAttr;
    *theCurrentAttr = tAttr;
    tAttr->next(NULL);
282 283
  } else {
    Uint32 tAttrId = tAttrInfo->m_attrId;
284 285 286 287 288 289 290 291
    if (tAttrId > (*theCurrentAttr)->attrId()) { // right order
      (*theCurrentAttr)->next(tAttr);
      tAttr->next(NULL);
      *theCurrentAttr = tAttr;
    } else if ((*theFirstAttr)->next() == NULL ||    // only one in list
	       (*theFirstAttr)->attrId() > tAttrId) {// or first 
      tAttr->next(*theFirstAttr);
      *theFirstAttr = tAttr;
292
    } else { // at least 2 in list and not first and not last
293
      NdbRecAttr *p = *theFirstAttr;
294 295 296 297 298 299
      NdbRecAttr *p_next = p->next();
      while (tAttrId > p_next->attrId()) {
	p = p_next;
	p_next = p->next();
      }
      if (tAttrId == p_next->attrId()) { // Using same attribute twice
300 301
	tAttr->release(); // do I need to do this?
	m_ndb->releaseRecAttr(tAttr);
302
	exit(-1);
303
	DBUG_RETURN(NULL);
304 305
      }
      // this is it, between p and p_next
306 307
      p->next(tAttr);
      tAttr->next(p_next);
308 309
    }
  }
310
  DBUG_RETURN(tAttr);
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
NdbBlob*
NdbEventOperationImpl::getBlobHandle(const char *colName, int n)
{
  DBUG_ENTER("NdbEventOperationImpl::getBlobHandle (colName)");

  assert(m_mergeEvents);

  if (m_state != EO_CREATED) {
    ndbout_c("NdbEventOperationImpl::getBlobHandle may only be called between "
	     "instantiation and execute()");
    DBUG_RETURN(NULL);
  }

  NdbColumnImpl *tAttrInfo = m_eventImpl->m_tableImpl->getColumn(colName);

  if (tAttrInfo == NULL) {
    ndbout_c("NdbEventOperationImpl::getBlobHandle attribute %s not found",colName);
    DBUG_RETURN(NULL);
  }

  NdbBlob* bh = getBlobHandle(tAttrInfo, n);
  DBUG_RETURN(bh);
}

NdbBlob*
NdbEventOperationImpl::getBlobHandle(const NdbColumnImpl *tAttrInfo, int n)
{
  DBUG_ENTER("NdbEventOperationImpl::getBlobHandle");
  DBUG_PRINT("info", ("attr=%s post/pre=%d", tAttrInfo->m_name.c_str(), n));
  
  // as in NdbOperation, create only one instance
  NdbBlob* tBlob = theBlobList;
  NdbBlob* tLastBlob = NULL;
  while (tBlob != NULL) {
    if (tBlob->theColumn == tAttrInfo && tBlob->theEventBlobVersion == n)
      DBUG_RETURN(tBlob);
    tLastBlob = tBlob;
    tBlob = tBlob->theNext;
  }

pekka@mysql.com's avatar
pekka@mysql.com committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  NdbEventOperationImpl* tBlobOp = NULL;

  const bool is_tinyblob = (tAttrInfo->getPartSize() == 0);
  assert(is_tinyblob == (tAttrInfo->m_blobTable == NULL));

  if (! is_tinyblob) {
    // blob event name
    char bename[MAX_TAB_NAME_SIZE];
    NdbBlob::getBlobEventName(bename, m_eventImpl, tAttrInfo);

    // find blob event op if any (it serves both post and pre handles)
    tBlobOp = theBlobOpList;
    NdbEventOperationImpl* tLastBlopOp = NULL;
    while (tBlobOp != NULL) {
      if (strcmp(tBlobOp->m_eventImpl->m_name.c_str(), bename) == 0) {
        break;
      }
      tLastBlopOp = tBlobOp;
371
      tBlobOp = tBlobOp->m_next;
372 373
    }

374 375
    DBUG_PRINT("info", ("%s blob event op for %s",
                        tBlobOp ? " reuse" : " create", bename));
pekka@mysql.com's avatar
pekka@mysql.com committed
376 377 378

    // create blob event op if not found
    if (tBlobOp == NULL) {
379 380 381 382 383 384 385 386 387 388 389 390 391 392
      // get blob event
      NdbDictionaryImpl& dict =
        NdbDictionaryImpl::getImpl(*m_ndb->getDictionary());
      NdbEventImpl* blobEvnt =
        dict.getBlobEvent(*this->m_eventImpl, tAttrInfo->m_column_no);
      if (blobEvnt == NULL) {
        m_error.code = dict.m_error.code;
        DBUG_RETURN(NULL);
      }

      // create blob event operation
      tBlobOp =
        m_ndb->theEventBuffer->createEventOperation(*blobEvnt, m_error);
      if (tBlobOp == NULL)
pekka@mysql.com's avatar
pekka@mysql.com committed
393 394 395 396 397 398
        DBUG_RETURN(NULL);

      // pointer to main table op
      tBlobOp->theMainOp = this;
      tBlobOp->m_mergeEvents = m_mergeEvents;

399
      // to hide blob op it is linked under main op, not under m_ndb
pekka@mysql.com's avatar
pekka@mysql.com committed
400 401 402
      if (tLastBlopOp == NULL)
        theBlobOpList = tBlobOp;
      else
403 404
        tLastBlopOp->m_next = tBlobOp;
      tBlobOp->m_next = NULL;
pekka@mysql.com's avatar
pekka@mysql.com committed
405
    }
406 407 408
  }

  tBlob = m_ndb->getNdbBlob();
409 410
  if (tBlob == NULL) {
    m_error.code = m_ndb->getNdbError().code;
411
    DBUG_RETURN(NULL);
412
  }
413 414 415

  // calls getValue on inline and blob part
  if (tBlob->atPrepare(this, tBlobOp, tAttrInfo, n) == -1) {
416
    m_error.code = tBlob->getNdbError().code;
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
    m_ndb->releaseNdbBlob(tBlob);
    DBUG_RETURN(NULL);
  }

  // add to list end
  if (tLastBlob == NULL)
    theBlobList = tBlob;
  else
    tLastBlob->theNext = tBlob;
  tBlob->theNext = NULL;
  DBUG_RETURN(tBlob);
}

int
NdbEventOperationImpl::readBlobParts(char* buf, NdbBlob* blob,
                                     Uint32 part, Uint32 count)
{
  DBUG_ENTER_EVENT("NdbEventOperationImpl::readBlobParts");
  DBUG_PRINT_EVENT("info", ("part=%u count=%u post/pre=%d",
                      part, count, blob->theEventBlobVersion));

  NdbEventOperationImpl* blob_op = blob->theBlobEventOp;

  EventBufData* main_data = m_data_item;
  DBUG_PRINT_EVENT("info", ("main_data=%p", main_data));
  assert(main_data != NULL);

  // search for blob parts list head
  EventBufData* head;
  assert(m_data_item != NULL);
  head = m_data_item->m_next_blob;
  while (head != NULL)
  {
    if (head->m_event_op == blob_op)
    {
      DBUG_PRINT_EVENT("info", ("found blob parts head %p", head));
      break;
    }
    head = head->m_next_blob;
  }

  Uint32 nparts = 0;
  EventBufData* data = head;
  // XXX optimize using part no ordering
  while (data != NULL)
  {
    /*
     * Hack part no directly out of buffer since it is not returned
     * in pre data (PK buglet).  For part data use receive_event().
     * This means extra copy.
     */
    blob_op->m_data_item = data;
    int r = blob_op->receive_event();
    assert(r > 0);
    Uint32 no = data->get_blob_part_no();
    Uint32 sz = blob->thePartSize;
    const char* src = blob->theBlobEventDataBuf.data;

    DBUG_PRINT_EVENT("info", ("part_data=%p part no=%u part sz=%u", data, no, sz));

    if (part <= no && no < part + count)
    {
      DBUG_PRINT_EVENT("info", ("part within read range"));
      memcpy(buf + (no - part) * sz, src, sz);
      nparts++;
    }
    else
    {
      DBUG_PRINT_EVENT("info", ("part outside read range"));
    }
    data = data->m_next;
  }
  assert(nparts == count);

  DBUG_RETURN_EVENT(0);
}

494 495 496
int
NdbEventOperationImpl::execute()
{
497
  DBUG_ENTER("NdbEventOperationImpl::execute");
498 499 500 501 502 503 504 505 506 507 508 509
  m_ndb->theEventBuffer->add_drop_lock();
  int r = execute_nolock();
  m_ndb->theEventBuffer->add_drop_unlock();
  DBUG_RETURN(r);
}

int
NdbEventOperationImpl::execute_nolock()
{
  DBUG_ENTER("NdbEventOperationImpl::execute_nolock");
  DBUG_PRINT("info", ("this=%p type=%s", this, !theMainOp ? "main" : "blob"));

510 511
  NdbDictionary::Dictionary *myDict = m_ndb->getDictionary();
  if (!myDict) {
512
    m_error.code= m_ndb->getNdbError().code;
513
    DBUG_RETURN(-1);
514 515
  }

516 517
  if (theFirstPkAttrs[0] == NULL && 
      theFirstDataAttrs[0] == NULL) { // defaults to get all
518 519
  }

520 521 522 523 524 525
  m_magic_number= NDB_EVENT_OP_MAGIC_NUMBER;
  m_state= EO_EXECUTING;
  mi_type= m_eventImpl->mi_type;
  m_ndb->theEventBuffer->add_op();
  int r= NdbDictionaryImpl::getImpl(*myDict).executeSubscribeEvent(*this);
  if (r == 0) {
526 527 528 529 530 531 532
    if (theMainOp == NULL) {
      DBUG_PRINT("info", ("execute blob ops"));
      NdbEventOperationImpl* blob_op = theBlobOpList;
      while (blob_op != NULL) {
        r = blob_op->execute_nolock();
        if (r != 0)
          break;
533
        blob_op = blob_op->m_next;
534 535 536 537
      }
    }
    if (r == 0)
      DBUG_RETURN(0);
538
  }
539 540 541 542
  //Error
  m_state= EO_ERROR;
  mi_type= 0;
  m_magic_number= 0;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
543
  m_error.code= myDict->getNdbError().code;
544
  m_ndb->theEventBuffer->remove_op();
545
  DBUG_RETURN(r);
546 547 548 549 550
}

int
NdbEventOperationImpl::stop()
{
551
  DBUG_ENTER("NdbEventOperationImpl::stop");
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  int i;

  for (i=0 ; i<2; i++) {
    NdbRecAttr *p = theFirstPkAttrs[i];
    while (p) {
      NdbRecAttr *p_next = p->next();
      m_ndb->releaseRecAttr(p);
      p = p_next;
    }
    theFirstPkAttrs[i]= 0;
  }
  for (i=0 ; i<2; i++) {
    NdbRecAttr *p = theFirstDataAttrs[i];
    while (p) {
      NdbRecAttr *p_next = p->next();
      m_ndb->releaseRecAttr(p);
      p = p_next;
    }
    theFirstDataAttrs[i]= 0;
  }

573
  if (m_state != EO_EXECUTING)
574
  {
575
    DBUG_RETURN(-1);
576
  }
577 578 579

  NdbDictionary::Dictionary *myDict = m_ndb->getDictionary();
  if (!myDict) {
580
    m_error.code= m_ndb->getNdbError().code;
581
    DBUG_RETURN(-1);
582 583
  }

584 585 586 587 588 589 590 591
  m_ndb->theEventBuffer->add_drop_lock();
  int r= NdbDictionaryImpl::getImpl(*myDict).stopSubscribeEvent(*this);
  m_ndb->theEventBuffer->remove_op();
  m_state= EO_DROPPED;
  mi_type= 0;
  if (r == 0) {
    m_ndb->theEventBuffer->add_drop_unlock();
    DBUG_RETURN(0);
592
  }
593 594 595 596
  //Error
  m_error.code= NdbDictionaryImpl::getImpl(*myDict).m_error.code;
  m_state= EO_ERROR;
  m_ndb->theEventBuffer->add_drop_unlock();
597
  DBUG_RETURN(r);
598 599
}

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
const bool NdbEventOperationImpl::tableNameChanged() const
{
  return (bool)AlterTableReq::getNameFlag(m_change_mask);
}

const bool NdbEventOperationImpl::tableFrmChanged() const
{
  return (bool)AlterTableReq::getFrmFlag(m_change_mask);
}

const bool NdbEventOperationImpl::tableFragmentationChanged() const
{
  return (bool)AlterTableReq::getFragDataFlag(m_change_mask);
}

const bool NdbEventOperationImpl::tableRangeListChanged() const
{
  return (bool)AlterTableReq::getRangeListFlag(m_change_mask);
}

620
Uint64
621 622
NdbEventOperationImpl::getGCI()
{
623
  return m_data_item->sdata->gci;
624 625
}

626
Uint64
627 628
NdbEventOperationImpl::getLatestGCI()
{
629
  return m_ndb->theEventBuffer->getLatestGCI();
630 631
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
bool
NdbEventOperationImpl::execSUB_TABLE_DATA(NdbApiSignal * signal, 
                                          LinearSectionPtr ptr[3])
{
  DBUG_ENTER("NdbEventOperationImpl::execSUB_TABLE_DATA");
  const SubTableData * const sdata=
    CAST_CONSTPTR(SubTableData, signal->getDataPtr());

  if(signal->isFirstFragment()){
    m_fragmentId = signal->getFragmentId();
    m_buffer.grow(4 * sdata->totalLen);
  } else {
    if(m_fragmentId != signal->getFragmentId()){
      abort();
    }
  }
  const Uint32 i = SubTableData::DICT_TAB_INFO;
  DBUG_PRINT("info", ("Accumulated %u bytes for fragment %u", 
                      4 * ptr[i].sz, m_fragmentId));
  m_buffer.append(ptr[i].p, 4 * ptr[i].sz);
  
  if(!signal->isLastFragment()){
    DBUG_RETURN(FALSE);
  }  
  
  DBUG_RETURN(TRUE);
}


661
int
662
NdbEventOperationImpl::receive_event()
663
{
664
  DBUG_ENTER_EVENT("NdbEventOperationImpl::receive_event");
665

666
  Uint32 operation= (Uint32)m_data_item->sdata->operation;
667
  DBUG_PRINT_EVENT("info",("sdata->operation %u",operation));
668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  if (operation == NdbDictionary::Event::_TE_ALTER)
  {
    // Parse the new table definition and
    // create a table object
    NdbDictionary::Dictionary *myDict = m_ndb->getDictionary();
    NdbDictionaryImpl *dict = & NdbDictionaryImpl::getImpl(*myDict);
    NdbError error;
    NdbDictInterface dif(error);
    NdbTableImpl *at;
    m_change_mask = m_data_item->sdata->changeMask;
    error.code = dif.parseTableInfo(&at,
                                    (Uint32*)m_buffer.get_data(), 
                                    m_buffer.length() / 4, 
                                    true);
    m_buffer.clear();
684 685 686 687 688 689 690 691
    if (at)
      at->buildColumnHash();
    else
    {
      DBUG_PRINT_EVENT("info", ("Failed to parse DictTabInfo error %u", 
                                error.code));
      DBUG_RETURN_EVENT(1);
    }
692 693

    NdbTableImpl *tmp_table_impl= m_eventImpl->m_tableImpl;
694
    m_eventImpl->m_tableImpl = at;
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

    DBUG_PRINT("info", ("switching table impl 0x%x -> 0x%x",
                        tmp_table_impl, at));

    // change the rec attrs to refer to the new table object
    int i;
    for (i = 0; i < 2; i++)
    {
      NdbRecAttr *p = theFirstPkAttrs[i];
      while (p)
      {
        int no = p->getColumn()->getColumnNo();
        NdbColumnImpl *tAttrInfo = at->getColumn(no);
        DBUG_PRINT("info", ("rec_attr: 0x%x  "
                            "switching column impl 0x%x -> 0x%x",
                            p, p->m_column, tAttrInfo));
        p->m_column = tAttrInfo;
        p = p->next();
      }
    }
    for (i = 0; i < 2; i++)
    {
      NdbRecAttr *p = theFirstDataAttrs[i];
      while (p)
      {
        int no = p->getColumn()->getColumnNo();
        NdbColumnImpl *tAttrInfo = at->getColumn(no);
        DBUG_PRINT("info", ("rec_attr: 0x%x  "
                            "switching column impl 0x%x -> 0x%x",
                            p, p->m_column, tAttrInfo));
        p->m_column = tAttrInfo;
        p = p->next();
      }
    }
    if (tmp_table_impl) 
      delete tmp_table_impl;
731 732
  }

733 734
  if (unlikely(operation >= NdbDictionary::Event::_TE_FIRST_NON_DATA_EVENT))
  {
735
    DBUG_RETURN_EVENT(1);
736
  }
737

738 739 740
  // now move the data into the RecAttrs
    
  int is_update= operation == NdbDictionary::Event::_TE_UPDATE;
741

742 743 744
  Uint32 *aAttrPtr = m_data_item->ptr[0].p;
  Uint32 *aAttrEndPtr = aAttrPtr + m_data_item->ptr[0].sz;
  Uint32 *aDataPtr = m_data_item->ptr[1].p;
745

746 747
  DBUG_DUMP_EVENT("after",(char*)m_data_item->ptr[1].p, m_data_item->ptr[1].sz*4);
  DBUG_DUMP_EVENT("before",(char*)m_data_item->ptr[2].p, m_data_item->ptr[2].sz*4);
748 749 750 751 752 753 754 755 756

  // copy data into the RecAttr's
  // we assume that the respective attribute lists are sorted

  // first the pk's
  {
    NdbRecAttr *tAttr= theFirstPkAttrs[0];
    NdbRecAttr *tAttr1= theFirstPkAttrs[1];
    while(tAttr)
757
    {
758
      assert(aAttrPtr < aAttrEndPtr);
759
      unsigned tDataSz= AttributeHeader(*aAttrPtr).getByteSize();
760 761 762 763 764
      assert(tAttr->attrId() ==
	     AttributeHeader(*aAttrPtr).getAttributeId());
      receive_data(tAttr, aDataPtr, tDataSz);
      if (is_update)
	receive_data(tAttr1, aDataPtr, tDataSz);
765 766 767
      else
        tAttr1->setUNDEFINED(); // do not leave unspecified
      tAttr1= tAttr1->next();
768 769
      // next
      aAttrPtr++;
770
      aDataPtr+= (tDataSz + 3) >> 2;
771
      tAttr= tAttr->next();
772
    }
773 774 775 776 777 778 779 780 781 782 783
  }
  
  NdbRecAttr *tWorkingRecAttr = theFirstDataAttrs[0];
  
  Uint32 tRecAttrId;
  Uint32 tAttrId;
  Uint32 tDataSz;
  int hasSomeData=0;
  while ((aAttrPtr < aAttrEndPtr) && (tWorkingRecAttr != NULL)) {
    tRecAttrId = tWorkingRecAttr->attrId();
    tAttrId = AttributeHeader(*aAttrPtr).getAttributeId();
784
    tDataSz = AttributeHeader(*aAttrPtr).getByteSize();
785 786
    
    while (tAttrId > tRecAttrId) {
787 788
      DBUG_PRINT_EVENT("info",("undef [%u] %u 0x%x [%u] 0x%x",
                               tAttrId, tDataSz, *aDataPtr, tRecAttrId, aDataPtr));
789 790
      tWorkingRecAttr->setUNDEFINED();
      tWorkingRecAttr = tWorkingRecAttr->next();
791 792
      if (tWorkingRecAttr == NULL)
	break;
793 794 795 796 797 798 799
      tRecAttrId = tWorkingRecAttr->attrId();
    }
    if (tWorkingRecAttr == NULL)
      break;
    
    if (tAttrId == tRecAttrId) {
      hasSomeData++;
800
      
801 802
      DBUG_PRINT_EVENT("info",("set [%u] %u 0x%x [%u] 0x%x",
                               tAttrId, tDataSz, *aDataPtr, tRecAttrId, aDataPtr));
803
      
804 805
      receive_data(tWorkingRecAttr, aDataPtr, tDataSz);
      tWorkingRecAttr = tWorkingRecAttr->next();
806
    }
807
    aAttrPtr++;
808
    aDataPtr += (tDataSz + 3) >> 2;
809
  }
810
    
811 812 813 814 815 816 817 818 819 820 821 822 823 824
  while (tWorkingRecAttr != NULL) {
    tRecAttrId = tWorkingRecAttr->attrId();
    //printf("set undefined [%u] %u %u [%u]\n",
    //       tAttrId, tDataSz, *aDataPtr, tRecAttrId);
    tWorkingRecAttr->setUNDEFINED();
    tWorkingRecAttr = tWorkingRecAttr->next();
  }
  
  tWorkingRecAttr = theFirstDataAttrs[1];
  aDataPtr = m_data_item->ptr[2].p;
  Uint32 *aDataEndPtr = aDataPtr + m_data_item->ptr[2].sz;
  while ((aDataPtr < aDataEndPtr) && (tWorkingRecAttr != NULL)) {
    tRecAttrId = tWorkingRecAttr->attrId();
    tAttrId = AttributeHeader(*aDataPtr).getAttributeId();
825
    tDataSz = AttributeHeader(*aDataPtr).getByteSize();
826 827
    aDataPtr++;
    while (tAttrId > tRecAttrId) {
828
      tWorkingRecAttr->setUNDEFINED();
829 830 831
      tWorkingRecAttr = tWorkingRecAttr->next();
      if (tWorkingRecAttr == NULL)
	break;
832
      tRecAttrId = tWorkingRecAttr->attrId();
833
    }
834 835 836 837 838 839 840
    if (tWorkingRecAttr == NULL)
      break;
    if (tAttrId == tRecAttrId) {
      assert(!m_eventImpl->m_tableImpl->getColumn(tRecAttrId)->getPrimaryKey());
      hasSomeData++;
      
      receive_data(tWorkingRecAttr, aDataPtr, tDataSz);
841 842
      tWorkingRecAttr = tWorkingRecAttr->next();
    }
843
    aDataPtr += (tDataSz + 3) >> 2;
844 845 846 847 848 849 850 851
  }
  while (tWorkingRecAttr != NULL) {
    tWorkingRecAttr->setUNDEFINED();
    tWorkingRecAttr = tWorkingRecAttr->next();
  }
  
  if (hasSomeData || !is_update)
  {
852
    DBUG_RETURN_EVENT(1);
853
  }
854

855
  DBUG_RETURN_EVENT(0);
856 857 858 859 860
}

NdbDictionary::Event::TableEvent 
NdbEventOperationImpl::getEventType()
{
861 862
  return (NdbDictionary::Event::TableEvent)
    (1 << (unsigned)m_data_item->sdata->operation);
863 864 865 866 867 868 869
}



void
NdbEventOperationImpl::print()
{
870
  int i;
871 872
  ndbout << "EventId " << m_eventId << "\n";

873
  for (i = 0; i < 2; i++) {
874 875 876 877 878 879 880 881
    NdbRecAttr *p = theFirstPkAttrs[i];
    ndbout << " %u " << i;
    while (p) {
      ndbout << " : " << p->attrId() << " = " << *p;
      p = p->next();
    }
    ndbout << "\n";
  }
882
  for (i = 0; i < 2; i++) {
883
    NdbRecAttr *p = theFirstDataAttrs[i];
884 885
    ndbout << " %u " << i;
    while (p) {
886
      ndbout << " : " << p->attrId() << " = " << *p;
887 888 889 890 891 892 893 894 895
      p = p->next();
    }
    ndbout << "\n";
  }
}

void
NdbEventOperationImpl::printAll()
{
896 897 898
  Uint32 *aAttrPtr = m_data_item->ptr[0].p;
  Uint32 *aAttrEndPtr = aAttrPtr + m_data_item->ptr[0].sz;
  Uint32 *aDataPtr = m_data_item->ptr[1].p;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

  //tRecAttr->setup(tAttrInfo, aValue)) {

  Uint32 tAttrId;
  Uint32 tDataSz;
  for (; aAttrPtr < aAttrEndPtr; ) {
    tAttrId = AttributeHeader(*aAttrPtr).getAttributeId();
    tDataSz = AttributeHeader(*aAttrPtr).getDataSize();

    aAttrPtr++;
    aDataPtr += tDataSz;
  }
}

/*
914 915
 * Class NdbEventBuffer
 * Each Ndb object has a Object.
916 917
 */

918 919
// ToDo ref count this so it get's destroyed
NdbMutex *NdbEventBuffer::p_add_drop_mutex= 0;
920

921 922 923 924 925 926
NdbEventBuffer::NdbEventBuffer(Ndb *ndb) :
  m_system_nodes(ndb->theImpl->theNoOfDBnodes),
  m_ndb(ndb),
  m_latestGCI(0),
  m_total_alloc(0),
  m_free_thresh(10),
927 928
  m_min_free_thresh(10),
  m_max_free_thresh(100),
929 930 931
  m_gci_slip_thresh(3),
  m_dropped_ev_op(0),
  m_active_op_count(0)
932
{
933 934 935
#ifdef VM_TRACE
  m_latest_command= "NdbEventBuffer::NdbEventBuffer";
#endif
936 937

  if ((p_cond = NdbCondition_Create()) ==  NULL) {
938
    ndbout_c("NdbEventHandle: NdbCondition_Create() failed");
939 940
    exit(-1);
  }
941 942 943 944 945 946 947
  m_mutex= ndb->theImpl->theWaiter.m_mutex;
  lock();
  if (p_add_drop_mutex == 0)
  {
    if ((p_add_drop_mutex = NdbMutex_Create()) == NULL) {
      ndbout_c("NdbEventBuffer: NdbMutex_Create() failed");
      exit(-1);
948 949
    }
  }
950
  unlock();
951

952 953 954 955 956 957 958 959 960 961 962 963
  // ToDo set event buffer size
  // pre allocate event data array
  m_sz= 0;
#ifdef VM_TRACE
  m_free_data_count= 0;
#endif
  m_free_data= 0;
  m_free_data_sz= 0;

  // initialize lists
  bzero(&g_empty_gci_container, sizeof(Gci_container));
  init_gci_containers();
964 965
}

966
NdbEventBuffer::~NdbEventBuffer()
967
{
968 969 970 971 972 973 974 975 976 977 978 979 980
  // todo lock?  what if receive thread writes here?
  for (unsigned j= 0; j < m_allocated_data.size(); j++)
  {
    unsigned sz= m_allocated_data[j]->sz;
    EventBufData *data= m_allocated_data[j]->data;
    EventBufData *end_data= data+sz;
    for (; data < end_data; data++)
    {
      if (data->sdata)
	NdbMem_Free(data->sdata);
    }
    NdbMem_Free((char*)m_allocated_data[j]);
  }
981

982
  NdbCondition_Destroy(p_cond);
983

984 985 986 987 988
  lock();
  if (p_add_drop_mutex)
  {
    NdbMutex_Destroy(p_add_drop_mutex);
    p_add_drop_mutex = 0;
989
  }
990
  unlock();
991 992 993
}

void
994
NdbEventBuffer::add_op()
995
{
996 997 998
  if(m_active_op_count == 0)
  {
    init_gci_containers();
999
  }
1000
  m_active_op_count++;
1001 1002 1003
}

void
1004
NdbEventBuffer::remove_op()
1005
{
1006
  m_active_op_count--;
1007
}
1008

1009
void
1010
NdbEventBuffer::init_gci_containers()
1011
{
1012 1013 1014 1015
  bzero(&m_complete_data, sizeof(m_complete_data));
  m_latest_complete_GCI = m_latestGCI = 0;
  m_active_gci.clear();
  m_active_gci.fill(2 * ACTIVE_GCI_DIRECTORY_SIZE - 1, g_empty_gci_container);
1016 1017
}

1018
int NdbEventBuffer::expand(unsigned sz)
1019
{
1020 1021 1022 1023
  unsigned alloc_size=
    sizeof(EventBufData_chunk) +(sz-1)*sizeof(EventBufData);
  EventBufData_chunk *chunk_data=
    (EventBufData_chunk *)NdbMem_Allocate(alloc_size);
1024

1025 1026
  chunk_data->sz= sz;
  m_allocated_data.push_back(chunk_data);
1027

1028 1029 1030
  EventBufData *data= chunk_data->data;
  EventBufData *end_data= data+sz;
  EventBufData *last_data= m_free_data;
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  bzero((void*)data, sz*sizeof(EventBufData));
  for (; data < end_data; data++)
  {
    data->m_next= last_data;
    last_data= data;
  }
  m_free_data= last_data;

  m_sz+= sz;
#ifdef VM_TRACE
  m_free_data_count+= sz;
#endif
  return 0;
1045 1046 1047
}

int
1048
NdbEventBuffer::pollEvents(int aMillisecondNumber, Uint64 *latestGCI)
1049
{
1050 1051 1052 1053 1054 1055 1056 1057
  int ret= 1;
#ifdef VM_TRACE
  const char *m_latest_command_save= m_latest_command;
  m_latest_command= "NdbEventBuffer::pollEvents";
#endif

  NdbMutex_Lock(m_mutex);
  NdbEventOperationImpl *ev_op= move_data();
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1058
  if (unlikely(ev_op == 0 && aMillisecondNumber))
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  {
    NdbCondition_WaitTimeout(p_cond, m_mutex, aMillisecondNumber);
    ev_op= move_data();
    if (unlikely(ev_op == 0))
      ret= 0;
  }
  if (latestGCI)
    *latestGCI= m_latestGCI;
#ifdef VM_TRACE
  if (ev_op)
  {
    // m_mutex is locked
    // update event ops data counters
    ev_op->m_data_count-= ev_op->m_data_done_count;
    ev_op->m_data_done_count= 0;
  }
  m_latest_command= m_latest_command_save;
#endif
  NdbMutex_Unlock(m_mutex); // we have moved the data
  return ret;
1079 1080
}

1081 1082
NdbEventOperation *
NdbEventBuffer::nextEvent()
1083
{
1084
  DBUG_ENTER_EVENT("NdbEventBuffer::nextEvent");
1085 1086 1087
#ifdef VM_TRACE
  const char *m_latest_command_save= m_latest_command;
#endif
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  if (m_used_data.m_count > 1024)
  {
#ifdef VM_TRACE
    m_latest_command= "NdbEventBuffer::nextEvent (lock)";
#endif
    NdbMutex_Lock(m_mutex);
    // return m_used_data to m_free_data
    free_list(m_used_data);

    NdbMutex_Unlock(m_mutex);
  }
#ifdef VM_TRACE
  m_latest_command= "NdbEventBuffer::nextEvent";
#endif

  EventBufData *data;
  while ((data= m_available_data.m_head))
  {
    NdbEventOperationImpl *op= data->m_event_op;
1108
    DBUG_PRINT_EVENT("info", ("available data=%p op=%p", data, op));
1109

1110 1111 1112
    // blob table ops must not be seen at this level
    assert(op->theMainOp == NULL);

1113 1114 1115 1116 1117
    // set NdbEventOperation data
    op->m_data_item= data;

    // remove item from m_available_data
    m_available_data.remove_first();
1118

1119 1120 1121 1122 1123 1124 1125
    // add it to used list
    m_used_data.append(data);

#ifdef VM_TRACE
    op->m_data_done_count++;
#endif

1126 1127
    // NUL event is not returned
    if (data->sdata->operation == NdbDictionary::Event::_TE_NUL)
1128 1129
    {
      DBUG_PRINT_EVENT("info", ("skip _TE_NUL"));
1130
      continue;
1131
    }
1132

1133 1134 1135 1136 1137 1138 1139 1140
    int r= op->receive_event();
    if (r > 0)
    {
      if (op->m_state == NdbEventOperation::EO_EXECUTING)
      {
#ifdef VM_TRACE
	m_latest_command= m_latest_command_save;
#endif
1141 1142 1143 1144 1145 1146
        NdbBlob* tBlob = op->theBlobList;
        while (tBlob != NULL)
        {
          (void)tBlob->atNextEvent();
          tBlob = tBlob->theNext;
        }
1147
	DBUG_RETURN_EVENT(op->m_facade);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
      }
      // the next event belonged to an event op that is no
      // longer valid, skip to next
      continue;
    }
#ifdef VM_TRACE
    m_latest_command= m_latest_command_save;
#endif
  }
  m_error.code= 0;
#ifdef VM_TRACE
  m_latest_command= m_latest_command_save;
#endif
1161
  DBUG_RETURN_EVENT(0);
1162
}
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
NdbEventOperationImpl*
NdbEventBuffer::getGCIEventOperations(Uint32* iter, Uint32* event_types)
{
  if (*iter < m_available_data.m_gci_op_count)
  {
    EventBufData_list::Gci_op g = m_available_data.m_gci_op_list[(*iter)++];
    if (event_types != NULL)
      *event_types = g.event_types;
    return g.op;
  }
  return NULL;
}

1177
void
1178
NdbEventBuffer::lock()
1179
{
1180
  NdbMutex_Lock(m_mutex);
1181 1182
}
void
1183
NdbEventBuffer::unlock()
1184
{
1185
  NdbMutex_Unlock(m_mutex);
1186 1187
}
void
1188
NdbEventBuffer::add_drop_lock()
1189 1190 1191 1192
{
  NdbMutex_Lock(p_add_drop_mutex);
}
void
1193
NdbEventBuffer::add_drop_unlock()
1194 1195 1196 1197
{
  NdbMutex_Unlock(p_add_drop_mutex);
}

1198 1199 1200
static
NdbOut&
operator<<(NdbOut& out, const Gci_container& gci)
1201
{
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
  out << "[ GCI: " << gci.m_gci
      << "  state: " << hex << gci.m_state 
      << "  head: " << hex << gci.m_data.m_head
      << "  tail: " << hex << gci.m_data.m_tail
#ifdef VM_TRACE
      << "  cnt: " << dec << gci.m_data.m_count
#endif
      << " gcp: " << dec << gci.m_gcp_complete_rep_count 
      << "]";
  return out;
1212 1213
}

1214 1215 1216
static
Gci_container*
find_bucket_chained(Vector<Gci_container> * active, Uint64 gci)
1217
{
1218 1219
  Uint32 pos = (gci & ACTIVE_GCI_MASK);
  Gci_container *bucket= active->getBase() + pos;
1220

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  if(gci > bucket->m_gci)
  {
    Gci_container* move;
    Uint32 move_pos = pos + ACTIVE_GCI_DIRECTORY_SIZE;
    do 
    {
      active->fill(move_pos, g_empty_gci_container);
      bucket = active->getBase() + pos; // Needs to recomputed after fill
      move = active->getBase() + move_pos;
      if(move->m_gcp_complete_rep_count == 0)
      {
	memcpy(move, bucket, sizeof(Gci_container));
	bzero(bucket, sizeof(Gci_container));
	bucket->m_gci = gci;
	bucket->m_gcp_complete_rep_count = ~(Uint32)0;
	return bucket;
      }
      move_pos += ACTIVE_GCI_DIRECTORY_SIZE;
    } while(true);
  }
  else /** gci < bucket->m_gci */
  {
    Uint32 size = active->size() - ACTIVE_GCI_DIRECTORY_SIZE;
    do 
    {
      pos += ACTIVE_GCI_DIRECTORY_SIZE;
      bucket += ACTIVE_GCI_DIRECTORY_SIZE;
      
      if(bucket->m_gci == gci)
	return bucket;
      
    } while(pos < size);
    
    return 0;
1255 1256 1257
  }
}

1258 1259 1260
inline
Gci_container*
find_bucket(Vector<Gci_container> * active, Uint64 gci)
1261
{
1262 1263 1264 1265 1266 1267
  Uint32 pos = (gci & ACTIVE_GCI_MASK);
  Gci_container *bucket= active->getBase() + pos;
  if(likely(gci == bucket->m_gci))
    return bucket;

  return find_bucket_chained(active,gci);
1268
}
1269

1270
void
1271
NdbEventBuffer::execSUB_GCP_COMPLETE_REP(const SubGcpCompleteRep * const rep)
1272
{
1273 1274
  if (unlikely(m_active_op_count == 0))
  {
1275
    return;
1276 1277
  }
  
1278
  DBUG_ENTER_EVENT("NdbEventBuffer::execSUB_GCP_COMPLETE_REP");
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

  const Uint64 gci= rep->gci;
  const Uint32 cnt= rep->gcp_complete_rep_count;

  Gci_container *bucket = find_bucket(&m_active_gci, gci);

  if (unlikely(bucket == 0))
  {
    /**
     * Already completed GCI...
     *   Possible in case of resend during NF handling
     */
1291
#ifdef VM_TRACE
1292 1293 1294 1295 1296
    ndbout << "bucket == 0, gci:" << gci
	   << " complete: " << m_complete_data << endl;
    for(Uint32 i = 0; i<m_active_gci.size(); i++)
    {
      ndbout << i << " - " << m_active_gci[i] << endl;
1297
    }
1298
#endif
1299
    DBUG_VOID_RETURN_EVENT;
1300
  }
1301 1302 1303

  Uint32 old_cnt = bucket->m_gcp_complete_rep_count;
  if(unlikely(old_cnt == ~(Uint32)0))
1304
  {
1305 1306 1307 1308 1309 1310 1311 1312 1313
    old_cnt = m_system_nodes;
  }
  
  assert(old_cnt >= cnt);
  bucket->m_gcp_complete_rep_count = old_cnt - cnt;

  if(old_cnt == cnt)
  {
    if(likely(gci == m_latestGCI + 1 || m_latestGCI == 0))
1314
    {
1315 1316
      m_latestGCI = m_complete_data.m_gci = gci; // before reportStatus
      if(!bucket->m_data.is_empty())
1317
      {
1318 1319 1320 1321
#ifdef VM_TRACE
	assert(bucket->m_data.m_count);
#endif
	m_complete_data.m_data.append(bucket->m_data);
1322
      }
1323
      reportStatus();
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
      bzero(bucket, sizeof(Gci_container));
      bucket->m_gci = gci + ACTIVE_GCI_DIRECTORY_SIZE;
      bucket->m_gcp_complete_rep_count = m_system_nodes;
      if(unlikely(m_latest_complete_GCI > gci))
      {
	complete_outof_order_gcis();
      }

      // signal that somethings happened

      NdbCondition_Signal(p_cond);
    }
    else
    {
      /** out of order something */
      ndbout_c("out of order bucket: %d gci: %lld m_latestGCI: %lld", 
	       bucket-m_active_gci.getBase(), gci, m_latestGCI);
      bucket->m_state = Gci_container::GC_COMPLETE;
      bucket->m_gcp_complete_rep_count = 1; // Prevent from being reused
      m_latest_complete_GCI = gci;
1344 1345
    }
  }
1346
  
1347
  DBUG_VOID_RETURN_EVENT;
1348 1349
}

1350 1351
void
NdbEventBuffer::complete_outof_order_gcis()
1352
{
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
  Uint64 start_gci = m_latestGCI + 1;
  Uint64 stop_gci = m_latest_complete_GCI;
  
  const Uint32 size = m_active_gci.size();
  Gci_container* array= m_active_gci.getBase();
  
  ndbout_c("complete_outof_order_gcis");
  for(Uint32 i = 0; i<size; i++)
  {
    ndbout << i << " - " << array[i] << endl;
1363
  }
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
  
  for(; start_gci <= stop_gci; start_gci++)
  {
    /**
     * Find gci
     */
    Uint32 i;
    Gci_container* bucket= 0;
    for(i = 0; i<size; i++)
    {
      Gci_container* tmp = array + i;
      if(tmp->m_gci == start_gci && tmp->m_state == Gci_container::GC_COMPLETE)
      {
	bucket= tmp;
	break;
1379 1380
      }
    }
1381 1382 1383
    if(bucket == 0)
    {
      break;
1384 1385
    }

1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    printf("complete_outof_order_gcis - completing %lld", start_gci);
    if(!bucket->m_data.is_empty())
    {
#ifdef VM_TRACE
      assert(bucket->m_data.m_count);
#endif
      m_complete_data.m_data.append(bucket->m_data);
#ifdef VM_TRACE
      ndbout_c(" moved %lld rows -> %lld", bucket->m_data.m_count,
	       m_complete_data.m_data.m_count);
#else
      ndbout_c("");
#endif
1399
    }
1400 1401 1402 1403 1404
    bzero(bucket, sizeof(Gci_container));
    if(i < ACTIVE_GCI_DIRECTORY_SIZE)
    {
      bucket->m_gci = start_gci + ACTIVE_GCI_DIRECTORY_SIZE;
      bucket->m_gcp_complete_rep_count = m_system_nodes;
1405
    }
1406 1407
    
    m_latestGCI = m_complete_data.m_gci = start_gci;
1408
  }
1409 1410
  
  ndbout_c("complete_outof_order_gcis: m_latestGCI: %lld", m_latestGCI);
1411 1412
}

tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
void
NdbEventBuffer::report_node_failure(Uint32 node_id)
{
  DBUG_ENTER("NdbEventBuffer::report_node_failure");
  SubTableData data;
  LinearSectionPtr ptr[3];
  bzero(&data, sizeof(data));
  bzero(ptr, sizeof(ptr));

  data.tableId = ~0;
  data.operation = NdbDictionary::Event::_TE_NODE_FAILURE;
  data.req_nodeid = (Uint8)node_id;
  data.ndbd_nodeid = (Uint8)node_id;
  data.logType = SubTableData::LOG;
  /**
   * Insert this event for each operation
   */
  NdbEventOperation* op= 0;
  while((op = m_ndb->getEventOperation(op)))
  {
    NdbEventOperationImpl* impl= &op->m_impl;
    data.senderData = impl->m_oid;
    insertDataL(impl, &data, ptr); 
  }
  DBUG_VOID_RETURN;
}

1440
void
1441
NdbEventBuffer::completeClusterFailed()
1442
{
1443
  DBUG_ENTER("NdbEventBuffer::completeClusterFailed");
1444

1445 1446 1447 1448
  SubTableData data;
  LinearSectionPtr ptr[3];
  bzero(&data, sizeof(data));
  bzero(ptr, sizeof(ptr));
1449

1450 1451 1452
  data.tableId = ~0;
  data.operation = NdbDictionary::Event::_TE_CLUSTER_FAILURE;
  data.logType = SubTableData::LOG;
1453

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
  /**
   * Find min not completed GCI
   */
  Uint32 sz= m_active_gci.size();
  Uint64 gci= ~0;
  Gci_container* bucket = 0;
  Gci_container* array = m_active_gci.getBase();
  for(Uint32 i = 0; i<sz; i++)
  {
    if(array[i].m_gcp_complete_rep_count && array[i].m_gci < gci)
    {
      bucket= array + i;
      gci = bucket->m_gci;
1467 1468 1469
    }
  }

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
  if(bucket == 0)
  {
    /**
     * Did not find any not completed GCI's
     *   lets fake one...
     */
    gci = m_latestGCI + 1;
    bucket = array + ( gci & ACTIVE_GCI_MASK );
    bucket->m_gcp_complete_rep_count = 1;
  }
  
  const Uint32 cnt= bucket->m_gcp_complete_rep_count = 1; 
1482

1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
  /**
   * Release all GCI's
   */
  for(Uint32 i = 0; i<sz; i++)
  {
    Gci_container* tmp = array + i;
    if(!tmp->m_data.is_empty())
    {
      free_list(tmp->m_data);
#if 0
      m_free_data_count++;
      EventBufData* loop= tmp->m_head;
      while(loop != tmp->m_tail)
      {
	m_free_data_count++;
	loop = loop->m_next;
      }
#endif
    }
    bzero(tmp, sizeof(Gci_container));
  }
  
  bucket->m_gci = gci;
  bucket->m_gcp_complete_rep_count = cnt;
  
  data.gci = gci;
  
  /**
   * Insert this event for each operation
   */
  NdbEventOperation* op= 0;
  while((op = m_ndb->getEventOperation(op)))
  {
    NdbEventOperationImpl* impl= &op->m_impl;
    data.senderData = impl->m_oid;
    insertDataL(impl, &data, ptr); 
  }
  
  /**
   * And finally complete this GCI
   */
  SubGcpCompleteRep rep;
  rep.gci= gci;
  rep.gcp_complete_rep_count= cnt;
  execSUB_GCP_COMPLETE_REP(&rep);
1528

1529
  DBUG_VOID_RETURN;
1530 1531
}

1532 1533
Uint64
NdbEventBuffer::getLatestGCI()
1534
{
1535
  return m_latestGCI;
1536 1537
}

1538 1539 1540 1541
int
NdbEventBuffer::insertDataL(NdbEventOperationImpl *op,
			    const SubTableData * const sdata, 
			    LinearSectionPtr ptr[3])
1542
{
1543
  DBUG_ENTER_EVENT("NdbEventBuffer::insertDataL");
1544
  Uint64 gci= sdata->gci;
1545

1546
  if ( likely((Uint32)op->mi_type & (1 << (Uint32)sdata->operation)) )
1547
  {
1548 1549
    Gci_container* bucket= find_bucket(&m_active_gci, gci);
      
1550 1551 1552 1553
    DBUG_PRINT_EVENT("info", ("data insertion in eventId %d", op->m_eventId));
    DBUG_PRINT_EVENT("info", ("gci=%d tab=%d op=%d node=%d",
                              sdata->gci, sdata->tableId, sdata->operation,
                              sdata->req_nodeid));
1554

1555 1556 1557 1558 1559 1560
    if (unlikely(bucket == 0))
    {
      /**
       * Already completed GCI...
       *   Possible in case of resend during NF handling
       */
1561
      DBUG_RETURN_EVENT(0);
1562
    }
1563
    
1564
    const bool is_blob_event = (op->theMainOp != NULL);
1565
    const bool is_data_event =
1566
      sdata->operation < NdbDictionary::Event::_TE_FIRST_NON_DATA_EVENT;
1567
    const bool use_hash =  op->m_mergeEvents && is_data_event;
1568

1569 1570 1571 1572 1573 1574 1575
    if (! is_data_event && is_blob_event)
    {
      // currently subscribed to but not used
      DBUG_PRINT_EVENT("info", ("ignore non-data event on blob table"));
      DBUG_RETURN_EVENT(0);
    }

1576 1577 1578 1579
    // find position in bucket hash table
    EventBufData* data = 0;
    EventBufData_hash::Pos hpos;
    if (use_hash)
1580
    {
1581 1582 1583
      bucket->m_data_hash.search(hpos, op, ptr);
      data = hpos.data;
    }
1584

1585 1586 1587 1588
    if (data == 0)
    {
      // allocate new result buffer
      data = alloc_data();
1589 1590
      if (unlikely(data == 0))
      {
1591
        op->m_has_error = 2;
1592
        DBUG_RETURN_EVENT(-1);
1593
      }
1594 1595 1596
      if (unlikely(copy_data(sdata, ptr, data)))
      {
        op->m_has_error = 3;
1597
        DBUG_RETURN_EVENT(-1);
1598
      }
1599
      data->m_event_op = op;
1600
      if (! is_blob_event || ! is_data_event)
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
      {
        bucket->m_data.append(data);
      }
      else
      {
        // find or create main event for this blob event
        EventBufData_hash::Pos main_hpos;
        int ret = get_main_data(bucket, main_hpos, data);
        if (ret == -1)
        {
          op->m_has_error = 4;
          DBUG_RETURN_EVENT(-1);
        }
        EventBufData* main_data = main_hpos.data;
        if (ret != 0) // main event was created
        {
          main_data->m_event_op = op->theMainOp;
          bucket->m_data.append(main_data);
          if (use_hash)
          {
            main_data->m_pkhash = main_hpos.pkhash;
            bucket->m_data_hash.append(main_hpos, main_data);
          }
        }
        // link blob event under main event
        add_blob_data(main_data, data);
      }
1628 1629
      if (use_hash)
      {
1630
        data->m_pkhash = hpos.pkhash;
1631 1632
        bucket->m_data_hash.append(hpos, data);
      }
1633
#ifdef VM_TRACE
1634
      op->m_data_count++;
1635
#endif
1636 1637
    }
    else
1638
    {
1639 1640 1641 1642
      // event with same op, PK found, merge into old buffer
      if (unlikely(merge_data(sdata, ptr, data)))
      {
        op->m_has_error = 3;
1643
        DBUG_RETURN_EVENT(-1);
1644 1645
      }
    }
1646
    DBUG_RETURN_EVENT(0);
1647
  }
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1648

1649
#ifdef VM_TRACE
1650
  if ((Uint32)op->m_eventImpl->mi_type & (1 << (Uint32)sdata->operation))
1651
  {
1652 1653
    DBUG_PRINT_EVENT("info",("Data arrived before ready eventId", op->m_eventId));
    DBUG_RETURN_EVENT(0);
1654 1655
  }
  else {
1656 1657
    DBUG_PRINT_EVENT("info",("skipped"));
    DBUG_RETURN_EVENT(0);
1658 1659
  }
#else
1660
  DBUG_RETURN_EVENT(0);
1661
#endif
1662 1663
}

1664 1665 1666
// allocate EventBufData
EventBufData*
NdbEventBuffer::alloc_data()
1667
{
1668
  DBUG_ENTER_EVENT("alloc_data");
1669 1670 1671
  EventBufData* data = m_free_data;

  if (unlikely(data == 0))
1672
  {
1673 1674 1675 1676 1677 1678 1679 1680 1681
#ifdef VM_TRACE
    assert(m_free_data_count == 0);
    assert(m_free_data_sz == 0);
#endif
    expand(4000);
    reportStatus();

    data = m_free_data;
    if (unlikely(data == 0))
1682 1683
    {
#ifdef VM_TRACE
1684 1685 1686 1687 1688 1689 1690 1691 1692
      printf("m_latest_command: %s\n", m_latest_command);
      printf("no free data, m_latestGCI %lld\n",
             m_latestGCI);
      printf("m_free_data_count %d\n", m_free_data_count);
      printf("m_available_data_count %d first gci %d last gci %d\n",
             m_available_data.m_count,
             m_available_data.m_head ? m_available_data.m_head->sdata->gci : 0,
             m_available_data.m_tail ? m_available_data.m_tail->sdata->gci : 0);
      printf("m_used_data_count %d\n", m_used_data.m_count);
1693
#endif
1694
      DBUG_RETURN_EVENT(0); // TODO handle this, overrun, or, skip?
1695 1696
    }
  }
1697 1698 1699 1700 1701 1702 1703 1704 1705

  // remove data from free list
  m_free_data = data->m_next;
  data->m_next = 0;
#ifdef VM_TRACE
  m_free_data_count--;
  assert(m_free_data_sz >= data->sz);
#endif
  m_free_data_sz -= data->sz;
1706
  DBUG_RETURN_EVENT(data);
1707 1708 1709 1710 1711 1712 1713
}

// allocate initial or bigger memory area in EventBufData
// takes sizes from given ptr and sets up data->ptr
int
NdbEventBuffer::alloc_mem(EventBufData* data, LinearSectionPtr ptr[3])
{
1714 1715
  DBUG_ENTER("NdbEventBuffer::alloc_mem");
  DBUG_PRINT("info", ("ptr sz %u + %u + %u", ptr[0].sz, ptr[1].sz, ptr[2].sz));
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
  const Uint32 min_alloc_size = 128;

  Uint32 sz4 = (sizeof(SubTableData) + 3) >> 2;
  Uint32 alloc_size = (sz4 + ptr[0].sz + ptr[1].sz + ptr[2].sz) << 2;
  if (alloc_size < min_alloc_size)
    alloc_size = min_alloc_size;

  if (data->sz < alloc_size)
  {
    NdbMem_Free((char*)data->memory);
    assert(m_total_alloc >= data->sz);
    m_total_alloc -= data->sz;
    data->memory = 0;
    data->sz = 0;

    data->memory = (Uint32*)NdbMem_Allocate(alloc_size);
    if (data->memory == 0)
1733
      DBUG_RETURN(-1);
1734 1735 1736 1737 1738 1739 1740 1741
    data->sz = alloc_size;
    m_total_alloc += data->sz;
  }

  Uint32* memptr = data->memory;
  memptr += sz4;
  int i;
  for (i = 0; i <= 2; i++)
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1742
  {
1743 1744 1745
    data->ptr[i].p = memptr;
    data->ptr[i].sz = ptr[i].sz;
    memptr += ptr[i].sz;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
1746 1747
  }

1748
  DBUG_RETURN(0);
1749 1750 1751 1752 1753 1754 1755
}

int 
NdbEventBuffer::copy_data(const SubTableData * const sdata,
                          LinearSectionPtr ptr[3],
                          EventBufData* data)
{
1756
  DBUG_ENTER_EVENT("NdbEventBuffer::copy_data");
1757

1758
  if (alloc_mem(data, ptr) != 0)
1759
    DBUG_RETURN_EVENT(-1);
1760 1761 1762 1763
  memcpy(data->sdata, sdata, sizeof(SubTableData));
  int i;
  for (i = 0; i <= 2; i++)
    memcpy(data->ptr[i].p, ptr[i].p, ptr[i].sz << 2);
1764
  DBUG_RETURN_EVENT(0);
1765
}
1766

1767 1768
static struct Ev_t {
  enum {
1769 1770 1771 1772 1773
    enum_INS = NdbDictionary::Event::_TE_INSERT,
    enum_DEL = NdbDictionary::Event::_TE_DELETE,
    enum_UPD = NdbDictionary::Event::_TE_UPDATE,
    enum_NUL = NdbDictionary::Event::_TE_NUL,
    enum_ERR = 255
1774 1775 1776
  };
  int t1, t2, t3;
} ev_t[] = {
1777 1778 1779 1780 1781 1782 1783 1784 1785
  { Ev_t::enum_INS, Ev_t::enum_INS, Ev_t::enum_ERR },
  { Ev_t::enum_INS, Ev_t::enum_DEL, Ev_t::enum_NUL }, //ok
  { Ev_t::enum_INS, Ev_t::enum_UPD, Ev_t::enum_INS }, //ok
  { Ev_t::enum_DEL, Ev_t::enum_INS, Ev_t::enum_UPD }, //ok
  { Ev_t::enum_DEL, Ev_t::enum_DEL, Ev_t::enum_ERR },
  { Ev_t::enum_DEL, Ev_t::enum_UPD, Ev_t::enum_ERR },
  { Ev_t::enum_UPD, Ev_t::enum_INS, Ev_t::enum_ERR },
  { Ev_t::enum_UPD, Ev_t::enum_DEL, Ev_t::enum_DEL }, //ok
  { Ev_t::enum_UPD, Ev_t::enum_UPD, Ev_t::enum_UPD }  //ok
1786
};
1787

1788 1789 1790 1791 1792 1793
/*
 *   | INS            | DEL              | UPD
 * 0 | pk ah + all ah | pk ah            | pk ah + new ah 
 * 1 | pk ad + all ad | old pk ad        | new pk ad + new ad 
 * 2 | empty          | old non-pk ah+ad | old ah+ad
 */
1794

1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
static AttributeHeader
copy_head(Uint32& i1, Uint32* p1, Uint32& i2, const Uint32* p2,
          Uint32 flags)
{
  AttributeHeader ah(p2[i2]);
  bool do_copy = (flags & 1);
  if (do_copy)
    p1[i1] = p2[i2];
  i1++;
  i2++;
  return ah;
}
1807

1808 1809 1810 1811 1812 1813 1814 1815 1816
static void
copy_attr(AttributeHeader ah,
          Uint32& j1, Uint32* p1, Uint32& j2, const Uint32* p2,
          Uint32 flags)
{
  bool do_copy = (flags & 1);
  bool with_head = (flags & 2);
  Uint32 n = with_head + ah.getDataSize();
  if (do_copy)
1817
  {
1818 1819
    Uint32 k;
    for (k = 0; k < n; k++)
1820
      p1[j1 + k] = p2[j2 + k];
1821
  }
1822 1823
  j1 += n;
  j2 += n;
1824 1825 1826 1827 1828 1829 1830
}

int 
NdbEventBuffer::merge_data(const SubTableData * const sdata,
                           LinearSectionPtr ptr2[3],
                           EventBufData* data)
{
1831
  DBUG_ENTER_EVENT("NdbEventBuffer::merge_data");
1832 1833 1834 1835 1836

  Uint32 nkey = data->m_event_op->m_eventImpl->m_tableImpl->m_noOfKeys;

  int t1 = data->sdata->operation;
  int t2 = sdata->operation;
1837
  if (t1 == Ev_t::enum_NUL)
1838
    DBUG_RETURN_EVENT(copy_data(sdata, ptr2, data));
1839 1840 1841 1842 1843 1844 1845 1846 1847

  Ev_t* tp = 0;
  int i;
  for (i = 0; i < sizeof(ev_t)/sizeof(ev_t[0]); i++) {
    if (ev_t[i].t1 == t1 && ev_t[i].t2 == t2) {
      tp = &ev_t[i];
      break;
    }
  }
1848
  assert(tp != 0 && tp->t3 != Ev_t::enum_ERR);
1849 1850 1851 1852 1853 1854 1855

  // save old data
  EventBufData olddata = *data;
  data->memory = 0;
  data->sz = 0;

  // compose ptr1 o ptr2 = ptr
1856 1857
  LinearSectionPtr (&ptr1)[3] = olddata.ptr;
  LinearSectionPtr (&ptr)[3] = data->ptr;
1858 1859 1860 1861 1862 1863 1864 1865

  // loop twice where first loop only sets sizes
  int loop;
  for (loop = 0; loop <= 1; loop++)
  {
    if (loop == 1)
    {
      if (alloc_mem(data, ptr) != 0)
1866
        DBUG_RETURN_EVENT(-1);
1867 1868 1869 1870
      *data->sdata = *sdata;
      data->sdata->operation = tp->t3;
    }

1871
    ptr[0].sz = ptr[1].sz = ptr[2].sz = 0;
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

    // copy pk from new version
    {
      AttributeHeader ah;
      Uint32 i = 0;
      Uint32 j = 0;
      Uint32 i2 = 0;
      Uint32 j2 = 0;
      while (i < nkey)
      {
        ah = copy_head(i, ptr[0].p, i2, ptr2[0].p, loop);
        copy_attr(ah, j, ptr[1].p, j2, ptr2[1].p, loop);
      }
      ptr[0].sz = i;
      ptr[1].sz = j;
    }

    // merge after values, new version overrides
1890
    if (tp->t3 != Ev_t::enum_DEL)
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
    {
      AttributeHeader ah;
      Uint32 i = ptr[0].sz;
      Uint32 j = ptr[1].sz;
      Uint32 i1 = 0;
      Uint32 j1 = 0;
      Uint32 i2 = nkey;
      Uint32 j2 = ptr[1].sz;
      while (i1 < nkey)
      {
        j1 += AttributeHeader(ptr1[0].p[i1++]).getDataSize();
      }
      while (1)
      {
        bool b1 = (i1 < ptr1[0].sz);
        bool b2 = (i2 < ptr2[0].sz);
        if (b1 && b2)
        {
          Uint32 id1 = AttributeHeader(ptr1[0].p[i1]).getAttributeId();
          Uint32 id2 = AttributeHeader(ptr2[0].p[i2]).getAttributeId();
          if (id1 < id2)
            b2 = false;
          else if (id1 > id2)
            b1 = false;
          else
          {
            j1 += AttributeHeader(ptr1[0].p[i1++]).getDataSize();
            b1 = false;
          }
        }
        if (b1)
        {
          ah = copy_head(i, ptr[0].p, i1, ptr1[0].p, loop);
          copy_attr(ah, j, ptr[1].p, j1, ptr1[1].p, loop);
        }
        else if (b2)
        {
          ah = copy_head(i, ptr[0].p, i2, ptr2[0].p, loop);
          copy_attr(ah, j, ptr[1].p, j2, ptr2[1].p, loop);
        }
        else
          break;
      }
      ptr[0].sz = i;
      ptr[1].sz = j;
    }

    // merge before values, old version overrides
1939
    if (tp->t3 != Ev_t::enum_INS)
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
    {
      AttributeHeader ah;
      Uint32 k = 0;
      Uint32 k1 = 0;
      Uint32 k2 = 0;
      while (1)
      {
        bool b1 = (k1 < ptr1[2].sz);
        bool b2 = (k2 < ptr2[2].sz);
        if (b1 && b2)
        {
          Uint32 id1 = AttributeHeader(ptr1[2].p[k1]).getAttributeId();
          Uint32 id2 = AttributeHeader(ptr2[2].p[k2]).getAttributeId();
          if (id1 < id2)
            b2 = false;
          else if (id1 > id2)
            b1 = false;
          else
          {
            k2 += 1 + AttributeHeader(ptr2[2].p[k2]).getDataSize();
            b2 = false;
          }
        }
        if (b1)
        {
          ah = AttributeHeader(ptr1[2].p[k1]);
          copy_attr(ah, k, ptr[2].p, k1, ptr1[2].p, loop | 2);
        }
        else if (b2)
        {
          ah = AttributeHeader(ptr2[2].p[k2]);
          copy_attr(ah, k, ptr[2].p, k2, ptr2[2].p, loop | 2);
        }
        else
          break;
      }
pekka@mysql.com's avatar
pekka@mysql.com committed
1976
      ptr[2].sz = k;
1977 1978 1979 1980 1981
    }
  }

  // free old data
  NdbMem_Free((char*)olddata.memory);
1982

1983
  DBUG_RETURN_EVENT(0);
1984
}
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
 
/*
 * Given blob part event, find main table event on inline part.  It
 * should exist (force in TUP) but may arrive later.  If so, create
 * NUL event on main table.  The real event replaces it later.
 */

// write attribute headers for concatened PK
static void
split_concatenated_pk(const NdbTableImpl* t, Uint32* ah_buffer,
                      const Uint32* pk_buffer, Uint32 pk_sz)
{
  Uint32 sz = 0; // words parsed so far
  Uint32 n;  // pk attr count
  Uint32 i;
  for (i = n = 0; i < t->m_columns.size() && n < t->m_noOfKeys; i++)
  {
    const NdbColumnImpl* c = t->getColumn(i);
    assert(c != NULL);
    if (! c->m_pk)
      continue;

    assert(sz < pk_sz);
    Uint32 bytesize = c->m_attrSize * c->m_arraySize;
    Uint32 lb, len;
    bool ok = NdbSqlUtil::get_var_length(c->m_type, &pk_buffer[sz], bytesize,
                                         lb, len);
    assert(ok);

    AttributeHeader ah(i, lb + len);
    ah_buffer[n++] = ah.m_value;
    sz += ah.getDataSize();
  }
  assert(n == t->m_noOfKeys && sz == pk_sz);
}

int
NdbEventBuffer::get_main_data(Gci_container* bucket,
                              EventBufData_hash::Pos& hpos,
                              EventBufData* blob_data)
{
  DBUG_ENTER_EVENT("NdbEventBuffer::get_main_data");

  NdbEventOperationImpl* main_op = blob_data->m_event_op->theMainOp;
  assert(main_op != NULL);
  const NdbTableImpl* mainTable = main_op->m_eventImpl->m_tableImpl;

  // create LinearSectionPtr for main table key
  LinearSectionPtr ptr[3];
  Uint32 ah_buffer[NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY];
  ptr[0].sz = mainTable->m_noOfKeys;
  ptr[0].p = ah_buffer;
  ptr[1].sz = AttributeHeader(blob_data->ptr[0].p[0]).getDataSize();
  ptr[1].p = blob_data->ptr[1].p;
  ptr[2].sz = 0;
  ptr[2].p = 0;
  split_concatenated_pk(mainTable, ptr[0].p, ptr[1].p, ptr[1].sz);

  DBUG_DUMP_EVENT("ah", (char*)ptr[0].p, ptr[0].sz << 2);
  DBUG_DUMP_EVENT("pk", (char*)ptr[1].p, ptr[1].sz << 2);

  // search for main event buffer
  bucket->m_data_hash.search(hpos, main_op, ptr);
  if (hpos.data != NULL)
    DBUG_RETURN_EVENT(0);

  // not found, create a place-holder
  EventBufData* main_data = alloc_data();
  if (main_data == NULL)
    DBUG_RETURN_EVENT(-1);
  SubTableData sdata = *blob_data->sdata;
  sdata.tableId = main_op->m_eventImpl->m_tableImpl->m_id;
  sdata.operation = NdbDictionary::Event::_TE_NUL;
  if (copy_data(&sdata, ptr, main_data) != 0)
    DBUG_RETURN_EVENT(-1);
  hpos.data = main_data;

  DBUG_RETURN_EVENT(1);
}

void
NdbEventBuffer::add_blob_data(EventBufData* main_data,
                              EventBufData* blob_data)
{
  DBUG_ENTER_EVENT("NdbEventBuffer::add_blob_data");
  DBUG_PRINT_EVENT("info", ("main_data=%p blob_data=%p", main_data, blob_data));
  EventBufData* head;
  head = main_data->m_next_blob;
  while (head != NULL)
  {
    if (head->m_event_op == blob_data->m_event_op)
      break;
    head = head->m_next_blob;
  }
  if (head == NULL)
  {
    head = blob_data;
    head->m_next_blob = main_data->m_next_blob;
    main_data->m_next_blob = head;
  }
  else
  {
    blob_data->m_next = head->m_next;
    head->m_next = blob_data;
  }
  DBUG_VOID_RETURN_EVENT;
}
2092

2093 2094
NdbEventOperationImpl *
NdbEventBuffer::move_data()
2095
{
2096 2097 2098 2099 2100 2101 2102
  // handle received data
  if (!m_complete_data.m_data.is_empty())
  {
    // move this list to last in m_available_data
    m_available_data.append(m_complete_data.m_data);

    bzero(&m_complete_data, sizeof(m_complete_data));
2103 2104
  }

2105 2106
  // handle used data
  if (!m_used_data.is_empty())
2107
  {
2108 2109
    // return m_used_data to m_free_data
    free_list(m_used_data);
2110
  }
2111 2112
  if (!m_available_data.is_empty())
  {
2113
    DBUG_ENTER_EVENT("NdbEventBuffer::move_data");
2114
#ifdef VM_TRACE
2115
    DBUG_PRINT_EVENT("exit",("m_available_data_count %u", m_available_data.m_count));
2116
#endif
2117
    DBUG_RETURN_EVENT(m_available_data.m_head->m_event_op);
2118 2119 2120
  }
  return 0;
}
2121

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
void
NdbEventBuffer::free_list(EventBufData_list &list)
{
  // return list to m_free_data
  list.m_tail->m_next= m_free_data;
  m_free_data= list.m_head;
#ifdef VM_TRACE
  m_free_data_count+= list.m_count;
#endif
  m_free_data_sz+= list.m_sz;

2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
  // free blobs XXX unacceptable performance, fix later
  {
    EventBufData* data = list.m_head;
    while (1) {
      while (data->m_next_blob != NULL) {
        EventBufData* blob_head = data->m_next_blob;
        data->m_next_blob = blob_head->m_next_blob;
        blob_head->m_next_blob = NULL;
        while (blob_head != NULL) {
          EventBufData* blob_part = blob_head;
          blob_head = blob_head->m_next;
          blob_part->m_next = m_free_data;
          m_free_data = blob_part;
#ifdef VM_TRACE
          m_free_data_count++;
#endif
          m_free_data_sz += blob_part->sz;
        }
      }
      if (data == list.m_tail)
        break;
      data = data->m_next;
    }
  }

2158
  // list returned to m_free_data
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
  list.m_head = list.m_tail = NULL;
  list.m_count = list.m_sz = 0;
}

void
EventBufData_list::add_gci_op(Gci_op g)
{
  assert(g.op != NULL);
  Uint32 i;
  for (i = 0; i < m_gci_op_count; i++) {
    if (m_gci_op_list[i].op == g.op)
      break;
  }
  if (i < m_gci_op_count) {
    m_gci_op_list[i].event_types |= g.event_types;
  } else {
    if (m_gci_op_count == m_gci_op_alloc) {
      Uint32 n = 1 + 2 * m_gci_op_alloc;
      Gci_op* old_list = m_gci_op_list;
      m_gci_op_list = new Gci_op [n];
      if (m_gci_op_alloc != 0) {
        Uint32 bytes = m_gci_op_alloc * sizeof(Gci_op);
        memcpy(m_gci_op_list, old_list, bytes);
        delete [] old_list;
      }
      m_gci_op_alloc = n;
    }
    assert(m_gci_op_count < m_gci_op_alloc);
    m_gci_op_list[m_gci_op_count++] = g;
  }
2189
}
2190

2191 2192 2193 2194 2195 2196 2197
NdbEventOperation*
NdbEventBuffer::createEventOperation(const char* eventName,
				     NdbError &theError)
{
  DBUG_ENTER("NdbEventBuffer::createEventOperation");
  NdbEventOperation* tOp= new NdbEventOperation(m_ndb, eventName);
  if (tOp == 0)
2198
  {
2199 2200
    theError.code= 4000;
    DBUG_RETURN(NULL);
2201
  }
2202 2203 2204 2205 2206 2207 2208
  if (tOp->getState() != NdbEventOperation::EO_CREATED) {
    theError.code= tOp->getNdbError().code;
    delete tOp;
    DBUG_RETURN(NULL);
  }
  DBUG_RETURN(tOp);
}
2209

2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
NdbEventOperationImpl*
NdbEventBuffer::createEventOperation(NdbEventImpl& evnt,
				     NdbError &theError)
{
  DBUG_ENTER("NdbEventBuffer::createEventOperation [evnt]");
  NdbEventOperationImpl* tOp= new NdbEventOperationImpl(m_ndb, evnt);
  if (tOp == 0)
  {
    theError.code= 4000;
    DBUG_RETURN(NULL);
  }
  if (tOp->getState() != NdbEventOperation::EO_CREATED) {
    theError.code= tOp->getNdbError().code;
    delete tOp;
    DBUG_RETURN(NULL);
  }
  DBUG_RETURN(tOp);
}

2229 2230 2231 2232
void
NdbEventBuffer::dropEventOperation(NdbEventOperation* tOp)
{
  NdbEventOperationImpl* op= getEventOperationImpl(tOp);
2233

2234
  op->stop();
2235

2236 2237 2238 2239 2240
  op->m_next= m_dropped_ev_op;
  op->m_prev= 0;
  if (m_dropped_ev_op)
    m_dropped_ev_op->m_prev= op;
  m_dropped_ev_op= op;
2241
 
2242 2243
  // stop blob event ops
  if (op->theMainOp == NULL)
2244 2245
  {
    NdbEventOperationImpl* tBlobOp = op->theBlobOpList;
2246 2247 2248 2249 2250
    while (tBlobOp != NULL)
    {
      tBlobOp->stop();
      tBlobOp = tBlobOp->m_next;
    }
2251 2252 2253 2254 2255 2256 2257 2258

    // release blob handles now, further access is user error
    while (op->theBlobList != NULL)
    {
      NdbBlob* tBlob = op->theBlobList;
      op->theBlobList = tBlob->theNext;
      m_ndb->releaseNdbBlob(tBlob);
    }
2259
  }
2260

2261 2262 2263
  // ToDo, take care of these to be deleted at the
  // appropriate time, after we are sure that there
  // are _no_ more events coming
2264

2265
  //  delete tOp;
2266
}
2267 2268 2269

void
NdbEventBuffer::reportStatus()
2270
{
2271 2272 2273 2274 2275 2276 2277 2278
  EventBufData *apply_buf= m_available_data.m_head;
  Uint64 apply_gci, latest_gci= m_latestGCI;
  if (apply_buf == 0)
    apply_buf= m_complete_data.m_data.m_head;
  if (apply_buf)
    apply_gci= apply_buf->sdata->gci;
  else
    apply_gci= latest_gci;
2279

2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
  if (100*m_free_data_sz < m_min_free_thresh*m_total_alloc &&
      m_total_alloc > 1024*1024)
  {
    /* report less free buffer than m_free_thresh,
       next report when more free than 2 * m_free_thresh
    */
    m_min_free_thresh= 0;
    m_max_free_thresh= 2 * m_free_thresh;
    goto send_report;
  }
  
  if (100*m_free_data_sz > m_max_free_thresh*m_total_alloc &&
      m_total_alloc > 1024*1024)
  {
    /* report more free than 2 * m_free_thresh
       next report when less free than m_free_thresh
    */
    m_min_free_thresh= m_free_thresh;
    m_max_free_thresh= 100;
    goto send_report;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
2300
  }
2301
  if (latest_gci-apply_gci >=  m_gci_slip_thresh)
2302
  {
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
    goto send_report;
  }
  return;

send_report:
  Uint32 data[8];
  data[0]= NDB_LE_EventBufferStatus;
  data[1]= m_total_alloc-m_free_data_sz;
  data[2]= m_total_alloc;
  data[3]= 0;
  data[4]= apply_gci & ~(Uint32)0;
  data[5]= apply_gci >> 32;
  data[6]= latest_gci & ~(Uint32)0;
  data[7]= latest_gci >> 32;
  m_ndb->theImpl->send_event_report(data,8);
2318
#ifdef VM_TRACE
2319
  assert(m_total_alloc >= m_free_data_sz);
2320
#endif
2321
}
2322

2323 2324 2325 2326 2327 2328
// hash table routines

// could optimize the all-fixed case
Uint32
EventBufData_hash::getpkhash(NdbEventOperationImpl* op, LinearSectionPtr ptr[3])
{
2329 2330 2331 2332
  DBUG_ENTER_EVENT("EventBufData_hash::getpkhash");
  DBUG_DUMP_EVENT("ah", (char*)ptr[0].p, ptr[0].sz << 2);
  DBUG_DUMP_EVENT("pk", (char*)ptr[1].p, ptr[1].sz << 2);

2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
  const NdbTableImpl* tab = op->m_eventImpl->m_tableImpl;

  // in all cases ptr[0] = pk ah.. ptr[1] = pk ad..
  // for pk update (to equivalent pk) post/pre values give same hash
  Uint32 nkey = tab->m_noOfKeys;
  assert(nkey != 0 && nkey <= ptr[0].sz);
  const Uint32* hptr = ptr[0].p;
  const uchar* dptr = (uchar*)ptr[1].p;

  // hash registers
  ulong nr1 = 0;
  ulong nr2 = 0;
  while (nkey-- != 0)
  {
    AttributeHeader ah(*hptr++);
    Uint32 bytesize = ah.getByteSize();
    assert(dptr + bytesize <= (uchar*)(ptr[1].p + ptr[1].sz));

    Uint32 i = ah.getAttributeId();
    const NdbColumnImpl* col = tab->getColumn(i);
    assert(col != 0);

    Uint32 lb, len;
    bool ok = NdbSqlUtil::get_var_length(col->m_type, dptr, bytesize, lb, len);
    assert(ok);

    CHARSET_INFO* cs = col->m_cs ? col->m_cs : &my_charset_bin;
    (*cs->coll->hash_sort)(cs, dptr + lb, len, &nr1, &nr2);
2361
    dptr += ((bytesize + 3) / 4) * 4;
2362
  }
2363 2364
  DBUG_PRINT_EVENT("info", ("hash result=%08x", nr1));
  DBUG_RETURN_EVENT(nr1);
2365 2366 2367 2368 2369
}

bool
EventBufData_hash::getpkequal(NdbEventOperationImpl* op, LinearSectionPtr ptr1[3], LinearSectionPtr ptr2[3])
{
2370 2371 2372 2373 2374 2375
  DBUG_ENTER_EVENT("EventBufData_hash::getpkequal");
  DBUG_DUMP_EVENT("ah1", (char*)ptr1[0].p, ptr1[0].sz << 2);
  DBUG_DUMP_EVENT("pk1", (char*)ptr1[1].p, ptr1[1].sz << 2);
  DBUG_DUMP_EVENT("ah2", (char*)ptr2[0].p, ptr2[0].sz << 2);
  DBUG_DUMP_EVENT("pk2", (char*)ptr2[1].p, ptr2[1].sz << 2);

2376 2377 2378 2379 2380 2381 2382 2383 2384
  const NdbTableImpl* tab = op->m_eventImpl->m_tableImpl;

  Uint32 nkey = tab->m_noOfKeys;
  assert(nkey != 0 && nkey <= ptr1[0].sz && nkey <= ptr2[0].sz);
  const Uint32* hptr1 = ptr1[0].p;
  const Uint32* hptr2 = ptr2[0].p;
  const uchar* dptr1 = (uchar*)ptr1[1].p;
  const uchar* dptr2 = (uchar*)ptr2[1].p;

2385 2386
  bool equal = true;

2387 2388 2389 2390 2391 2392
  while (nkey-- != 0)
  {
    AttributeHeader ah1(*hptr1++);
    AttributeHeader ah2(*hptr2++);
    // sizes can differ on update of varchar endspace
    Uint32 bytesize1 = ah1.getByteSize();
2393
    Uint32 bytesize2 = ah2.getByteSize();
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
    assert(dptr1 + bytesize1 <= (uchar*)(ptr1[1].p + ptr1[1].sz));
    assert(dptr2 + bytesize2 <= (uchar*)(ptr2[1].p + ptr2[1].sz));

    assert(ah1.getAttributeId() == ah2.getAttributeId());
    Uint32 i = ah1.getAttributeId();
    const NdbColumnImpl* col = tab->getColumn(i);
    assert(col != 0);

    Uint32 lb1, len1;
    bool ok1 = NdbSqlUtil::get_var_length(col->m_type, dptr1, bytesize1, lb1, len1);
    Uint32 lb2, len2;
    bool ok2 = NdbSqlUtil::get_var_length(col->m_type, dptr2, bytesize2, lb2, len2);
    assert(ok1 && ok2 && lb1 == lb2);

    CHARSET_INFO* cs = col->m_cs ? col->m_cs : &my_charset_bin;
    int res = (cs->coll->strnncollsp)(cs, dptr1 + lb1, len1, dptr2 + lb2, len2, false);
    if (res != 0)
2411 2412 2413 2414
    {
      equal = false;
      break;
    }
2415 2416
    dptr1 += ((bytesize1 + 3) / 4) * 4;
    dptr2 += ((bytesize2 + 3) / 4) * 4;
2417
  }
2418 2419 2420

  DBUG_PRINT_EVENT("info", ("equal=%s", equal ? "true" : "false"));
  DBUG_RETURN_EVENT(equal);
2421 2422 2423 2424 2425
}

void
EventBufData_hash::search(Pos& hpos, NdbEventOperationImpl* op, LinearSectionPtr ptr[3])
{
2426
  DBUG_ENTER_EVENT("EventBufData_hash::search");
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
  Uint32 pkhash = getpkhash(op, ptr);
  Uint32 index = (op->m_oid ^ pkhash) % GCI_EVENT_HASH_SIZE;
  EventBufData* data = m_hash[index];
  while (data != 0)
  {
    if (data->m_event_op == op &&
        data->m_pkhash == pkhash &&
        getpkequal(op, data->ptr, ptr))
      break;
    data = data->m_next_hash;
  }
  hpos.index = index;
  hpos.data = data;
  hpos.pkhash = pkhash;
2441 2442
  DBUG_PRINT_EVENT("info", ("search result=%p", data));
  DBUG_VOID_RETURN_EVENT;
2443 2444
}

2445 2446
template class Vector<Gci_container>;
template class Vector<NdbEventBuffer::EventBufData_chunk*>;