failoversci.cpp

来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 864 行 · 第 1/2 页

CPP
864
字号
/* 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 <ndb_global.h>#include "sisci_types.h"#include "sisci_api.h"#include "sisci_error.h"//#include "sisci_demolib.h"#include <NdbTick.h>#include <NdbSleep.h>#define NO_CALLBACK         NULL#define NO_FLAGS            0#define DATA_TRANSFER_READY 8sci_error_t             error;sci_desc_t              sdOne;sci_desc_t              sdTwo;sci_local_segment_t     localSegmentOne;sci_local_segment_t     localSegmentTwo;sci_remote_segment_t    remoteSegmentOne;sci_remote_segment_t    remoteSegmentTwo;sci_map_t               localMapOne;sci_map_t               localMapTwo;sci_map_t               remoteMapOne;sci_map_t               remoteMapTwo;unsigned int            localAdapterNo = 0;unsigned int            standbyAdapterNo = 1;unsigned int            localNodeId1;unsigned int            localNodeId2;unsigned int            remoteNodeId1 = 0;unsigned int            remoteNodeId2 = 0;unsigned int            localSegmentId;unsigned int            remoteSegmentId1;unsigned int            remoteSegmentId2;unsigned int            segmentSize = 8192;unsigned int            offset = 0;unsigned int            client = 0;unsigned int            server = 0;unsigned int            *localbufferPtr;static int data;static int interruptConnected=0;/*********************************************************************************//*                                U S A G E                                      *//*                                                                               *//*********************************************************************************/void Usage(){    printf("Usage of shmem\n");    printf("shmem -rn <remote node-id> -client/server [ -adapterno <adapter no> -size <segment size> ] \n\n");    printf(" -rn               : Remote node-id\n");    printf(" -client           : The local node is client\n");    printf(" -server           : The local node is server\n");    printf(" -adapterno        : Local adapter number (default %d)\n", localAdapterNo);    printf(" -size             : Segment block size   (default %d)\n", segmentSize);    printf(" -help             : This helpscreen\n");    printf("\n");}/*********************************************************************************//*                   P R I N T   P A R A M E T E R S                             *//*                                                                               *//*********************************************************************************/void PrintParameters(void){    printf("Test parameters for %s \n",(client) ?  "client" : "server" );    printf("----------------------------\n\n");    printf("Local node-id1      : %d\n",localNodeId1);    printf("Local node-id2      : %d\n",localNodeId2);    //    printf("Remote node-id     : %d\n",remoteNodeId);    printf("Local adapter no.  : %d\n",localAdapterNo);    printf("Segment size       : %d\n",segmentSize);    printf("----------------------------\n\n");}/*********************************************************************************//*                F I L L   S E G M E N T   W I T H   D A T A                    *//*                                                                               *//*********************************************************************************/sci_error_t GetLocalNodeId(Uint32 localAdapterNo, Uint32* localNodeId){  sci_query_adapter_t queryAdapter;  sci_error_t  error;  unsigned int _localNodeId;    queryAdapter.subcommand = SCI_Q_ADAPTER_NODEID;  queryAdapter.localAdapterNo = localAdapterNo;  queryAdapter.data = &_localNodeId;  SCIQuery(SCI_Q_ADAPTER,&queryAdapter,NO_FLAGS,&error);    *localNodeId=_localNodeId;    return error;}sci_error_t SendInterrupt(sci_desc_t   sd,	      Uint32 localAdapterNo, 	      Uint32 localSciNodeId, 	      Uint32 remoteSciNodeId,	      Uint32 interruptNo){    sci_error_t             error;  sci_remote_interrupt_t  remoteInterrupt;  Uint32 timeOut = SCI_INFINITE_TIMEOUT;    // Now connect to the other sides interrupt flag     do {      SCIConnectInterrupt(sd, &remoteInterrupt, remoteSciNodeId, localAdapterNo,			  interruptNo, timeOut, NO_FLAGS, &error);    } while (error != SCI_ERR_OK);        if (error != SCI_ERR_OK) {      fprintf(stderr, "SCIConnectInterrupt failed - Error code 0x%x\n", error);      return error;    }   // Trigger interrupt  printf("\nNode %u sent interrupt (0x%x) to node %d\n",localSciNodeId, interruptNo, remoteSciNodeId);  SCITriggerInterrupt(remoteInterrupt, NO_FLAGS, &error);  if (error != SCI_ERR_OK) {    fprintf(stderr, "SCITriggerInterrupt failed - Error code 0x%x\n", error);    return error;  }        // Disconnect and remove interrupts     SCIDisconnectInterrupt(remoteInterrupt, NO_FLAGS, &error);    if (error != SCI_ERR_OK) {      fprintf(stderr, "SCIDisconnectInterrupt failed - Error code 0x%x\n", error);      return error;     }      return error;}sci_error_t ReceiveInterrupt(sci_desc_t sd,		 Uint32 localAdapterNo,		 Uint32 localSciNodeId, 		 Uint32 interruptNo,		 Uint32 timeout) {    sci_error_t             error;  sci_local_interrupt_t   localInterrupt;  Uint32 timeOut = SCI_INFINITE_TIMEOUT;  // Create an interrupt   SCICreateInterrupt(sd, &localInterrupt, localAdapterNo,		       &interruptNo, 0, NULL, SCI_FLAG_FIXED_INTNO, &error);  if (error != SCI_ERR_OK) {    fprintf(stderr, "SCICreateInterrupt failed - Error code 0x%x\n", error);    return error;   }        // Wait for an interrupt   SCIWaitForInterrupt(localInterrupt, timeOut, NO_FLAGS, &error);  printf("\nNode %u received interrupt (0x%x)\n", localSciNodeId, interruptNo);    // Remove interrupt     SCIRemoveInterrupt(localInterrupt, NO_FLAGS, &error);  if (error != SCI_ERR_OK) {    fprintf(stderr, "SCIRemoveInterrupt failed - Error code 0x%x\n", error);    return error;   }  return error;}sci_error_t FillSegmentWithData(unsigned int segmentSize, int reverse){    unsigned int i;    unsigned int nostores;        nostores = (segmentSize) / sizeof(unsigned int);        /* Allocate buffer */        localbufferPtr = (unsigned int*)malloc( segmentSize );    if ( localbufferPtr == NULL ) {        /*         * Unable to create local buffer - Insufficient memory available         */        return SCI_ERR_NOSPC;    }    if(reverse) {      /* Fill in the data into a local buffer */      printf("Filling forward order \n");      for (i=0;i<nostores;i++) {	localbufferPtr[i] = i;      }    }    else {      int temp=nostores;      printf("Filling reverse order \n");      for (i=0;i<nostores;i++) {	localbufferPtr[i] = temp-- ;      }         }        return SCI_ERR_OK;}/*********************************************************************************//*                    P R I N T   C L I E N T   D A T A                          *//*                                                                               *//*********************************************************************************/void PrintClientData(void){    unsigned int i;    printf("\nClient data: ");    /* Print the first 20 entries in the segment */    for (i=0;i<20;i++) {        printf("%d ",localbufferPtr[i]);    }    printf("\n");}/*********************************************************************************//*                    P R I N T   S E R V E R   D A T A                          *//*                                                                               *//*********************************************************************************/void PrintServerData(volatile unsigned int *localMapAddr){     unsigned int *buffer;     int i;    //    printf("\nServer data: ");    buffer = (unsigned int *)localMapAddr;    /* Print the first 20 entries in the segment */    for (i=0; i< 20; i++) {                printf("%d ",buffer[i]);    }    printf("\n");}/*********************************************************************************//*                       T R A N S F E R   D A T A                               *//*                                                                               *//*********************************************************************************/unsigned int TransferData(sci_map_t             remoteMap,                          volatile unsigned int *remoteSegmentAddr1, 			  volatile unsigned int *remoteSegmentAddr2,                           unsigned int          segmentSize){    volatile unsigned int   *remoteBuffer1;  volatile unsigned int   *remoteBuffer;  volatile unsigned int   *remoteBuffer2;  static int times = 0;    sci_sequence_t          sequence;    sci_error_t             error;    unsigned int            nostores;    unsigned int            j;    sci_sequence_status_t   sequenceStatus;    remoteBuffer1 = (volatile unsigned int *)remoteSegmentAddr1;    remoteBuffer2 = (volatile unsigned int *)remoteSegmentAddr2;    remoteBuffer=remoteBuffer1;	/* 4-byte test only */ 	nostores = (segmentSize) / sizeof(unsigned int);    /* Create a sequence for data error checking */        SCICreateMapSequence(remoteMapOne,&sequence,NO_FLAGS,&error);    if (error != SCI_ERR_OK) {        fprintf(stderr,"SCICreateMapSequence failed - Error code 0x%x\n",error);        return error;    }    /* Fill in the data into a local buffer */    error = SendInterrupt(sdOne,localAdapterNo,localNodeId1,remoteNodeId1, DATA_TRANSFER_READY);    error = FillSegmentWithData(segmentSize, 0); tryagain:    PrintServerData(localbufferPtr);    fprintf(stderr,"After recover \n");    while(1){      //data=0;          if (error != SCI_ERR_OK) {        /*         * Unable to create local buffer - Insufficient memory available         */        printf( "Unable to create local buffer - Insufficient memory available\n" );	        return error;      }      do {        /* Start data error checking */        sequenceStatus = SCIStartSequence(sequence,NO_FLAGS,&error);      } while (sequenceStatus != SCI_SEQ_OK) ;                      /* Transfer data to remote node */      for (j=0;j<nostores;j++) {	remoteBuffer[j] = localbufferPtr[j];      }            /* Check for error after data transfer */      sequenceStatus = SCICheckSequence(sequence,NO_FLAGS,&error);      if (sequenceStatus != SCI_SEQ_OK) {        fprintf(stderr,"Data transfer failed\n");	if(times==0) {	  error = FillSegmentWithData(segmentSize, 1);	  	  SCICreateMapSequence(remoteMapTwo,&sequence,NO_FLAGS,&error);	  if (error != SCI_ERR_OK) {	    fprintf(stderr,"SCICreateMapSequence failed - Error code 0x%x\n",error);	    return error;	    return SCI_ERR_TRANSFER_FAILED;	  }	}	else 	  {	    error = FillSegmentWithData(segmentSize, 0);	    /* Create a sequence for data error checking */    	    SCICreateMapSequence(remoteMapOne,&sequence,NO_FLAGS,&error);	    if (error != SCI_ERR_OK) {	      fprintf(stderr,"SCICreateMapSequence failed - Error code 0x%x\n",error);	      return error;	      return SCI_ERR_TRANSFER_FAILED;	}	    	  }	fprintf(stderr,"Recovery \n");	if(times==0)	  remoteBuffer=remoteBuffer2;	else	  remoteBuffer=remoteBuffer1;	times++;	printf("remotebuffer %p   times %d\n", remoteBuffer, times);	goto tryagain;      }          int timeout=0;      //      error = SendInterrupt(sdOne,localAdapterNo,localNodeId1,remoteNodeId1, DATA_TRANSFER_READY);      //      NdbSleep_MilliSleep(100);      //error = ReceiveInterrupt(sdOne,localAdapterNo,localNodeId1,DATA_TRANSFER_READY, timeout);    }    /* Remove the Sequence */    SCIRemoveSequence(sequence,NO_FLAGS, &error);    if (error != SCI_ERR_OK) {      fprintf(stderr,"SCIRemoveSequence failed - Error code 0x%x\n",error);      return error;    }        return SCI_ERR_OK;}/*********************************************************************************//*                    S H M E M   C L I E N T   N O D E                          *//*                                                                               *//*********************************************************************************/unsigned int ShmemClientNode(void){    volatile unsigned int *remoteMapAddr1;    volatile unsigned int *remoteMapAddr2;    printf("here?\n");        /* Create a segmentId */    remoteSegmentId1 = 1;//(remoteNodeId1 << 16) | localNodeId1;    /* Connect to remote segment */    printf("Connect to remote segment ....  \n");    printf("segid = %d  node %d \n",remoteSegmentId1, remoteNodeId1 );     do {       SCIConnectSegment(sdOne,			&remoteSegmentOne,			remoteNodeId1,

⌨️ 快捷键说明

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