bbb7c4d70c083dc0285248a946892a5182da5e9f
braney
  Thu Jul 8 13:04:25 2021 -0700
use mysql advisory locks to avoid duplication of hubUrl in hubStatus

diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index fa6e9a4..67e2bff 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -427,49 +427,47 @@
     dbCount++;
     dyStringPrintf(dy,"%s,", hel->name);
     }
 *pCount = dbCount;
 
 return dy->string;
 }
 
 static void insertHubUrlInStatus(char *url)
 /* add a url to the hubStatus table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[4096];
 char *statusTable = getHubStatusTableName();
 
-struct errCatch *errCatch = errCatchNew();
-if (errCatchStart(errCatch))
+sqlGetLockWithTimeout(conn, "central_hubStatus", 15);
+
+// Try to grab a row right before we lock
+sqlSafef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", statusTable, url);
+struct sqlResult *sr = sqlGetResult(conn, query);
+
+if (sqlNextRow(sr) == NULL)  // if we got something from this query, someone added it right before we locked it
     {
     if (sqlFieldIndex(conn, statusTable, "firstAdded") >= 0)
         sqlSafef(query, sizeof(query), "insert into %s (hubUrl,shortLabel,longLabel,dbCount,dbList,status,lastOkTime,lastNotOkTime,errorMessage,firstAdded) values (\"%s\",\"\",\"\",0,NULL,0,\"\",\"\",\"\",now())", statusTable, url);
     else
         sqlSafef(query, sizeof(query), "insert into %s (hubUrl) values (\"%s\")",
 	statusTable, url);
     sqlUpdate(conn, query);
     }
-errCatchEnd(errCatch);
-if (errCatch->gotError)
-    {
-    // if we got a duplicate error, it means this hubUrl is already in the 
-    // hubStatus table.
-    const char *error = sqlLastError(conn);
-    if (!startsWith("Duplicate entry", error))
-        errAbort("%s", error);
-    }
+sqlFreeResult(&sr);
+sqlReleaseLock(conn, "central_hubStatus");
 hDisconnectCentral(&conn);
 }
 
 static unsigned getHubId(char *url, char **errorMessage)
 /* find id for url in hubStatus table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[4096];
 char **row;
 boolean foundOne = FALSE;
 int id = 0;
 
 char *statusTableName = getHubStatusTableName();
 sqlSafef(query, sizeof(query), "select id,errorMessage from %s where hubUrl = \"%s\"", statusTableName, url);