~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/test/src/HugoAsynchTransactions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <NdbSleep.h>
 
17
#include <HugoAsynchTransactions.hpp>
 
18
 
 
19
HugoAsynchTransactions::HugoAsynchTransactions(const NdbDictionary::Table& _t)
 
20
  : HugoTransactions(_t),
 
21
    transactionsCompleted(0),
 
22
    numTransactions(0),
 
23
    transactions(NULL)
 
24
{
 
25
}
 
26
 
 
27
HugoAsynchTransactions::~HugoAsynchTransactions(){
 
28
  deallocTransactions();
 
29
}
 
30
 
 
31
void asynchCallback(int result, NdbConnection* pTrans, 
 
32
                    void* anObject) {
 
33
  HugoAsynchTransactions* pHugo = (HugoAsynchTransactions*) anObject;
 
34
  
 
35
  pHugo->transactionCompleted();
 
36
 
 
37
  if (result == -1) {
 
38
    const NdbError err = pTrans->getNdbError();
 
39
    switch(err.status) {
 
40
    case NdbError::Success:
 
41
      ERR(err);
 
42
      g_info << "ERROR: NdbError reports success when transcaction failed"
 
43
             << endl;
 
44
      break;
 
45
      
 
46
    case NdbError::TemporaryError:      
 
47
      ERR(err);
 
48
      break;
 
49
 
 
50
#if 0      
 
51
    case 626: // Tuple did not exist
 
52
      g_info << (unsigned int)pHugo->getTransactionsCompleted() << ": " 
 
53
             << err.code << " " << err.message << endl;
 
54
      break;
 
55
#endif
 
56
 
 
57
    case NdbError::UnknownResult:
 
58
      ERR(err);
 
59
      break;
 
60
      
 
61
    case NdbError::PermanentError:
 
62
      switch (err.classification) {
 
63
      case NdbError::ConstraintViolation:
 
64
        // Tuple already existed, OK in this application, 
 
65
        // but should be reported
 
66
        g_info << (unsigned int)pHugo->getTransactionsCompleted() 
 
67
               << ": " << err.code << " " << err.message << endl;
 
68
        break;
 
69
      default:
 
70
        ERR(err);
 
71
        break;
 
72
      }
 
73
      break;
 
74
    }
 
75
  } else {// if (result == -1)
 
76
    /*
 
77
    ndbout << (unsigned int)pHugo->getTransactionsCompleted() << " completed" 
 
78
           << endl;
 
79
    */
 
80
  }
 
81
}
 
82
 
 
83
int
 
84
HugoAsynchTransactions::loadTableAsynch(Ndb* pNdb, 
 
85
                                  int records,
 
86
                                  int batch,
 
87
                                  int trans,
 
88
                                  int operations){
 
89
 
 
90
  int result = executeAsynchOperation(pNdb, records, batch, trans, operations, 
 
91
                                      NO_INSERT);
 
92
  g_info << (unsigned int)transactionsCompleted * operations 
 
93
         << "|- inserted..." << endl;
 
94
 
 
95
  return result;
 
96
 
97
 
 
98
void
 
99
HugoAsynchTransactions::transactionCompleted() {
 
100
  transactionsCompleted++;
 
101
}
 
102
 
 
103
long
 
104
HugoAsynchTransactions::getTransactionsCompleted() {
 
105
  return transactionsCompleted;
 
106
}
 
107
 
 
108
int 
 
109
HugoAsynchTransactions::pkDelRecordsAsynch(Ndb* pNdb, 
 
110
                                     int records,
 
111
                                     int batch,
 
112
                                     int trans,
 
113
                                     int operations) {
 
114
  
 
115
  g_info << "|- Deleting records asynchronous..." << endl;
 
116
 
 
117
  int result =  executeAsynchOperation(pNdb, records, batch, trans, 
 
118
                                       operations, 
 
119
                                       NO_DELETE);
 
120
  g_info << "|- " << (unsigned int)transactionsCompleted * operations 
 
121
         << " deleted..." << endl;
 
122
 
 
123
  return result;  
 
124
}
 
125
 
 
126
int 
 
127
HugoAsynchTransactions::pkReadRecordsAsynch(Ndb* pNdb, 
 
128
                                      int records,
 
129
                                      int batch,
 
130
                                      int trans,
 
131
                                      int operations) {
 
132
 
 
133
  g_info << "|- Reading records asynchronous..." << endl;
 
134
 
 
135
  allocRows(trans*operations);
 
136
  int result = executeAsynchOperation(pNdb, records, batch, trans, operations, 
 
137
                                      NO_READ);
 
138
 
 
139
  g_info << "|- " << (unsigned int)transactionsCompleted * operations 
 
140
         << " read..."
 
141
         << endl;
 
142
 
 
143
  deallocRows();
 
144
 
 
145
  return result;
 
146
}
 
147
 
 
148
int 
 
149
HugoAsynchTransactions::pkUpdateRecordsAsynch(Ndb* pNdb, 
 
150
                                        int records,
 
151
                                        int batch,
 
152
                                        int trans,
 
153
                                        int operations) {
 
154
 
 
155
  g_info << "|- Updating records asynchronous..." << endl;
 
156
 
 
157
  int             check = 0;
 
158
  int             cTrans = 0;
 
159
  int             cReadRecords = 0;
 
160
  int             cReadIndex = 0;
 
161
  int             cRecords = 0;
 
162
  int             cIndex = 0;
 
163
 
 
164
  transactionsCompleted = 0;
 
165
 
 
166
  allocRows(trans*operations);
 
167
  allocTransactions(trans);
 
168
  int a, t, r;
 
169
 
 
170
  for (int i = 0; i < batch; i++) { // For each batch
 
171
    while (cRecords < records*batch) {
 
172
      cTrans = 0;
 
173
      cReadIndex = 0;
 
174
      for (t = 0; t < trans; t++) { // For each transaction
 
175
        transactions[t] = pNdb->startTransaction();
 
176
        if (transactions[t] == NULL) {
 
177
          ERR(pNdb->getNdbError());
 
178
          return NDBT_FAILED;
 
179
        }       
 
180
        for (int k = 0; k < operations; k++) { // For each operation
 
181
          NdbOperation* pOp = transactions[t]->getNdbOperation(tab.getName());
 
182
          if (pOp == NULL) { 
 
183
            ERR(transactions[t]->getNdbError());
 
184
            pNdb->closeTransaction(transactions[t]);
 
185
            return NDBT_FAILED;
 
186
          }
 
187
          
 
188
          // Read
 
189
          // Define primary keys
 
190
          check = pOp->readTupleExclusive();
 
191
          if (equalForRow(pOp, cReadRecords) != 0)
 
192
          {
 
193
            ERR(transactions[t]->getNdbError());
 
194
            pNdb->closeTransaction(transactions[t]);
 
195
            return NDBT_FAILED;
 
196
          }         
 
197
          // Define attributes to read  
 
198
          for (a = 0; a < tab.getNoOfColumns(); a++) {
 
199
            if ((rows[cReadIndex]->attributeStore(a) = 
 
200
                 pOp->getValue(tab.getColumn(a)->getName())) == 0) {
 
201
              ERR(transactions[t]->getNdbError());
 
202
              pNdb->closeTransaction(transactions[t]);
 
203
              return NDBT_FAILED;
 
204
            }
 
205
          }               
 
206
          cReadIndex++;
 
207
          cReadRecords++;
 
208
          
 
209
        } // For each operation
 
210
        
 
211
        // Let's prepare...
 
212
        transactions[t]->executeAsynchPrepare(NoCommit, &asynchCallback, 
 
213
                                        this);
 
214
        cTrans++;
 
215
 
 
216
        if (cReadRecords >= records) {
 
217
          // No more transactions needed
 
218
          break;
 
219
        }      
 
220
      } // For each transaction
 
221
 
 
222
      // Wait for all outstanding transactions
 
223
      pNdb->sendPollNdb(3000, 0, 0);
 
224
 
 
225
      // Verify the data!
 
226
      for (r = 0; r < trans*operations; r++) {
 
227
        if (calc.verifyRowValues(rows[r]) != 0) {
 
228
          g_info << "|- Verify failed..." << endl;
 
229
          // Close all transactions
 
230
          for (int t = 0; t < cTrans; t++) {
 
231
            pNdb->closeTransaction(transactions[t]);
 
232
          }
 
233
          return NDBT_FAILED;
 
234
        }
 
235
      } 
 
236
 
 
237
      // Update
 
238
      cTrans = 0;
 
239
      cIndex = 0;
 
240
      for (t = 0; t < trans; t++) { // For each transaction
 
241
        for (int k = 0; k < operations; k++) { // For each operation
 
242
          NdbOperation* pOp = transactions[t]->getNdbOperation(tab.getName());
 
243
          if (pOp == NULL) { 
 
244
            ERR(transactions[t]->getNdbError());
 
245
            pNdb->closeTransaction(transactions[t]);
 
246
            return NDBT_FAILED;
 
247
          }
 
248
          
 
249
          int updates = calc.getUpdatesValue(rows[cIndex]) + 1;
 
250
 
 
251
          check = pOp->updateTuple();
 
252
          if (check == -1) {
 
253
            ERR(transactions[t]->getNdbError());
 
254
            pNdb->closeTransaction(transactions[t]);
 
255
              return NDBT_FAILED;
 
256
          }
 
257
 
 
258
          // Set search condition for the record
 
259
          if (equalForRow(pOp, cReadRecords) != 0)
 
260
          {
 
261
            ERR(transactions[t]->getNdbError());
 
262
            pNdb->closeTransaction(transactions[t]);
 
263
            return NDBT_FAILED;
 
264
          }         
 
265
 
 
266
          // Update the record
 
267
          for (a = 0; a < tab.getNoOfColumns(); a++) {
 
268
            if (tab.getColumn(a)->getPrimaryKey() == false) {
 
269
              if (setValueForAttr(pOp, a, cRecords, updates) != 0) {
 
270
                ERR(transactions[t]->getNdbError());
 
271
                pNdb->closeTransaction(transactions[t]);
 
272
                return NDBT_FAILED;
 
273
              }
 
274
            }
 
275
          }       
 
276
          cIndex++;
 
277
          cRecords++;
 
278
          
 
279
        } // For each operation
 
280
        
 
281
        // Let's prepare...
 
282
        transactions[t]->executeAsynchPrepare(Commit, &asynchCallback, 
 
283
                                        this);
 
284
        cTrans++;
 
285
 
 
286
        if (cRecords >= records) {
 
287
          // No more transactions needed
 
288
          break;
 
289
        }      
 
290
      } // For each transaction
 
291
 
 
292
      // Wait for all outstanding transactions
 
293
      pNdb->sendPollNdb(3000, 0, 0);
 
294
 
 
295
      // Close all transactions
 
296
      for (t = 0; t < cTrans; t++) {
 
297
        pNdb->closeTransaction(transactions[t]);
 
298
      }
 
299
 
 
300
    } // while (cRecords < records*batch)
 
301
 
 
302
  } // For each batch
 
303
 
 
304
  deallocTransactions();
 
305
  deallocRows();
 
306
  
 
307
  g_info << "|- " << ((unsigned int)transactionsCompleted * operations)/2 
 
308
         << " updated..." << endl;
 
309
  return NDBT_OK;
 
310
}
 
311
 
 
312
void 
 
313
HugoAsynchTransactions::allocTransactions(int trans) {
 
314
  if (transactions != NULL) {
 
315
    deallocTransactions(); 
 
316
  }
 
317
  numTransactions = trans;
 
318
  transactions = new NdbConnection*[numTransactions];  
 
319
}
 
320
 
 
321
void 
 
322
HugoAsynchTransactions::deallocTransactions() {
 
323
  if (transactions != NULL){
 
324
    delete[] transactions;
 
325
  }
 
326
  transactions = NULL;
 
327
}
 
328
 
 
329
int 
 
330
HugoAsynchTransactions::executeAsynchOperation(Ndb* pNdb,                     
 
331
                                         int records,
 
332
                                         int batch,
 
333
                                         int trans,
 
334
                                         int operations,
 
335
                                         NDB_OPERATION theOperation,
 
336
                                         ExecType theType) {
 
337
 
 
338
  int             check = 0;
 
339
  //  int             retryAttempt = 0;  // Not used at the moment
 
340
  //  int             retryMax = 5;      // Not used at the moment
 
341
  int             cTrans = 0;
 
342
  int             cRecords = 0;
 
343
  int             cIndex = 0;
 
344
  int a,t,r;
 
345
 
 
346
  transactionsCompleted = 0;
 
347
  allocTransactions(trans);
 
348
 
 
349
  for (int i = 0; i < batch; i++) { // For each batch
 
350
    while (cRecords < records*batch) {
 
351
      cTrans = 0;
 
352
      cIndex = 0;
 
353
      for (t = 0; t < trans; t++) { // For each transaction
 
354
        transactions[t] = pNdb->startTransaction();
 
355
        if (transactions[t] == NULL) {
 
356
          ERR(pNdb->getNdbError());
 
357
          return NDBT_FAILED;
 
358
        }       
 
359
        for (int k = 0; k < operations; k++) { // For each operation
 
360
          NdbOperation* pOp = transactions[t]->getNdbOperation(tab.getName());
 
361
          if (pOp == NULL) { 
 
362
            ERR(transactions[t]->getNdbError());
 
363
            pNdb->closeTransaction(transactions[t]);
 
364
            return NDBT_FAILED;
 
365
          }
 
366
          
 
367
          switch (theOperation) {
 
368
          case NO_INSERT: 
 
369
            // Insert
 
370
            check = pOp->insertTuple();
 
371
            if (check == -1) { 
 
372
              ERR(transactions[t]->getNdbError());
 
373
              pNdb->closeTransaction(transactions[t]);
 
374
              return NDBT_FAILED;
 
375
            }
 
376
            
 
377
            // Set a calculated value for each attribute in this table   
 
378
            for (a = 0; a < tab.getNoOfColumns(); a++) {
 
379
              if (setValueForAttr(pOp, a, cRecords, 0 ) != 0) {   
 
380
                ERR(transactions[t]->getNdbError());
 
381
                pNdb->closeTransaction(transactions[t]);          
 
382
                return NDBT_FAILED;
 
383
              }
 
384
            } // For each attribute
 
385
            break;
 
386
          case NO_UPDATE:
 
387
            // This is a special case and is handled in the calling client...
 
388
            break;
 
389
          break;
 
390
          case NO_READ:
 
391
            // Define primary keys
 
392
            check = pOp->readTuple();
 
393
            if (equalForRow(pOp, cRecords) != 0)
 
394
            {
 
395
              ERR(transactions[t]->getNdbError());
 
396
              pNdb->closeTransaction(transactions[t]);
 
397
              return NDBT_FAILED;
 
398
            }       
 
399
            // Define attributes to read  
 
400
            for (a = 0; a < tab.getNoOfColumns(); a++) {
 
401
              if ((rows[cIndex]->attributeStore(a) = 
 
402
                   pOp->getValue(tab.getColumn(a)->getName())) == 0) {
 
403
                ERR(transactions[t]->getNdbError());
 
404
                pNdb->closeTransaction(transactions[t]);
 
405
                return NDBT_FAILED;
 
406
              }
 
407
            }             
 
408
            break;
 
409
          case NO_DELETE:
 
410
            // Delete
 
411
            check = pOp->deleteTuple();
 
412
            if (check == -1) { 
 
413
              ERR(transactions[t]->getNdbError());
 
414
              pNdb->closeTransaction(transactions[t]);
 
415
              return NDBT_FAILED;
 
416
            }
 
417
 
 
418
            // Define primary keys
 
419
            if (equalForRow(pOp, cRecords) != 0)
 
420
            {
 
421
              ERR(transactions[t]->getNdbError());
 
422
              pNdb->closeTransaction(transactions[t]);
 
423
              return NDBT_FAILED;
 
424
            }    
 
425
            break;
 
426
          default:
 
427
            // Should not happen...
 
428
            pNdb->closeTransaction(transactions[t]);            
 
429
            return NDBT_FAILED;
 
430
          }
 
431
 
 
432
          cIndex++;
 
433
          cRecords++;
 
434
 
 
435
        } // For each operation
 
436
    
 
437
        // Let's prepare...
 
438
        transactions[t]->executeAsynchPrepare(theType, &asynchCallback, 
 
439
                                        this);
 
440
        cTrans++;
 
441
 
 
442
        if (cRecords >= records) {
 
443
          // No more transactions needed
 
444
          break;
 
445
        }      
 
446
      } // For each transaction
 
447
 
 
448
      // Wait for all outstanding transactions
 
449
      pNdb->sendPollNdb(3000, 0, 0);
 
450
 
 
451
      // ugly... it's starts to resemble flexXXX ...:(
 
452
      switch (theOperation) {
 
453
      case NO_READ:
 
454
        // Verify the data!
 
455
        for (r = 0; r < trans*operations; r++) {
 
456
          if (calc.verifyRowValues(rows[r]) != 0) {
 
457
            g_info << "|- Verify failed..." << endl;
 
458
            // Close all transactions
 
459
            for (int t = 0; t < cTrans; t++) {
 
460
              pNdb->closeTransaction(transactions[t]);
 
461
            }
 
462
            return NDBT_FAILED;
 
463
          }
 
464
        }       
 
465
        break;
 
466
      case NO_INSERT:
 
467
      case NO_UPDATE:
 
468
      case NO_DELETE:
 
469
        break;
 
470
      }
 
471
 
 
472
      // Close all transactions
 
473
      for (t = 0; t < cTrans; t++) {
 
474
        pNdb->closeTransaction(transactions[t]);
 
475
      }
 
476
 
 
477
    } // while (cRecords < records*batch)
 
478
 
 
479
  } // For each batch
 
480
 
 
481
  deallocTransactions();
 
482
 
 
483
  return NDBT_OK;
 
484
 
 
485
}