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

📄 master.cpp

📁 extremeDB s sample code,useful for you
💻 CPP
字号:
/***************************************************************** *                                                               * * This file is a part of the eXtremeDB-HA Application Framework * * Copyright (c) 2001-2006 McObject LLC                          *  * All Rights Reserved                                           * *                                                               *     ***************************************************************** * ++ *  * PROJECT:   eXtremeDB(tm) (c) 2003 McObject LLC * * SUBSYSTEM: HA framework * * MODULE:    master.cpp * * ABSTRACT:  C++ implementation of master mode * * * VERSION:   1.0 * * HISTORY: *            1.0- 1 SS     18-Feb-2004 Created it was * * -- * *****************************************************************/#include  "mcoha.hpp"#include  "watchdog.h"/****************************************************** * Master Mode * * Create master's databases (or connect to the main master's databases) * and run master mode.  ******************************************************/int   App::MasterMode(){  int           i,j;  MCO_RET       rc;  FILE					* f;    Printf("\n*** Master is being started ");    if(!main_master) {/********************************************************************** * slave master   * attempt to connect to existing databases **********************************************************************/      Printf("in \"slave master\" mode ***\n");      	    for (i=0, j = 0; i < NumberOfDb; i++) {          /* connect to the database, obtain a database handle */        if( (DbInstances[j] = ConnectToDatabase( i )) != 0) {            j++;        }      }            if((NumberOfDb=j) == 0) {        Printf("\ncould not find any existing database!\n\n");	      KILL(); 	      EXIT(-1);      }      SET_PROCESS_PRIORITY(NORMAL_PRIORITY-2);      Printf("\n %d existing databases are found\n", j);    }    else { // isMainMaster  /*******************************************************************************   * "main" master   *   *  if this is the "main" master process, it creates a new database.   *  The "main" master process must also create a separate thread for the shared commit   *  if it works in the MULIMASTER_SHM mode   *******************************************************************************/      Printf("in \"main master\" mode ***\n");      Printf("\ncreating the new database(s)\n");      /* connect to the databases, obtain  database's handles */	    for (i=0, j = 0; i < NumberOfDb; i++) {        DbInstances[i] = CreateDatabase( i );      }    }    t_start = GetCurrentTime();    Master( 1 );   // flag = 1 - database initialization    t_stop = GetCurrentTime();  /* output data for comparison with replica */    Printf("\nMaster: initializing verification:\n");      if((f = fopen("monitor.out", "w"))==0) {      Printf ("  Master:  error opening file monitor.out\n");    }    else {      Printf ("  Master: file monitor.out is opened\n");      for( i=0; i < NumberOfDb; i++) {        Database* Db;        Db = DbInstances[i];        db = Db->db;        if ( (rc=mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &Db->t )) )        {          Printf( "\nMaster: error starting read transaction %d\n", rc );          continue;        }        t = Db->t;        fprintf(f,"database instance %d\n", i);        Printf("writing data...\n");        Db->s1.output(f);        Printf ("  Master: Sensor1 data is written\n");        mco_trans_commit(t);      }      fclose( f );    }    t_end = mco_system_get_current_time();      t_end -= t_stop;    t_stop -= t_start;    t_start -= t_begin;    Printf("\n\n"      "Initialization time:%7d.%03d\n"      "Processing time:    %7d.%03d\n"      "Shutdown time:     %7d.%03d\n"      "Saving database to replica time: %ld ms\n",      (int)t_start/1000,(int) t_start%1000,      (int)t_stop/1000, (int)t_stop%1000,      (int)t_end/1000, (int)t_end%1000,      init_time      );  /********************************************************************   * you may insert here some code for notifying 'slave' master processes   * of termination of the 'main' master program in multimaster mode   ********************************************************************/      ExitHandler(0);    /* terminate the program gracefully */      KILL();     return  0;}/* * Runs master mode. Creates database objects, then performs * database transactions in a loop */void   App::Master(int flag){  int             i,j;  MCO_RET         rc;  unsigned long   counter;  int2            nr;  mco_uquad       auto_oid;  timer_unit      time = 0, time1,time3 = 0;//, time2  Database*       Db;  int             curr_id = 0;    mode.max_number_of_replicas = 3;#ifdef CFG_SHARED_COMMIT    mode.mode_flags = MCO_MULTIPROCESS_COMMIT;#else    mode.mode_flags = MCO_YES;#endif    /* build so many databases as it is set in the option in command line */  Printf( "\n\nBuilding the database(s), please wait\n" );  for(i=0; i < NumberOfDb; i++) {    Db  = DbInstances[i];    db  = Db->db;    Db->SetHAmode( &mode);  // set MASTER mode    /* fill out the database with some initial data */    if(flag) {        Printf("\n *** Database %d ***", Db->id);        Printf("\nMaster: populating initial data for Sensor1...");        Db->s1.init(S1_RECORDS);        wdt_flag = 0;        Printf("done, still available %dK\n",Db->free_mem( 0 ));    }    GetCurrentAutoOid(&auto_oid);    Printf("Starting with auto oid: %d:%d\n",(int) auto_oid.hi,(int) auto_oid.lo);  }  /* main loop - switch on sensors */  Printf( "\nMaster: processing data, press Enter to terminate... \n");  time1 = GetCurrentTime();  for (i=0, counter=1;; counter++, i++ )  {#ifdef PRINT_REPORTS    if(!i) time  = GetCurrentTime();#endif    for(j=0; j < NumberOfDb; j++) {      Db  = DbInstances[j];      db  = Db->db;      wdt_flag = 0;    // clear commit watchdog flag      if ( (rc=Db->StartTransaction(MCO_READ_WRITE, MCO_TRANS_FOREGROUND)) )      {         Printf( "\nerror starting write transaction %d\n", rc );        exit( 1 );      }      t = Db->t;      Db->s1.new_record( 1000000+counter );      if (stop_flag)      {        rc = TransRollback();        break;      }      if(MCO_S_OK != (rc = Db->TransCommit()))      {         Printf( "\nMaster: error commiting transaction %d\n", rc );      }      wdt_flag = 0;    // clear commit watchdog flag    }    if (stop_flag)  break;#ifdef PRINT_REPORTS    if(i==REPORT_COUNTER) {      if (main_master) {        Db->KeepAlive(10000);   // send signal "KEEP_ALIVE" to replicas                                // in order to give them sign that master is "alive"      }      if( (time = GetCurrentTime() - time) ==0) time++;	    time3 = (time3 + (1000*i/time))/2;      Printf("%ld ms for %d transactions (%d TPS);",time/NumberOfDb, i, (int)time3*NumberOfDb); //(int)1000*i/time      i = -1;      nr = DbInstances[curr_id]->GetNumberOfReplicas();      Printf(" number of replicas: (%d) %d \n",curr_id+1, nr);      if(++curr_id >= NumberOfDb ) curr_id = 0;    }#endif      }  Printf("\n*** Master complete ***\n");  Printf ("\n\n");  {    mco_uquad auto_oid;    DbInstances[0]->GetCurrentAutoOid(&auto_oid);    Printf("Communication over %s channel, last autoid = %d:%d\n",      CHANNEL_NAME, (int)auto_oid.hi, (int)auto_oid.lo);  }}

⌨️ 快捷键说明

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