⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hugotransactions.cpp

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/* 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 "HugoTransactions.hpp"#include <NdbSleep.h>HugoTransactions::HugoTransactions(const NdbDictionary::Table& _tab,				   const NdbDictionary::Index* idx):  HugoOperations(_tab, idx),  row(_tab){  m_defaultScanUpdateMethod = 3;}HugoTransactions::~HugoTransactions(){  deallocRows();}intHugoTransactions::scanReadRecords(Ndb* pNdb, 				  int records,				  int abortPercent,				  int parallelism, 				  NdbOperation::LockMode lm,                                  int scan_flags){    int                  retryAttempt = 0;  const int            retryMax = 100;  int                  check, a;  NdbScanOperation	       *pOp;  while (true){    if (retryAttempt >= retryMax){      g_err << "ERROR: has retried this operation " << retryAttempt 	    << " times, failing!" << endl;      return NDBT_FAILED;    }    pTrans = pNdb->startTransaction();    if (pTrans == NULL) {      const NdbError err = pNdb->getNdbError();      if (err.status == NdbError::TemporaryError){	ERR(err);	NdbSleep_MilliSleep(50);	retryAttempt++;	continue;      }      ERR(err);      return NDBT_FAILED;    }    pOp = getScanOperation(pTrans);    if (pOp == NULL) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }    if( pOp ->readTuples(lm, scan_flags, parallelism) ) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }        check = pOp->interpret_exit_ok();    if( check == -1 ) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }      for(a = 0; a<tab.getNoOfColumns(); a++){      if((row.attributeStore(a) = 	  pOp->getValue(tab.getColumn(a)->getName())) == 0) {	ERR(pTrans->getNdbError());	closeTransaction(pNdb);	return NDBT_FAILED;      }    }    check = pTrans->execute(NoCommit);    if( check == -1 ) {      const NdbError err = pTrans->getNdbError();      if (err.status == NdbError::TemporaryError){	ERR(err);	closeTransaction(pNdb);	NdbSleep_MilliSleep(50);	retryAttempt++;	continue;      }      ERR(err);      closeTransaction(pNdb);      return NDBT_FAILED;    }    // Abort after 1-100 or 1-records rows    int ranVal = rand();    int abortCount = ranVal % (records == 0 ? 100 : records);     bool abortTrans = false;    if (abort > 0){      // Abort if abortCount is less then abortPercent       if (abortCount < abortPercent) 	abortTrans = true;    }        int eof;    int rows = 0;    while((eof = pOp->nextResult(true)) == 0){      rows++;      if (calc.verifyRowValues(&row) != 0){	closeTransaction(pNdb);	return NDBT_FAILED;      }      if (abortCount == rows && abortTrans == true){	ndbout << "Scan is aborted" << endl;	g_info << "Scan is aborted" << endl;	pOp->close();	if( check == -1 ) {	  ERR(pTrans->getNdbError());	  closeTransaction(pNdb);	  return NDBT_FAILED;	}		closeTransaction(pNdb);	return NDBT_OK;      }    }    if (eof == -1) {      const NdbError err = pTrans->getNdbError();            if (err.status == NdbError::TemporaryError){	ERR_INFO(err);	closeTransaction(pNdb);	NdbSleep_MilliSleep(50);	switch (err.code){	case 488:	case 245:	case 490:	  // Too many active scans, no limit on number of retry attempts	  break;	default:	  retryAttempt++;	}	continue;      }      ERR(err);      closeTransaction(pNdb);      return NDBT_FAILED;    }    closeTransaction(pNdb);    g_info << rows << " rows have been read" << endl;    if (records != 0 && rows != records){      g_err << "Check expected number of records failed" << endl 	    << "  expected=" << records <<", " << endl	    << "  read=" << rows << endl;      return NDBT_FAILED;    }        return NDBT_OK;  }  return NDBT_FAILED;}intHugoTransactions::scanReadRecords(Ndb* pNdb, 				  const NdbDictionary::Index * pIdx,				  int records,				  int abortPercent,				  int parallelism, 				  NdbOperation::LockMode lm,                                  int scan_flags){    int                  retryAttempt = 0;  const int            retryMax = 100;  int                  check, a;  NdbIndexScanOperation	       *pOp;  while (true){    if (retryAttempt >= retryMax){      g_err << "ERROR: has retried this operation " << retryAttempt 	    << " times, failing!" << endl;      return NDBT_FAILED;    }    pTrans = pNdb->startTransaction();    if (pTrans == NULL) {      const NdbError err = pNdb->getNdbError();      if (err.status == NdbError::TemporaryError){	ERR(err);	NdbSleep_MilliSleep(50);	retryAttempt++;	continue;      }      ERR(err);      return NDBT_FAILED;    }    pOp = pTrans->getNdbIndexScanOperation(pIdx->getName(), tab.getName());    if (pOp == NULL) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }    if( pOp ->readTuples(lm, scan_flags, parallelism) ) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }        check = pOp->interpret_exit_ok();    if( check == -1 ) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }      for(a = 0; a<tab.getNoOfColumns(); a++){      if((row.attributeStore(a) = 	  pOp->getValue(tab.getColumn(a)->getName())) == 0) {	ERR(pTrans->getNdbError());	closeTransaction(pNdb);	return NDBT_FAILED;      }    }    check = pTrans->execute(NoCommit);    if( check == -1 ) {      const NdbError err = pTrans->getNdbError();      if (err.status == NdbError::TemporaryError){	ERR(err);	closeTransaction(pNdb);	NdbSleep_MilliSleep(50);	retryAttempt++;	continue;      }      ERR(err);      closeTransaction(pNdb);      return NDBT_FAILED;    }    // Abort after 1-100 or 1-records rows    int ranVal = rand();    int abortCount = ranVal % (records == 0 ? 100 : records);     bool abortTrans = false;    if (abort > 0){      // Abort if abortCount is less then abortPercent       if (abortCount < abortPercent) 	abortTrans = true;    }        int eof;    int rows = 0;    while((eof = pOp->nextResult(true)) == 0){      rows++;      if (calc.verifyRowValues(&row) != 0){	closeTransaction(pNdb);	return NDBT_FAILED;      }      if (abortCount == rows && abortTrans == true){	ndbout << "Scan is aborted" << endl;	g_info << "Scan is aborted" << endl;	pOp->close();	if( check == -1 ) {	  ERR(pTrans->getNdbError());	  closeTransaction(pNdb);	  return NDBT_FAILED;	}		closeTransaction(pNdb);	return NDBT_OK;      }    }    if (eof == -1) {      const NdbError err = pTrans->getNdbError();            if (err.status == NdbError::TemporaryError){	ERR_INFO(err);	closeTransaction(pNdb);	NdbSleep_MilliSleep(50);	switch (err.code){	case 488:	case 245:	case 490:	  // Too many active scans, no limit on number of retry attempts	  break;	default:	  retryAttempt++;	}	continue;      }      ERR(err);      closeTransaction(pNdb);      return NDBT_FAILED;    }    closeTransaction(pNdb);    g_info << rows << " rows have been read" << endl;    if (records != 0 && rows != records){      g_err << "Check expected number of records failed" << endl 	    << "  expected=" << records <<", " << endl	    << "  read=" << rows << endl;      return NDBT_FAILED;    }        return NDBT_OK;  }  return NDBT_FAILED;}#define RESTART_SCAN 99intHugoTransactions::scanUpdateRecords(Ndb* pNdb, 				    int records,				    int abortPercent,				    int parallelism){  if(m_defaultScanUpdateMethod == 1){    return scanUpdateRecords1(pNdb, records, abortPercent, parallelism);  } else if(m_defaultScanUpdateMethod == 2){    return scanUpdateRecords2(pNdb, records, abortPercent, parallelism);  } else {    return scanUpdateRecords3(pNdb, records, abortPercent, parallelism);  }}// Scan all records exclusive and update // them one by oneintHugoTransactions::scanUpdateRecords1(Ndb* pNdb, 				     int records,				     int abortPercent,				     int parallelism){  return scanUpdateRecords3(pNdb, records, abortPercent, 1);}// Scan all records exclusive and update // them batched by asking nextScanResult to// give us all cached records before fetching new // records from dbintHugoTransactions::scanUpdateRecords2(Ndb* pNdb, 				     int records,				     int abortPercent,				     int parallelism){  return scanUpdateRecords3(pNdb, records, abortPercent, parallelism);}intHugoTransactions::scanUpdateRecords3(Ndb* pNdb, 				     int records,				     int abortPercent,				     int parallelism){  int                  retryAttempt = 0;  const int            retryMax = 100;  int check, a;  NdbScanOperation *pOp;  while (true){restart:    if (retryAttempt++ >= retryMax){      g_info << "ERROR: has retried this operation " << retryAttempt 	     << " times, failing!" << endl;      return NDBT_FAILED;    }    pTrans = pNdb->startTransaction();    if (pTrans == NULL) {      const NdbError err = pNdb->getNdbError();      ERR(err);      if (err.status == NdbError::TemporaryError){	NdbSleep_MilliSleep(50);	continue;      }      return NDBT_FAILED;    }    pOp = getScanOperation(pTrans);    if (pOp == NULL) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }        if( pOp->readTuplesExclusive(parallelism) ) {      ERR(pTrans->getNdbError());      closeTransaction(pNdb);      return NDBT_FAILED;    }        // Read all attributes from this table        for(a=0; a<tab.getNoOfColumns(); a++){      if((row.attributeStore(a) = pOp->getValue(tab.getColumn(a)->getName())) == NULL){	ERR(pTrans->getNdbError());	closeTransaction(pNdb);	return NDBT_FAILED;      }    }        check = pTrans->execute(NoCommit);    if( check == -1 ) {      const NdbError err = pTrans->getNdbError();      ERR(err);      closeTransaction(pNdb);      if (err.status == NdbError::TemporaryError){	NdbSleep_MilliSleep(50);	continue;      }      return NDBT_FAILED;    }      // Abort after 1-100 or 1-records rows    int ranVal = rand();    int abortCount = ranVal % (records == 0 ? 100 : records);     bool abortTrans = false;    if (abort > 0){      // Abort if abortCount is less then abortPercent       if (abortCount < abortPercent) 	abortTrans = true;    }        int rows = 0;    while((check = pOp->nextResult(true)) == 0){      do {	rows++;	NdbOperation* pUp = pOp->updateCurrentTuple();	if(pUp == 0){	  ERR(pTrans->getNdbError());	  closeTransaction(pNdb);	  return NDBT_FAILED;	}	const int updates = calc.getUpdatesValue(&row) + 1;	const int r = calc.getIdValue(&row);  	for(a = 0; a<tab.getNoOfColumns(); a++){	  if (tab.getColumn(a)->getPrimaryKey() == false){	    if(setValueForAttr(pUp, a, r, updates ) != 0){	      ERR(pTrans->getNdbError());	      closeTransaction(pNdb);	      return NDBT_FAILED;	    }	  }	}	if (rows == abortCount && abortTrans == true){	  g_info << "Scan is aborted" << endl;	  // This scan should be aborted	  closeTransaction(pNdb);	  return NDBT_OK;	}      } while((check = pOp->nextResult(false)) == 0);      if(check != -1){	check = pTrans->execute(Commit);   	pTrans->restart();      }      const NdbError err = pTrans->getNdbError();          if( check == -1 ) {	closeTransaction(pNdb);	ERR(err);	if (err.status == NdbError::TemporaryError){	  NdbSleep_MilliSleep(50);	  goto restart;	}	return NDBT_FAILED;      }    }        const NdbError err = pTrans->getNdbError();        if( check == -1 ) {      closeTransaction(pNdb);      ERR(err);      if (err.status == NdbError::TemporaryError){	NdbSleep_MilliSleep(50);	goto restart;      }      return NDBT_FAILED;    }        closeTransaction(pNdb);        g_info << rows << " rows have been updated" << endl;    return NDBT_OK;  }  return NDBT_FAILED;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -