@@ -223,6 +223,7 @@ static void reset_pqconn(PGconn **conn);
223
223
static void reset_pqconn_and_res (PGconn * * conn , PGresult * res );
224
224
static void configure_retry (CopyPartState * cpts , int millis );
225
225
static struct timespec timespec_now_plus_millis (int millis );
226
+ struct timespec timespec_now (void );
226
227
227
228
/*
228
229
* Steps are:
@@ -377,10 +378,8 @@ move_primary(Cmd *cmd)
377
378
void
378
379
init_cp_state (CopyPartState * cps , const char * part_name , int32 dst_node )
379
380
{
380
- int e ;
381
-
382
381
cps -> part_name = part_name ;
383
- if ((cps -> src_node = get_partition_owner (part_name )) == -1 )
382
+ if ((cps -> src_node = get_primary_owner (part_name )) == -1 )
384
383
{
385
384
shmn_elog (WARNING , "Partition %s doesn't exist, not moving it" ,
386
385
part_name );
@@ -402,10 +401,7 @@ init_cp_state(CopyPartState *cps, const char *part_name, int32 dst_node)
402
401
}
403
402
404
403
/* Task is ready to be processed right now */
405
- if ((e = clock_gettime (CLOCK_MONOTONIC , & cps -> waketm )) == -1 )
406
- {
407
- shmn_elog (FATAL , "clock_gettime failed, %s" , strerror (e ));
408
- }
404
+ cps -> waketm = timespec_now ();
409
405
cps -> fd_to_epoll = -1 ;
410
406
cps -> fd_in_epoll_set = -1 ;
411
407
@@ -496,7 +492,6 @@ exec_tasks(CopyPartState **tasks, int ntasks)
496
492
slist_head timeout_states = SLIST_STATIC_INIT (timeout_states );
497
493
slist_mutable_iter iter ;
498
494
/* at least one task will require our attention at waketm */
499
- struct timespec waketm ;
500
495
struct timespec curtm ;
501
496
int timeout ;
502
497
int unfinished_moves = 0 ; /* number of not yet failed or succeeded tasks */
@@ -505,12 +500,10 @@ exec_tasks(CopyPartState **tasks, int ntasks)
505
500
int epfd ;
506
501
struct epoll_event evlist [MAX_EVENTS ];
507
502
508
- /* In the beginning, all tasks are ready for execution, so wake tm is right
509
- * is actually current time. We also need to put all tasks to the
510
- * timeout_states list to invoke them.
503
+ /*
504
+ * In the beginning, all tasks are ready for execution, so we need to put
505
+ * all tasks to the timeout_states list to invoke them.
511
506
*/
512
- if ((e = clock_gettime (CLOCK_MONOTONIC , & waketm )) == -1 )
513
- shmn_elog (FATAL , "clock_gettime failed, %s" , strerror (e ));
514
507
for (i = 0 ; i < ntasks ; i ++ )
515
508
{
516
509
/* TODO: make sure one part is touched only by one task */
@@ -545,8 +538,7 @@ exec_tasks(CopyPartState **tasks, int ntasks)
545
538
CopyPartStateNode * cps_node =
546
539
slist_container (CopyPartStateNode , list_node , iter .cur );
547
540
CopyPartState * cps = cps_node -> cps ;
548
- if ((e = clock_gettime (CLOCK_MONOTONIC , & curtm )) == -1 )
549
- shmn_elog (FATAL , "clock_gettime failed, %s" , strerror (e ));
541
+ curtm = timespec_now ();
550
542
551
543
if (timespeccmp (cps -> waketm , curtm ) <= 0 )
552
544
{
602
594
calc_timeout (slist_head * timeout_states )
603
595
{
604
596
slist_iter iter ;
605
- int e ;
606
597
struct timespec curtm ;
607
598
int timeout ;
608
599
/* could use timespec field for this, but that's more readable */
@@ -633,8 +624,7 @@ calc_timeout(slist_head *timeout_states)
633
624
if (!waketm_set )
634
625
return -1 ;
635
626
636
- if ((e = clock_gettime (CLOCK_MONOTONIC , & curtm )) == -1 )
637
- shmn_elog (FATAL , "clock_gettime failed, %s" , strerror (e ));
627
+ curtm = timespec_now ();
638
628
if (timespeccmp (waketm , curtm ) <= 0 )
639
629
{
640
630
shmn_elog (DEBUG1 , "Non-negative timeout, waking immediately" );
@@ -951,16 +941,26 @@ void configure_retry(CopyPartState *cps, int millis)
951
941
}
952
942
953
943
/*
954
- * Get current time + given milliseconds . Fails with PG elog(FATAL) if gettime
955
- * failed. Not very generic, yes, but exactly what we need.
944
+ * Get current CLOCK_MONOTONIC time . Fails with PG elog(FATAL) if gettime
945
+ * failed.
956
946
*/
957
- struct timespec timespec_now_plus_millis ( int millis )
947
+ struct timespec timespec_now ( void )
958
948
{
959
- struct timespec t ;
960
949
int e ;
950
+ struct timespec t ;
961
951
962
952
if ((e = clock_gettime (CLOCK_MONOTONIC , & t )) == -1 )
963
953
shmn_elog (FATAL , "clock_gettime failed, %s" , strerror (e ));
964
954
955
+ return t ;
956
+ }
957
+
958
+ /*
959
+ * Get current time + given milliseconds. Fails with PG elog(FATAL) if gettime
960
+ * failed.
961
+ */
962
+ struct timespec timespec_now_plus_millis (int millis )
963
+ {
964
+ struct timespec t = timespec_now ();
965
965
return timespec_add_millis (t , millis );
966
966
}
0 commit comments