This is an automated email from the ASF dual-hosted git repository.

abukor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 73934d2bbfa476261845e2817cc65b38f7cfc832
Author: Attila Bukor <[email protected]>
AuthorDate: Wed Apr 1 22:59:13 2020 +0200

    [postgres] Deflake MiniPostgres tests
    
    Tests depending on MiniPostgres were flaky as in some cases Postgres
    takes longer than expected to start. This commit introduces checking if
    psql can connect to MiniPostgres as part of start-up and retries every
    100ms for 5 seconds.
    
    Ran MiniRangerTest 1000 times on dist-test with no failures with this
    fix.
    
    Change-Id: I5106b3e2aeb9dabad9a7ee0b17059c1df2042bb4
    Reviewed-on: http://gerrit.cloudera.org:8080/15629
    Tested-by: Kudu Jenkins
    Reviewed-by: Adar Dembo <[email protected]>
---
 src/kudu/postgres/mini_postgres.cc | 27 ++++++++++++++++++++++++---
 src/kudu/postgres/mini_postgres.h  |  4 ++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/kudu/postgres/mini_postgres.cc 
b/src/kudu/postgres/mini_postgres.cc
index 5b0e60d..ab739d5 100644
--- a/src/kudu/postgres/mini_postgres.cc
+++ b/src/kudu/postgres/mini_postgres.cc
@@ -91,10 +91,11 @@ Status MiniPostgres::Start() {
   if (!wait.ok()) {
     // TODO(abukor): implement retry with a different port if it can't bind
     WARN_NOT_OK(process_->Kill(SIGINT), "failed to send SIGINT to Postgres");
-  } else {
-    LOG(INFO) << "Postgres bound to " << port_;
+    return wait;
   }
-  return wait;
+
+  LOG(INFO) << "Postgres bound to " << port_;
+  return WaitForReady();
 }
 
 Status MiniPostgres::Stop() {
@@ -142,5 +143,25 @@ Status MiniPostgres::CreateConfigs() {
   return file->Close();
 }
 
+Status MiniPostgres::WaitForReady() const {
+  Status s;
+  MonoTime deadline = MonoTime::Now() + MonoDelta::FromSeconds(5);
+  while (MonoTime::Now() < deadline) {
+    Subprocess psql({
+        JoinPathSegments(bin_dir_, "postgres/pg_isready"),
+        "-p", SimpleItoa(port_),
+        "-h", host_,
+    });
+    RETURN_NOT_OK(psql.Start());
+    s = psql.WaitAndCheckExitCode();
+    if (s.ok()) {
+      return s;
+    }
+    SleepFor(MonoDelta::FromMilliseconds(100));
+  }
+
+  return Status::TimedOut(s.ToString());
+}
+
 } // namespace postgres
 } // namespace kudu
diff --git a/src/kudu/postgres/mini_postgres.h 
b/src/kudu/postgres/mini_postgres.h
index c60a824..67ac085 100644
--- a/src/kudu/postgres/mini_postgres.h
+++ b/src/kudu/postgres/mini_postgres.h
@@ -23,6 +23,7 @@
 
 #include <glog/logging.h>
 
+#include "kudu/gutil/port.h"
 #include "kudu/util/env.h"
 #include "kudu/util/path_util.h"
 #include "kudu/util/status.h"
@@ -84,6 +85,9 @@ class MiniPostgres {
     return DirName(exe);
   }
 
+  // Tests connection to Postgres, blocking until success or it times out.
+  Status WaitForReady() const WARN_UNUSED_RESULT;
+
   // 'pg_root' is the subdirectory in which the Postgres data files will live.
   Status CreateConfigs();
 

Reply via email to