static void run_slave_wait(struct Xxx &xxx, struct XxxR &xxxr) { struct timeval old_end_time = xxxr.start_time, end_time; Ndb *ndb = xxx.ndb; const NdbDictionary::Table *myTable = xxx.table; int retry_sleep= 10; /* 10 milliseconds */ int retries= 100; while (1) { Uint32 val; NdbTransaction *trans = ndb->startTransaction(); if (trans == NULL) goto err; { NdbOperation *op = trans->getNdbOperation(myTable); if (op == NULL) APIERROR(trans->getNdbError()); op->readTuple(); op->equal(xxx.pk_col, xxxr.pk_val); op->getValue(xxx.col, (char *)&val); if (trans->execute(NdbTransaction::Commit)) goto err; } /* read done, check time of read */ gettimeofday(&end_time, 0); ndb->closeTransaction(trans); //fprintf(stderr, "read %u waiting for %u\n", val, xxxr.val); if (xxxr.val != val) { /* expected value not received yet */ retries = 100; NdbSleep_MilliSleep(retry_sleep); old_end_time = end_time; continue; } break; err: const NdbError this_error= trans ? trans->getNdbError() : ndb->getNdbError(); if (this_error.status == NdbError::TemporaryError) { if (retries--) { if (trans) ndb->closeTransaction(trans); NdbSleep_MilliSleep(retry_sleep); continue; // retry } } if (trans) ndb->closeTransaction(trans); APIERROR(this_error); } Int64 elapsed_usec1 = ((Int64)end_time.tv_sec - (Int64)xxxr.start_time.tv_sec)*1000*1000 + ((Int64)end_time.tv_usec - (Int64)xxxr.start_time.tv_usec); Int64 elapsed_usec2 = ((Int64)end_time.tv_sec - (Int64)old_end_time.tv_sec)*1000*1000 + ((Int64)end_time.tv_usec - (Int64)old_end_time.tv_usec); xxxr.latency = ((elapsed_usec1 - elapsed_usec2/2)+999)/1000; }
/** * Transaction 5 - T5 * * Delete session * * Input: * SubscriberNumber * ServerId * ServerBit * DoRollback * Output: * ChangedBy * ChangedTime * Location * BranchExecuted */ void userTransaction_T5(UserHandle * uh, SubscriberNumber inNumber, ServerId inServerId, ServerBit inServerBit, DoRollback inDoRollback, BranchExecuted * outBranchExecuted){ Ndb * pNDB = uh->pNDB; DEBUG3("T5(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId); NdbConnection * MyTransaction = 0; NdbOperation * MyOperation = 0; char outChangedBy [sizeof(ChangedBy) +(4-(sizeof(ChangedBy) & 3))]; char outChangedTime [sizeof(ChangedTime)+(4-(sizeof(ChangedTime) & 3))]; Location outLocation; GroupId groupId; ActiveSessions sessions; Permission permission; SubscriberSuffix inSuffix; int check; NdbRecAttr * check2; MyTransaction = pNDB->startTransaction(); if (MyTransaction == NULL) error_handler("T5-1: startTranscation", pNDB->getNdbErrorString(), pNDB->getNdbError()); MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE); CHECK_NULL(MyOperation, "T5-1: getNdbOperation", MyTransaction); MyOperation->interpretedUpdateTuple(); MyOperation->equal(IND_SUBSCRIBER_NUMBER, inNumber); MyOperation->getValue(IND_SUBSCRIBER_LOCATION, (char *)&outLocation); MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY, &outChangedBy[0]); MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME, &outChangedTime[0]); MyOperation->getValue(IND_SUBSCRIBER_GROUP, (char *)&groupId); MyOperation->getValue(IND_SUBSCRIBER_SESSIONS, (char *)&sessions); MyOperation->subValue(IND_SUBSCRIBER_SESSIONS, (uint32)inServerBit); MyTransaction->execute( NoCommit ); /* Operation 2 */ MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE); CHECK_NULL(MyOperation, "T5-2: getNdbOperation", MyTransaction); MyOperation->readTuple(); MyOperation->equal(IND_GROUP_ID, (char*)&groupId); MyOperation->getValue(IND_GROUP_ALLOW_DELETE, (char *)&permission); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-2: NoCommit", MyTransaction); if(((permission & inServerBit) == inServerBit) && ((sessions & inServerBit) == inServerBit)){ memcpy(inSuffix, &inNumber[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH], SUBSCRIBER_NUMBER_SUFFIX_LENGTH); DEBUG2("deleting(%.*s) - ", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, inSuffix); /* Operation 3 */ MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE); CHECK_NULL(MyOperation, "T5-3: getNdbOperation", MyTransaction); MyOperation->deleteTuple(); MyOperation->equal(IND_SESSION_SUBSCRIBER, (char*)inNumber); MyOperation->equal(IND_SESSION_SERVER, (char*)&inServerId); /* Operation 4 */ /* Operation 5 */ MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE); CHECK_NULL(MyOperation, "T5-5: getNdbOperation", MyTransaction); MyOperation->interpretedUpdateTuple(); MyOperation->equal(IND_SERVER_ID, (char*)&inServerId); MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX, (char*)inSuffix); MyOperation->incValue(IND_SERVER_DELETES, (uint32)1); (* outBranchExecuted) = 1; } else { (* outBranchExecuted) = 0; DEBUG1("%s", ((permission & inServerBit) ? "permission - " : "no permission - ")); DEBUG1("%s", ((sessions & inServerBit) ? "in session - " : "no in session - ")); } if(!inDoRollback && (* outBranchExecuted)){ DEBUG("commit\n"); check = MyTransaction->execute( Commit ); CHECK_MINUS_ONE(check, "T5: Commit", MyTransaction); } else { DEBUG("rollback\n"); check = MyTransaction->execute(Rollback); CHECK_MINUS_ONE(check, "T5:Rollback", MyTransaction); } pNDB->closeTransaction(MyTransaction); }
int main() { ndb_init(); Ndb_cluster_connection *cluster_connection= new Ndb_cluster_connection(); // Object representing the cluster if (cluster_connection->wait_until_ready(30,30)) { std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } int r= cluster_connection->connect(5 /* retries */, 3 /* delay between retries */, 1 /* verbose */); if (r > 0) { std::cout << "Cluster connect failed, possibly resolved with more retries.\n"; exit(-1); } else if (r < 0) { std::cout << "Cluster connect failed.\n"; exit(-1); } if (cluster_connection->wait_until_ready(30,30)) { std::cout << "Cluster was not ready within 30 secs." << std::endl; exit(-1); } Ndb* myNdb = new Ndb( cluster_connection, "TEST_DB_2" ); // Object representing the database NdbTransaction* myNdbTransaction[2]; // For transactions NdbOperation* myNdbOperation; // For operations if (myNdb->init(2) == -1) { // Want two parallel insert transactions APIERROR(myNdb->getNdbError()); exit(-1); } /****************************************************** * Insert (we do two insert transactions in parallel) * ******************************************************/ const NdbDictionary::Dictionary* myDict= myNdb->getDictionary(); const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); if (myTable == NULL) APIERROR(myDict->getNdbError()); for (int i = 0; i < 2; i++) { myNdbTransaction[i] = myNdb->startTransaction(); if (myNdbTransaction[i] == NULL) APIERROR(myNdb->getNdbError()); myNdbOperation = myNdbTransaction[i]->getNdbOperation(myTable); if (myNdbOperation == NULL) APIERROR(myNdbTransaction[i]->getNdbError()); myNdbOperation->insertTuple(); myNdbOperation->equal("ATTR1", 20 + i); myNdbOperation->setValue("ATTR2", 20 + i); // Prepare transaction (the transaction is NOT yet sent to NDB) myNdbTransaction[i]->executeAsynchPrepare(NdbTransaction::Commit, &callback, NULL); } // Send all transactions to NDB myNdb->sendPreparedTransactions(0); // Poll all transactions myNdb->pollNdb(3000, 2); // Close all transactions for (int i = 0; i < 2; i++) myNdb->closeTransaction(myNdbTransaction[i]); delete myNdb; delete cluster_connection; ndb_end(0); return 0; }
/** * Transaction 5 - T5 * * Delete session * * Input: * SubscriberNumber * ServerId * ServerBit * DoRollback * Output: * ChangedBy * ChangedTime * Location * BranchExecuted */ int T5(void * obj, const SubscriberNumber inNumber, const SubscriberSuffix inSuffix, const ServerId inServerId, const ServerBit inServerBit, ChangedBy outChangedBy, ChangedTime outChangedTime, Location * outLocation, DoRollback inDoRollback, BranchExecuted * outBranchExecuted, BenchmarkTime * outTransactionTime) { Ndb * pNDB = (Ndb *) obj; NdbConnection * MyTransaction = 0; NdbOperation * MyOperation = 0; GroupId groupId; ActiveSessions sessions; Permission permission; BenchmarkTime start; get_time(&start); int check; NdbRecAttr * check2; MyTransaction = pNDB->startTransaction(); if (MyTransaction == NULL) error_handler("T5-1: startTranscation", pNDB->getNdbErrorString(), 0); MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE); CHECK_NULL(MyOperation, "T5-1: getNdbOperation", MyTransaction); check = MyOperation->readTupleExclusive(); CHECK_MINUS_ONE(check, "T5-1: readTuple", MyTransaction); check = MyOperation->equal(IND_SUBSCRIBER_NUMBER, inNumber); CHECK_MINUS_ONE(check, "T5-1: equal subscriber", MyTransaction); check2 = MyOperation->getValue(IND_SUBSCRIBER_LOCATION, (char *)outLocation); CHECK_NULL(check2, "T5-1: getValue location", MyTransaction); check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY, outChangedBy); CHECK_NULL(check2, "T5-1: getValue changed_by", MyTransaction); check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME, outChangedTime); CHECK_NULL(check2, "T5-1: getValue changed_time", MyTransaction); check2 = MyOperation->getValue(IND_SUBSCRIBER_GROUP, (char *)&groupId); CHECK_NULL(check2, "T5-1: getValue group", MyTransaction); check2 = MyOperation->getValue(IND_SUBSCRIBER_SESSIONS, (char *)&sessions); CHECK_NULL(check2, "T5-1: getValue sessions", MyTransaction); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-1: NoCommit", MyTransaction); /* Operation 2 */ MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE); CHECK_NULL(MyOperation, "T5-2: getNdbOperation", MyTransaction); check = MyOperation->readTuple(); CHECK_MINUS_ONE(check, "T5-2: readTuple", MyTransaction); check = MyOperation->equal(IND_GROUP_ID, (char*)&groupId); CHECK_MINUS_ONE(check, "T5-2: equal group", MyTransaction); check2 = MyOperation->getValue(IND_GROUP_ALLOW_DELETE, (char *)&permission); CHECK_NULL(check2, "T5-2: getValue allow_delete", MyTransaction); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-2: NoCommit", MyTransaction); DEBUG3("T5(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId); if(((permission & inServerBit) == inServerBit) && ((sessions & inServerBit) == inServerBit)) { DEBUG("deleting - "); /* Operation 3 */ MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE); CHECK_NULL(MyOperation, "T5-3: getNdbOperation", MyTransaction); check = MyOperation->deleteTuple(); CHECK_MINUS_ONE(check, "T5-3: deleteTuple", MyTransaction); check = MyOperation->equal(IND_SESSION_SUBSCRIBER, (char*)inNumber); CHECK_MINUS_ONE(check, "T5-3: equal number", MyTransaction); check = MyOperation->equal(IND_SESSION_SERVER, (char*)&inServerId); CHECK_MINUS_ONE(check, "T5-3: equal server id", MyTransaction); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-3: NoCommit", MyTransaction); /* Operation 4 */ MyOperation = MyTransaction->getNdbOperation(SUBSCRIBER_TABLE); CHECK_NULL(MyOperation, "T5-4: getNdbOperation", MyTransaction); check = MyOperation->interpretedUpdateTuple(); CHECK_MINUS_ONE(check, "T5-4: interpretedUpdateTuple", MyTransaction); check = MyOperation->equal(IND_SUBSCRIBER_NUMBER, (char*)inNumber); CHECK_MINUS_ONE(check, "T5-4: equal number", MyTransaction); check = MyOperation->subValue(IND_SUBSCRIBER_SESSIONS, (uint32)inServerBit); CHECK_MINUS_ONE(check, "T5-4: dec value", MyTransaction); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-4: NoCommit", MyTransaction); /* Operation 5 */ MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE); CHECK_NULL(MyOperation, "T5-5: getNdbOperation", MyTransaction); check = MyOperation->interpretedUpdateTuple(); CHECK_MINUS_ONE(check, "T5-5: interpretedUpdateTuple", MyTransaction); check = MyOperation->equal(IND_SERVER_ID, (char*)&inServerId); CHECK_MINUS_ONE(check, "T5-5: equal serverId", MyTransaction); check = MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX, (char*)inSuffix); CHECK_MINUS_ONE(check, "T5-5: equal suffix", MyTransaction); check = MyOperation->incValue(IND_SERVER_DELETES, (uint32)1); CHECK_MINUS_ONE(check, "T5-5: inc value", MyTransaction); check = MyTransaction->execute( NoCommit ); CHECK_MINUS_ONE(check, "T5-5: NoCommit", MyTransaction); (* outBranchExecuted) = 1; } else { DEBUG1("%s", ((permission & inServerBit) ? "permission - " : "no permission - ")); DEBUG1("%s", ((sessions & inServerBit) ? "in session - " : "no in session - ")); (* outBranchExecuted) = 0; } if(!inDoRollback) { DEBUG("commit\n"); check = MyTransaction->execute( Commit ); CHECK_MINUS_ONE(check, "T5: Commit", MyTransaction); } else { DEBUG("rollback\n"); check = MyTransaction->execute(Rollback); CHECK_MINUS_ONE(check, "T5:Rollback", MyTransaction); } pNDB->closeTransaction(MyTransaction); get_time(outTransactionTime); time_diff(outTransactionTime, &start); return 0; }
int run_read(){ int iter = g_paramters[P_LOOPS].value; NDB_TICKS start1, stop; int sum_time= 0; const Uint32 rows = g_paramters[P_ROWS].value; const Uint32 range = g_paramters[P_RANGE].value; start1 = NdbTick_CurrentMillisecond(); NdbConnection * pTrans = g_ndb->startTransaction(); if(!pTrans){ g_err << "Failed to start transaction" << endl; err(g_ndb->getNdbError()); return -1; } NdbOperation * pOp; NdbScanOperation * pSp; NdbIndexOperation * pUp; NdbIndexScanOperation * pIp; Uint32 start_row = rand() % (rows - range); Uint32 stop_row = start_row + range; /** * 0 - serial pk * 1 - batch pk * 2 - serial uniq * 3 - batch uniq * 4 - index eq * 5 - range scan * 6 - interpreted scan */ int check = 0; void* res = (void*)~0; const Uint32 pk = 0; Uint32 cnt = 0; for(; start_row < stop_row; start_row++){ switch(g_paramters[P_OPER].value){ case 0: pOp = pTrans->getNdbOperation(g_table); check = pOp->readTuple(); check = pOp->equal(pk, start_row); break; case 1: for(; start_row<stop_row; start_row++){ pOp = pTrans->getNdbOperation(g_table); check = pOp->readTuple(); check = pOp->equal(pk, start_row); for(int j = 0; j<g_tab->getNoOfColumns(); j++){ res = pOp->getValue(j); assert(res); } } break; case 2: pOp = pTrans->getNdbIndexOperation(g_unique, g_table); check = pOp->readTuple(); check = pOp->equal(pk, start_row); break; case 3: for(; start_row<stop_row; start_row++){ pOp = pTrans->getNdbIndexOperation(g_unique, g_table); check = pOp->readTuple(); check = pOp->equal(pk, start_row); for(int j = 0; j<g_tab->getNoOfColumns(); j++){ res = pOp->getValue(j); assert(res); } } break; case 4: pOp = pSp = pIp = pTrans->getNdbIndexScanOperation(g_ordered,g_table); pIp->readTuples(NdbScanOperation::LM_CommittedRead, 0, 0); check = pIp->setBound(pk, NdbIndexScanOperation::BoundEQ, &start_row); break; case 5: pOp = pSp = pIp = pTrans->getNdbIndexScanOperation(g_ordered,g_table); pIp->readTuples(NdbScanOperation::LM_CommittedRead, 0, 0); check = pIp->setBound(pk, NdbIndexScanOperation::BoundLE, &start_row); check = pIp->setBound(pk, NdbIndexScanOperation::BoundGT, &stop_row); start_row = stop_row; break; case 6: pOp = pSp = pIp = pTrans->getNdbIndexScanOperation(g_ordered,g_table); pIp->readTuples(NdbScanOperation::LM_CommittedRead, 0, 0, true); check = pIp->setBound(pk, NdbIndexScanOperation::BoundLE, &start_row); check = pIp->setBound(pk, NdbIndexScanOperation::BoundGT, &stop_row); start_row = stop_row; break; case 7: pOp = pSp = pTrans->getNdbScanOperation(g_table); pSp->readTuples(NdbScanOperation::LM_CommittedRead, 0, 0); NdbScanFilter filter(pOp) ; filter.begin(NdbScanFilter::AND); filter.ge(pk, start_row); filter.lt(pk, stop_row); filter.end(); start_row = stop_row; break; } assert(res); if(check != 0){ ndbout << pOp->getNdbError() << endl; ndbout << pTrans->getNdbError() << endl; } assert(check == 0); for(int j = 0; j<g_tab->getNoOfColumns(); j++){ res = pOp->getValue(j); assert(res); } check = pTrans->execute(NoCommit); if(check != 0){ ndbout << pTrans->getNdbError() << endl; } assert(check == 0); if(g_paramters[P_OPER].value >= 4){ while((check = pSp->nextResult(true)) == 0){ cnt++; } if(check == -1){ err(pTrans->getNdbError()); return -1; } assert(check == 1); pSp->close(); } } assert(g_paramters[P_OPER].value < 4 || (cnt == range)); pTrans->close(); stop = NdbTick_CurrentMillisecond(); g_times[g_paramters[P_OPER].value] += (stop - start1); return 0; }