@@ -5297,157 +5297,115 @@ static void parse_dbresponse(CDB2DBINFORESPONSE *dbinfo_response, char valid_hos
52975297static int retry_queries (cdb2_hndl_tp * hndl , int num_retry , int run_last )
52985298{
52995299 debugprint ("retry_all %d, intran %d\n" , hndl -> retry_all , hndl -> in_trans );
5300-
5301- if (!hndl -> retry_all || !hndl -> in_trans )
5302- return 0 ;
5303-
53045300 int rc = 0 ;
5305- if (!(hndl -> snapshot_file || hndl -> query_no <= 1 )) {
5306- debugprint ("in_trans=%d snapshot_file=%d query_no=%d\n" , hndl -> in_trans ,
5307- hndl -> snapshot_file , hndl -> query_no );
5308- sprintf (hndl -> errstr , "Database disconnected while in transaction." );
5309- return CDB2ERR_TRAN_IO_ERROR ; /* Fail if disconnect happens in
5310- transaction which doesn't have snapshot
5311- info.*/
5312- }
5313-
5314- /* Replay all the queries. */
5315- char * host = "NOT-CONNECTED" ;
5316- if (hndl -> connected_host >= 0 )
5317- host = hndl -> hosts [hndl -> connected_host ];
5318-
5319- /*Send Begin. */
5320- hndl -> is_retry = num_retry ;
5321-
5322- clear_responses (hndl );
5323- hndl -> read_intrans_results = 1 ;
5324-
5325- hndl -> in_trans = 0 ;
5326- debugprint ("sending 'begin' to %s\n" , host );
5327- rc = cdb2_send_query (hndl , hndl , hndl -> sb , hndl -> dbname , "begin" ,
5328- hndl -> num_set_commands , hndl -> num_set_commands_sent ,
5329- hndl -> commands , 0 , NULL , 0 , NULL , 1 , 0 , num_retry , 0 ,
5330- __LINE__ );
5331- hndl -> in_trans = 1 ;
5332- debugprint ("cdb2_send_query rc = %d, setting in_trans to 1\n" , rc );
5301+ if (!hndl -> retry_all )
5302+ return 0 ;
5303+ if (hndl -> in_trans && hndl -> snapshot_file ) { /* Replay all the queries. */
5304+ /*Send Begin. */
5305+ hndl -> is_retry = num_retry ;
53335306
5334- if (rc != 0 ) {
5335- sbuf2close (hndl -> sb );
5336- hndl -> sb = NULL ;
5337- debugprint ("send_query host=%s rc=%d; returning 1\n" , host , rc );
5338- return 1 ;
5339- }
5340- int len = 0 ;
5341- int type = 0 ;
5342- rc = cdb2_read_record (hndl , & hndl -> first_buf , & len , & type );
5343- if (rc ) {
5344- sbuf2close (hndl -> sb );
5345- hndl -> sb = NULL ;
5346- debugprint ("cdb2_read_record from %s rc=%d, returning 1\n" , host , rc );
5347- return 1 ;
5348- }
5307+ clear_responses (hndl );
5308+ hndl -> read_intrans_results = 1 ;
5309+ hndl -> in_trans = 0 ;
5310+ debugprint ("sending 'begin'\n" );
5311+ rc = cdb2_send_query (hndl , hndl , hndl -> sb , hndl -> dbname , "begin" , hndl -> num_set_commands ,
5312+ hndl -> num_set_commands_sent , hndl -> commands , 0 , NULL , 0 , NULL , 1 , 0 , num_retry , 0 ,
5313+ __LINE__ );
5314+ debugprint ("cdb2_send_query rc = %d, setting in_trans to 1\n" , rc );
53495315
5350- if (type == RESPONSE_HEADER__DBINFO_RESPONSE ) {
5351- if (hndl -> flags & CDB2_DIRECT_CPU ) {
5352- debugprint ("directcpu will ignore dbinfo\n" );
5316+ if (rc != 0 ) {
5317+ sbuf2close (hndl -> sb );
5318+ hndl -> sb = NULL ;
5319+ debugprint ("send_query rc=%d; returning 1\n" , rc );
53535320 return 1 ;
53545321 }
5355- /* The master sent info about nodes that might be coherent. */
5356- sbuf2close (hndl -> sb );
5357- hndl -> sb = NULL ;
5358- CDB2DBINFORESPONSE * dbinfo_response = NULL ;
5359- dbinfo_response =
5360- cdb2__dbinforesponse__unpack (NULL , len , hndl -> first_buf );
5361- parse_dbresponse (dbinfo_response , hndl -> hosts , hndl -> ports ,
5362- & hndl -> master , & hndl -> num_hosts ,
5363- & hndl -> num_hosts_sameroom , hndl -> debug_trace ,
5364- & hndl -> s_sslmode );
5365- cdb2__dbinforesponse__free_unpacked (dbinfo_response , NULL );
5366- debugprint ("type=%d returning 1\n" , type );
5367-
5368- /* Clear cached SSL sessions - Hosts may have changed. */
5369- if (hndl -> sess != NULL ) {
5370- SSL_SESSION_free (hndl -> sess -> sessobj );
5371- hndl -> sess -> sessobj = NULL ;
5372- }
5373- return 1 ;
5374- }
5375- if (hndl -> first_buf != NULL ) {
5376- hndl -> firstresponse =
5377- cdb2__sqlresponse__unpack (NULL , len , hndl -> first_buf );
5378- } else {
5379- fprintf (stderr , "td 0x%p %s:%d: Can't read response from DB\n" ,
5380- (void * )pthread_self (), __func__ , __LINE__ );
5381- sbuf2close (hndl -> sb );
5382- hndl -> sb = NULL ;
5383- return 1 ;
5384- }
5385- while ((rc = cdb2_next_record_int (hndl , 0 )) == CDB2_OK )
5386- ;
5387-
5388- if (hndl -> sb == NULL ) {
5389- debugprint ("sb is NULL, next_record returns %d, returning 1\n" , rc );
5390- return 1 ;
5391- }
5392-
5393- struct cdb2_query * item ;
5394- TAILQ_FOREACH (item , & hndl -> queries , entry )
5395- {
5396- /* This is the case when we got disconnected while reading the query.
5397- In that case retry all the queries and skip their results,
5398- except the last one. */
5399- if (!run_last && item == TAILQ_LAST (& hndl -> queries , query_list )) {
5400- break ;
5401- }
5402-
5403- struct newsqlheader hdr = {.type = ntohl (CDB2_REQUEST_TYPE__CDB2QUERY ),
5404- .compression = ntohl (0 ),
5405- .length = ntohl (item -> len )};
5406- debugprint ("resending '%s' to %s\n" , item -> sql , host );
5407-
5408- sbuf2write ((char * )& hdr , sizeof (hdr ), hndl -> sb );
5409- sbuf2write ((char * )item -> buf , item -> len , hndl -> sb );
5410- sbuf2flush (hndl -> sb );
5411-
5412- clear_responses (hndl );
5413-
5322+ hndl -> in_trans = 1 ;
54145323 int len = 0 ;
5415-
5416- if (!hndl -> read_intrans_results && !item -> is_read ) {
5417- continue ;
5418- }
5419- /* This is for select queries, we send just the last row. */
54205324 rc = cdb2_read_record (hndl , & hndl -> first_buf , & len , NULL );
54215325 if (rc ) {
5422- debugprint ("Can't read response from the db node %s\n" , host );
5423- free (hndl -> first_buf );
5424- hndl -> first_buf = NULL ;
54255326 sbuf2close (hndl -> sb );
54265327 hndl -> sb = NULL ;
5328+ free (hndl -> first_buf );
5329+ hndl -> first_buf = NULL ;
5330+ debugprint ("cdb2_read_record rc=%d, returning 1\n" , rc );
54275331 return 1 ;
54285332 }
54295333 if (hndl -> first_buf != NULL ) {
5430- hndl -> firstresponse =
5431- cdb2__sqlresponse__unpack (NULL , len , hndl -> first_buf );
5334+ hndl -> firstresponse = cdb2__sqlresponse__unpack (NULL , len , hndl -> first_buf );
54325335 } else {
5433- debugprint ("Can't read response from the db node %s\n" , host );
5336+ fprintf (stderr , "td 0x%p %s:%d: Can't read response from the db\n" , (void * )pthread_self (), __func__ ,
5337+ __LINE__ );
54345338 sbuf2close (hndl -> sb );
54355339 hndl -> sb = NULL ;
5436- return 1 ;
5340+ free (hndl -> first_buf );
5341+ hndl -> first_buf = NULL ;
5342+ return -1 ;
54375343 }
5438- int read_rc ;
5439-
5440- while ((read_rc = cdb2_next_record_int (hndl , 0 )) == CDB2_OK )
5344+ while ((rc = cdb2_next_record_int (hndl , 0 )) == CDB2_OK )
54415345 ;
54425346
5443- if (hndl -> sb == NULL ) {
5444- debugprint ("sb is NULL, next_record_int returns "
5445- "%d, returning 1\n" ,
5446- read_rc );
5447- return 1 ;
5347+ struct cdb2_query * item ;
5348+ TAILQ_FOREACH (item , & hndl -> queries , entry )
5349+ {
5350+ /* This is the case when we got disconnected while reading the query.
5351+ In that case retry all the queries and skip their results,
5352+ except the last one. */
5353+ if (!run_last && item == TAILQ_LAST (& hndl -> queries , query_list )) {
5354+ break ;
5355+ }
5356+
5357+ struct newsqlheader hdr = {
5358+ .type = ntohl (CDB2_REQUEST_TYPE__CDB2QUERY ), .compression = ntohl (0 ), .length = ntohl (item -> len )};
5359+ debugprint ("resending '%s'\n" , item -> sql );
5360+
5361+ sbuf2write ((char * )& hdr , sizeof (hdr ), hndl -> sb );
5362+ sbuf2write ((char * )item -> buf , item -> len , hndl -> sb );
5363+ sbuf2flush (hndl -> sb );
5364+
5365+ clear_responses (hndl );
5366+
5367+ int len = 0 ;
5368+
5369+ if (!hndl -> read_intrans_results && !item -> is_read ) {
5370+ continue ;
5371+ }
5372+ /* This is for select queries, we send just the last row. */
5373+ rc = cdb2_read_record (hndl , & hndl -> first_buf , & len , NULL );
5374+ if (rc ) {
5375+ snprintf (hndl -> errstr , sizeof (hndl -> errstr ), "%s:%d Can't read response from the db\n" , __func__ ,
5376+ __LINE__ );
5377+ free (hndl -> first_buf );
5378+ hndl -> first_buf = NULL ;
5379+ sbuf2close (hndl -> sb );
5380+ hndl -> sb = NULL ;
5381+ return -1 ;
5382+ }
5383+ if (hndl -> first_buf != NULL ) {
5384+ hndl -> firstresponse = cdb2__sqlresponse__unpack (NULL , len , hndl -> first_buf );
5385+ } else {
5386+ snprintf (hndl -> errstr , sizeof (hndl -> errstr ), "%s:%d Can't read response from the db\n" , __func__ ,
5387+ __LINE__ );
5388+ sbuf2close (hndl -> sb );
5389+ hndl -> sb = NULL ;
5390+ return -1 ;
5391+ }
5392+ int read_rc ;
5393+
5394+ while ((read_rc = cdb2_next_record_int (hndl , 0 )) == CDB2_OK )
5395+ ;
5396+
5397+ if (hndl -> sb == NULL ) {
5398+ debugprint ("sb is NULL, next_record_int returns "
5399+ "%d, returning 1\n" ,
5400+ read_rc );
5401+ return 1 ;
5402+ }
54485403 }
5404+ clear_responses (hndl );
5405+ } else if (hndl -> in_trans ) {
5406+ snprintf (hndl -> errstr , sizeof (hndl -> errstr ), "%s: Database disconnected while in transaction\n" , __func__ );
5407+ return CDB2ERR_TRAN_IO_ERROR ; /* Fail if disconnect happens in transaction which doesn't have snapshot info.*/
54495408 }
5450- clear_responses (hndl );
54515409 return 0 ;
54525410}
54535411
0 commit comments