mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / test / ndbapi / bench / userInterface.cpp
blobbf903c189d4c0628d3508d35ff38f06bb77a5568
1 /* Copyright (c) 2003, 2005 MySQL AB
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 /***************************************************************
17 * I N C L U D E D F I L E S *
18 ***************************************************************/
20 #include <ndb_global.h>
21 #ifndef NDB_WIN32
22 #include <sys/time.h>
23 #endif
25 #include "ndb_error.hpp"
26 #include "userInterface.h"
27 #include <NdbThread.h>
28 #include <NdbTick.h>
29 #include <NdbMutex.h>
30 #include <NdbSleep.h>
31 #include "ndb_schema.hpp"
32 #include <NDBT.hpp>
33 #include <NdbSchemaCon.hpp>
35 /***************************************************************
36 * L O C A L C O N S T A N T S *
37 ***************************************************************/
39 /***************************************************************
40 * L O C A L D A T A S T R U C T U R E S *
41 ***************************************************************/
43 /***************************************************************
44 * L O C A L F U N C T I O N S *
45 ***************************************************************/
47 extern int localDbPrepare(UserHandle *uh);
49 static int dbCreate(UserHandle *uh);
51 /***************************************************************
52 * L O C A L D A T A *
53 ***************************************************************/
55 /***************************************************************
56 * P U B L I C D A T A *
57 ***************************************************************/
60 /***************************************************************
61 ****************************************************************
62 * L O C A L F U N C T I O N S C O D E S E C T I O N *
63 ****************************************************************
64 ***************************************************************/
66 /***************************************************************
67 ****************************************************************
68 * P U B L I C F U N C T I O N S C O D E S E C T I O N *
69 ****************************************************************
70 ***************************************************************/
72 /*-----------------------------------*/
73 /* Time related Functions */
74 /* */
75 /* Returns a double value in seconds */
76 /*-----------------------------------*/
77 double userGetTimeSync(void)
79 static int initialized = 0;
80 static NDB_TICKS initSecs = 0;
81 static Uint32 initMicros = 0;
82 double timeValue = 0;
84 if ( !initialized ) {
85 initialized = 1;
86 NdbTick_CurrentMicrosecond(&initSecs, &initMicros);
87 timeValue = 0.0;
88 } else {
89 NDB_TICKS secs = 0;
90 Uint32 micros = 0;
92 NdbTick_CurrentMicrosecond(&secs, &micros);
94 double s = (double)secs - (double)initSecs;
95 double us = (double)secs - (double)initMicros;
97 timeValue = s + (us / 1000000.0);
100 return timeValue;
103 // 0 - OK
104 // 1 - Retry transaction
105 // 2 - Permanent
106 int
107 userDbCommit(UserHandle *uh){
108 if(uh->pCurrTrans != 0){
109 int check = uh->pCurrTrans->execute( Commit );
110 NdbError err = uh->pCurrTrans->getNdbError();
111 uh->pNDB->closeTransaction(uh->pCurrTrans);
112 uh->pCurrTrans = 0;
114 if(err.status != NdbError::Success)
115 ndbout << err << endl;
117 if(err.status == NdbError::TemporaryError &&
118 err.classification == NdbError::OverloadError){
119 NdbSleep_SecSleep(3);
122 return err.status;
124 return 2;
128 * TRUE - Normal table
129 * FALSE - Table w.o. checkpoing and logging
132 #ifdef __cplusplus
133 extern "C" {
134 #endif
135 extern int useTableLogging;
136 #ifdef __cplusplus
138 #endif
142 create_table_server(Ndb * pNdb){
143 int check;
144 NdbSchemaCon * MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
145 if( MySchemaTransaction == NULL )
146 error_handler("startSchemaTransaction", pNdb->getNdbError(), 0);
148 NdbSchemaOp * MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
149 if( MySchemaOp == NULL )
150 error_handler("getNdbSchemaOp", MySchemaTransaction->getNdbError(), 0);
152 // Create table
153 check = MySchemaOp->createTable( SERVER_TABLE,
154 8, // Table size
155 TupleKey, // Key Type
156 1 // Nr of Pages
157 ,DistributionGroup,
162 useTableLogging
164 if( check == -1 )
165 error_handler("createTable", MySchemaTransaction->getNdbError(), 0);
167 check = MySchemaOp->createAttribute
168 ( SERVER_SUBSCRIBER_SUFFIX,
169 TupleKey,
170 sizeof(char) << 3,
171 SUBSCRIBER_NUMBER_SUFFIX_LENGTH,
172 String,
173 MMBased,
174 NotNullAttribute,
178 16);
179 if( check == -1 )
180 error_handler("createAttribute (subscriber suffix)",
181 MySchemaTransaction->getNdbError(), 0);
183 // Create first column, primary key
184 check = MySchemaOp->createAttribute( SERVER_ID,
185 TupleKey,
186 sizeof(ServerId) << 3,
188 UnSigned,
189 MMBased,
190 NotNullAttribute );
191 if( check == -1 )
192 error_handler("createAttribute (serverid)",
193 MySchemaTransaction->getNdbError(), 0);
196 check = MySchemaOp->createAttribute( SERVER_NAME,
197 NoKey,
198 sizeof(char) << 3,
199 SERVER_NAME_LENGTH,
200 String,
201 MMBased,
202 NotNullAttribute );
203 if( check == -1 )
204 error_handler("createAttribute (server name)",
205 MySchemaTransaction->getNdbError(), 0);
208 check = MySchemaOp->createAttribute( SERVER_READS,
209 NoKey,
210 sizeof(Counter) << 3,
212 UnSigned,
213 MMBased,
214 NotNullAttribute );
215 if( check == -1 )
216 error_handler("createAttribute (server reads)",
217 MySchemaTransaction->getNdbError(), 0);
219 check = MySchemaOp->createAttribute( SERVER_INSERTS,
220 NoKey,
221 sizeof(Counter) << 3,
223 UnSigned,
224 MMBased,
225 NotNullAttribute );
226 if( check == -1 )
227 error_handler("createAttribute (server inserts)",
228 MySchemaTransaction->getNdbError(), 0);
230 check = MySchemaOp->createAttribute( SERVER_DELETES,
231 NoKey,
232 sizeof(Counter) << 3,
234 UnSigned,
235 MMBased,
236 NotNullAttribute );
237 if( check == -1 )
238 error_handler("createAttribute (server deletes)",
239 MySchemaTransaction->getNdbError(), 0);
241 if( MySchemaTransaction->execute() == -1 ) {
242 error_handler("schemaTransaction->execute()",
243 MySchemaTransaction->getNdbError(), 0);
245 NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
246 return 0;
250 create_table_group(Ndb * pNdb){
251 int check;
253 NdbSchemaCon * MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
254 if( MySchemaTransaction == NULL )
255 error_handler("startSchemaTransaction", pNdb->getNdbError(), 0);
257 NdbSchemaOp * MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
258 if( MySchemaOp == NULL )
259 error_handler("getNdbSchemaOp", MySchemaTransaction->getNdbError(), 0);
261 // Create table
262 check = MySchemaOp->createTable( GROUP_TABLE,
263 8, // Table size
264 TupleKey, // Key Type
265 1 // Nr of Pages
266 ,All,
271 useTableLogging
274 if( check == -1 )
275 error_handler("createTable", MySchemaTransaction->getNdbError(), 0);
277 // Create first column, primary key
278 check = MySchemaOp->createAttribute( GROUP_ID,
279 TupleKey,
280 sizeof(GroupId) << 3,
282 UnSigned,
283 MMBased,
284 NotNullAttribute );
285 if( check == -1 )
286 error_handler("createAttribute (group id)",
287 MySchemaTransaction->getNdbError(), 0);
289 check = MySchemaOp->createAttribute( GROUP_NAME,
290 NoKey,
291 sizeof(char) << 3,
292 GROUP_NAME_LENGTH,
293 String,
294 MMBased,
295 NotNullAttribute );
296 if( check == -1 )
297 error_handler("createAttribute (group name)",
298 MySchemaTransaction->getNdbError(), 0);
301 check = MySchemaOp->createAttribute( GROUP_ALLOW_READ,
302 NoKey,
303 sizeof(Permission) << 3,
305 String,
306 MMBased,
307 NotNullAttribute );
308 if( check == -1 )
309 error_handler("createAttribute (group read)",
310 MySchemaTransaction->getNdbError(), 0);
313 check = MySchemaOp->createAttribute( GROUP_ALLOW_INSERT,
314 NoKey,
315 sizeof(Permission) << 3,
317 UnSigned,
318 MMBased,
319 NotNullAttribute );
320 if( check == -1 )
321 error_handler("createAttribute (group insert)",
322 MySchemaTransaction->getNdbError(), 0);
324 check = MySchemaOp->createAttribute( GROUP_ALLOW_DELETE,
325 NoKey,
326 sizeof(Permission) << 3,
328 UnSigned,
329 MMBased,
330 NotNullAttribute );
331 if( check == -1 )
332 error_handler("createAttribute (group delete)",
333 MySchemaTransaction->getNdbError(), 0);
335 if( MySchemaTransaction->execute() == -1 ) {
336 error_handler("schemaTransaction->execute()",
337 MySchemaTransaction->getNdbError(), 0);
339 NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
340 return 0;
344 create_table_subscriber(Ndb * pNdb){
345 int check;
346 NdbSchemaCon * MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
347 if( MySchemaTransaction == NULL )
348 error_handler("startSchemaTransaction", pNdb->getNdbError(), 0);
350 NdbSchemaOp * MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
351 if( MySchemaOp == NULL )
352 error_handler("getNdbSchemaOp", MySchemaTransaction->getNdbError(), 0);
354 // Create table
355 check = MySchemaOp->createTable( SUBSCRIBER_TABLE,
356 8, // Table size
357 TupleKey, // Key Type
358 1 // Nr of Pages
359 ,DistributionGroup,
364 useTableLogging
366 if( check == -1 )
367 error_handler("createTable", MySchemaTransaction->getNdbError(), 0);
369 // Create first column, primary key
370 check = MySchemaOp->createAttribute
371 ( SUBSCRIBER_NUMBER,
372 TupleKey,
373 sizeof(char) << 3,
374 SUBSCRIBER_NUMBER_LENGTH,
375 String,
376 MMBased,
377 NotNullAttribute,
381 16);
382 if( check == -1 )
383 error_handler("createAttribute (subscriber number)",
384 MySchemaTransaction->getNdbError(), 0);
386 check = MySchemaOp->createAttribute( SUBSCRIBER_NAME,
387 NoKey,
388 sizeof(char) << 3,
389 SUBSCRIBER_NAME_LENGTH,
390 String,
391 MMBased,
392 NotNullAttribute );
393 if( check == -1 )
394 error_handler("createAttribute (subscriber name)",
395 MySchemaTransaction->getNdbError(), 0);
398 check = MySchemaOp->createAttribute( SUBSCRIBER_GROUP,
399 NoKey,
400 sizeof(GroupId) << 3,
402 UnSigned,
403 MMBased,
404 NotNullAttribute );
405 if( check == -1 )
406 error_handler("createAttribute (subscriber_group)",
407 MySchemaTransaction->getNdbError(), 0);
410 check = MySchemaOp->createAttribute( SUBSCRIBER_LOCATION,
411 NoKey,
412 sizeof(Location) << 3,
414 UnSigned,
415 MMBased,
416 NotNullAttribute );
417 if( check == -1 )
418 error_handler("createAttribute (server reads)",
419 MySchemaTransaction->getNdbError(), 0);
421 check = MySchemaOp->createAttribute( SUBSCRIBER_SESSIONS,
422 NoKey,
423 sizeof(ActiveSessions) << 3,
425 UnSigned,
426 MMBased,
427 NotNullAttribute );
428 if( check == -1 )
429 error_handler("createAttribute (subscriber_sessions)",
430 MySchemaTransaction->getNdbError(), 0);
432 check = MySchemaOp->createAttribute( SUBSCRIBER_CHANGED_BY,
433 NoKey,
434 sizeof(char) << 3,
435 CHANGED_BY_LENGTH,
436 String,
437 MMBased,
438 NotNullAttribute );
439 if( check == -1 )
440 error_handler("createAttribute (subscriber_changed_by)",
441 MySchemaTransaction->getNdbError(), 0);
443 check = MySchemaOp->createAttribute( SUBSCRIBER_CHANGED_TIME,
444 NoKey,
445 sizeof(char) << 3,
446 CHANGED_TIME_LENGTH,
447 String,
448 MMBased,
449 NotNullAttribute );
450 if( check == -1 )
451 error_handler("createAttribute (subscriber_changed_time)",
452 MySchemaTransaction->getNdbError(), 0);
454 if( MySchemaTransaction->execute() == -1 ) {
455 error_handler("schemaTransaction->execute()",
456 MySchemaTransaction->getNdbError(), 0);
458 NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
459 return 0;
463 create_table_session(Ndb * pNdb){
464 int check;
465 NdbSchemaCon * MySchemaTransaction = NdbSchemaCon::startSchemaTrans(pNdb);
466 if( MySchemaTransaction == NULL )
467 error_handler("startSchemaTransaction", pNdb->getNdbError(), 0);
469 NdbSchemaOp * MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
470 if( MySchemaOp == NULL )
471 error_handler("getNdbSchemaOp",
472 MySchemaTransaction->getNdbError(), 0);
474 // Create table
475 check = MySchemaOp->createTable( SESSION_TABLE,
476 8, // Table size
477 TupleKey, // Key Type
478 1 // Nr of Pages
479 ,DistributionGroup,
484 useTableLogging
486 if( check == -1 )
487 error_handler("createTable", MySchemaTransaction->getNdbError(), 0);
489 check = MySchemaOp->createAttribute( SESSION_SUBSCRIBER,
490 TupleKey,
491 sizeof(char) << 3,
492 SUBSCRIBER_NUMBER_LENGTH,
493 String,
494 MMBased,
495 NotNullAttribute,
499 16);
500 if( check == -1 )
501 error_handler("createAttribute (session_subscriber)",
502 MySchemaTransaction->getNdbError(), 0);
504 // Create first column, primary key
505 check = MySchemaOp->createAttribute( SESSION_SERVER,
506 TupleKey,
507 sizeof(ServerId) << 3,
509 UnSigned,
510 MMBased,
511 NotNullAttribute );
512 if( check == -1 )
513 error_handler("createAttribute (session_server)",
514 MySchemaTransaction->getNdbError(), 0);
517 check = MySchemaOp->createAttribute( SESSION_DATA,
518 NoKey,
519 sizeof(char) << 3,
520 SESSION_DETAILS_LENGTH,
521 String,
522 MMBased,
523 NotNullAttribute );
524 if( check == -1 )
525 error_handler("createAttribute (session_data)",
526 MySchemaTransaction->getNdbError(), 0);
528 if( MySchemaTransaction->execute() == -1 ) {
529 error_handler("schemaTransaction->execute()",
530 MySchemaTransaction->getNdbError(), 0);
532 NdbSchemaCon::closeSchemaTrans(MySchemaTransaction);
533 return 0;
536 void
537 create_table(const char * name, int (* function)(Ndb * pNdb), Ndb* pNdb){
538 printf("creating table %s...", name);
539 if(pNdb->getDictionary()->getTable(name) != 0){
540 printf(" it already exists\n");
541 return;
542 } else {
543 printf("\n");
545 function(pNdb);
546 printf("creating table %s... done\n", name);
549 static int dbCreate(Ndb * pNdb)
551 create_table(SUBSCRIBER_TABLE, create_table_subscriber, pNdb);
552 create_table(GROUP_TABLE , create_table_group, pNdb);
553 create_table(SESSION_TABLE , create_table_session, pNdb);
554 create_table(SERVER_TABLE , create_table_server, pNdb);
555 return 0;
558 #ifndef NDB_WIN32
559 #include <unistd.h>
560 #endif
562 UserHandle*
563 userDbConnect(uint32 createDb, char *dbName)
565 Ndb_cluster_connection *con= new Ndb_cluster_connection();
566 if(con->connect(12, 5, 1) != 0)
568 ndbout << "Unable to connect to management server." << endl;
569 return 0;
571 if (con->wait_until_ready(30,0) < 0)
573 ndbout << "Cluster nodes not ready in 30 seconds." << endl;
574 return 0;
577 Ndb * pNdb = new Ndb(con, dbName);
579 //printf("Initializing...\n");
580 pNdb->init();
582 //printf("Waiting...");
583 while(pNdb->waitUntilReady() != 0){
584 //printf("...");
586 // printf("done\n");
588 if( createDb )
589 dbCreate(pNdb);
592 UserHandle * uh = new UserHandle;
593 uh->pNCC = con;
594 uh->pNDB = pNdb;
595 uh->pCurrTrans = 0;
597 return uh;
600 void userDbDisconnect(UserHandle *uh)
602 delete uh;
605 int userDbInsertServer(UserHandle *uh,
606 ServerId serverId,
607 SubscriberSuffix suffix,
608 ServerName name)
610 int check;
612 uint32 noOfRead = 0;
613 uint32 noOfInsert = 0;
614 uint32 noOfDelete = 0;
616 NdbConnection * MyTransaction = 0;
617 if(uh->pCurrTrans != 0){
618 MyTransaction = uh->pCurrTrans;
619 } else {
620 uh->pCurrTrans = MyTransaction = uh->pNDB->startTransaction();
622 if (MyTransaction == NULL)
623 error_handler("startTranscation", uh->pNDB->getNdbError(), 0);
625 NdbOperation *MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE);
626 CHECK_NULL(MyOperation, "getNdbOperation", MyTransaction);
628 check = MyOperation->insertTuple();
629 CHECK_MINUS_ONE(check, "insert tuple", MyTransaction);
631 check = MyOperation->equal(SERVER_ID, (char*)&serverId);
632 CHECK_MINUS_ONE(check, "setValue id", MyTransaction);
634 check = MyOperation->setValue(SERVER_SUBSCRIBER_SUFFIX, suffix);
635 CHECK_MINUS_ONE(check, "setValue suffix", MyTransaction);
637 check = MyOperation->setValue(SERVER_NAME, name);
638 CHECK_MINUS_ONE(check, "setValue name", MyTransaction);
640 check = MyOperation->setValue(SERVER_READS, (char*)&noOfRead);
641 CHECK_MINUS_ONE(check, "setValue reads", MyTransaction);
643 check = MyOperation->setValue(SERVER_INSERTS, (char*)&noOfInsert);
644 CHECK_MINUS_ONE(check, "setValue inserts", MyTransaction);
646 check = MyOperation->setValue(SERVER_DELETES, (char*)&noOfDelete);
647 CHECK_MINUS_ONE(check, "setValue deletes", MyTransaction);
649 return 0;
652 int userDbInsertSubscriber(UserHandle *uh,
653 SubscriberNumber number,
654 uint32 groupId,
655 SubscriberName name)
657 int check;
658 uint32 activeSessions = 0;
659 Location l = 0;
660 ChangedBy changedBy; snprintf(changedBy, sizeof(changedBy), "ChangedBy");
661 ChangedTime changedTime; snprintf(changedTime, sizeof(changedTime), "ChangedTime");
663 NdbConnection * MyTransaction = 0;
664 if(uh->pCurrTrans != 0){
665 MyTransaction = uh->pCurrTrans;
666 } else {
667 uh->pCurrTrans = MyTransaction = uh->pNDB->startTransaction();
669 if (MyTransaction == NULL)
670 error_handler("startTranscation", uh->pNDB->getNdbError(), 0);
672 NdbOperation *MyOperation = MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
673 CHECK_NULL(MyOperation, "getNdbOperation", MyTransaction);
675 check = MyOperation->insertTuple();
676 CHECK_MINUS_ONE(check, "insertTuple", MyTransaction);
678 check = MyOperation->equal(SUBSCRIBER_NUMBER, number);
679 CHECK_MINUS_ONE(check, "equal", MyTransaction);
681 check = MyOperation->setValue(SUBSCRIBER_NAME, name);
682 CHECK_MINUS_ONE(check, "setValue name", MyTransaction);
684 check = MyOperation->setValue(SUBSCRIBER_GROUP, (char*)&groupId);
685 CHECK_MINUS_ONE(check, "setValue group", MyTransaction);
687 check = MyOperation->setValue(SUBSCRIBER_LOCATION, (char*)&l);
688 CHECK_MINUS_ONE(check, "setValue location", MyTransaction);
690 check = MyOperation->setValue(SUBSCRIBER_SESSIONS, (char*)&activeSessions);
691 CHECK_MINUS_ONE(check, "setValue sessions", MyTransaction);
693 check = MyOperation->setValue(SUBSCRIBER_CHANGED_BY, changedBy);
694 CHECK_MINUS_ONE(check, "setValue changedBy", MyTransaction);
696 check = MyOperation->setValue(SUBSCRIBER_CHANGED_TIME, changedTime);
697 CHECK_MINUS_ONE(check, "setValue changedTime", MyTransaction);
699 return 0;
702 int userDbInsertGroup(UserHandle *uh,
703 GroupId groupId,
704 GroupName name,
705 Permission allowRead,
706 Permission allowInsert,
707 Permission allowDelete)
709 int check;
711 NdbConnection * MyTransaction = 0;
712 if(uh->pCurrTrans != 0){
713 MyTransaction = uh->pCurrTrans;
714 } else {
715 uh->pCurrTrans = MyTransaction = uh->pNDB->startTransaction();
717 if (MyTransaction == NULL)
718 error_handler("startTranscation", uh->pNDB->getNdbError(), 0);
720 NdbOperation *MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE);
721 CHECK_NULL(MyOperation, "getNdbOperation", MyTransaction);
723 check = MyOperation->insertTuple();
724 CHECK_MINUS_ONE(check, "insertTuple", MyTransaction);
726 check = MyOperation->equal(GROUP_ID, (char*)&groupId);
727 CHECK_MINUS_ONE(check, "equal", MyTransaction);
729 check = MyOperation->setValue(GROUP_NAME, name);
730 CHECK_MINUS_ONE(check, "setValue name", MyTransaction);
732 check = MyOperation->setValue(GROUP_ALLOW_READ, (char*)&allowRead);
733 CHECK_MINUS_ONE(check, "setValue allowRead", MyTransaction);
735 check = MyOperation->setValue(GROUP_ALLOW_INSERT, (char*)&allowInsert);
736 CHECK_MINUS_ONE(check, "setValue allowInsert", MyTransaction);
738 check = MyOperation->setValue(GROUP_ALLOW_DELETE, (char*)&allowDelete);
739 CHECK_MINUS_ONE(check, "setValue allowDelete", MyTransaction);
741 return 0;