bdr: Add bdr_parse_slot_name and clean up some related code.
authorAndres Freund <andres@anarazel.de>
Mon, 9 Feb 2015 02:30:05 +0000 (03:30 +0100)
committerAndres Freund <andres@anarazel.de>
Thu, 12 Feb 2015 09:16:59 +0000 (10:16 +0100)
Makefile.in
bdr.c
bdr_catalogs.c
bdr_common.c
bdr_init_copy.c
bdr_internal.h

index e75f645bec9c5a850057a170eed13cb0e8f9fdbb..98300b4882eb58a35e0f98fb0aa2ccf0482b6de6 100644 (file)
@@ -102,7 +102,7 @@ bdr_version.h: bdr_version.h.in
 
 bdr.o: bdr_version.h
 
-bdr_init_copy: bdr_init_copy.o
+bdr_init_copy: bdr_init_copy.o bdr_common.o
    $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(libpq_pgport) $(LIBS) -o $@$(X)
 
 scripts/bdr_initial_load: scripts/bdr_initial_load.in
diff --git a/bdr.c b/bdr.c
index e1d71e89d66f33540a76421716ab86c9c9dd3523..3ff133f2a227ff1434417b88c66f44a07e7d7ad2 100644 (file)
--- a/bdr.c
+++ b/bdr.c
@@ -203,23 +203,17 @@ bdr_build_ident_and_slotname(uint64 remote_sysid, TimeLineID remote_tlid,
        Name out_slot_name)
 {
    StringInfoData  replication_identifier;
-   char            local_sysid[33];
 
    initStringInfo(&replication_identifier);
 
    Assert(MyDatabaseId != InvalidOid);
    Assert(remote_dboid != InvalidOid);
 
-   snprintf(local_sysid, sizeof(local_sysid), UINT64_FORMAT,
-            GetSystemIdentifier());
-
    /*
     * Build slot name identifying the local node to the remote end.
     */
-   snprintf(NameStr(*out_slot_name), NAMEDATALEN, BDR_SLOT_NAME_FORMAT,
-            remote_dboid, local_sysid, ThisTimeLineID,
-            MyDatabaseId, EMPTY_REPLICATION_NAME);
-   NameStr(*out_slot_name)[NAMEDATALEN - 1] = '\0';
+   bdr_slot_name(out_slot_name, GetSystemIdentifier(), ThisTimeLineID, MyDatabaseId,
+                 remote_dboid);
 
    /*
     * Build replication identifier.
index fb4b85078ae4d31dc2bb0db0d603e31aea158549..7ac4475b3d11a3c7c2200e16970214227cfcba95 100644 (file)
@@ -317,6 +317,17 @@ bdr_fetch_sysid_via_node_id(RepNodeId node_id, uint64 *sysid, TimeLineID *tli,
    }
 }
 
+void
+bdr_parse_slot_name(const char *sname, uint64 *remote_sysid, TimeLineID *remote_tli,
+                   Oid *remote_dboid, Oid *local_dboid)
+{
+   NameData    replication_name;
+
+   if (sscanf(sname, BDR_SLOT_NAME_FORMAT,
+              local_dboid, remote_sysid, remote_tli, remote_dboid,
+              NameStr(replication_name)) != 4)
+       elog(ERROR, "could not parse slot name: %s", sname);
+}
 
 RepNodeId
 bdr_fetch_node_id_via_sysid(uint64 sysid, TimeLineID tli, Oid dboid)
index c4ad1cf622a9db3093b077a5e76d7f7fdb83b214..b94b9c0e84585210a147edf8c9960f6fa0fccbf1 100644 (file)
@@ -27,13 +27,8 @@ void
 bdr_slot_name(Name slot_name, uint64 sysid, TimeLineID tlid,
              Oid dboid, Oid local_dboid)
 {
-   char        sysid_str[33];
-
-   snprintf(sysid_str, sizeof(sysid_str), UINT64_FORMAT, sysid);
-   sysid_str[sizeof(sysid_str)-1] = '\0';
-
    snprintf(NameStr(*slot_name), NAMEDATALEN, BDR_SLOT_NAME_FORMAT,
-            local_dboid, sysid_str, tlid, dboid,
+            local_dboid, sysid, tlid, dboid,
             EMPTY_REPLICATION_NAME);
    NameStr(*slot_name)[NAMEDATALEN-1] = '\0';
 }
index 472ab9e1475845413c76056e2175a522b1dba421..9041b4aa953f70227a80d384edb3dc23a22ed8a4 100644 (file)
@@ -750,16 +750,14 @@ initialize_data_dir(char *data_dir, char *connstr,
 static void
 initialize_replication_slot(PGconn *conn, NodeInfo *ni, Oid dboid)
 {
-   char        slotname[NAMEDATALEN];
-   char        system_identifier_s[32];
+   NameData    slotname;
    PQExpBuffer query = createPQExpBuffer();
    PGresult   *res;
 
-   snprintf(system_identifier_s, sizeof(system_identifier_s), UINT64_FORMAT, ni->local_sysid);
-   snprintf(slotname, NAMEDATALEN, BDR_SLOT_NAME_FORMAT,
-            dboid, system_identifier_s, ni->local_tlid, dboid, "");
+   /* dboids are the same, because we just cloned... */
+   bdr_slot_name(&slotname, ni->local_sysid, ni->local_tlid, dboid, dboid);
    appendPQExpBuffer(query, "SELECT pg_create_logical_replication_slot(%s, '%s');",
-                     PQescapeLiteral(conn, slotname, NAMEDATALEN), "bdr");
+                     PQescapeLiteral(conn, NameStr(slotname), NAMEDATALEN), "bdr");
 
    res = PQexec(conn, query->data);
 
index 8c529511990f290cfb07c7456e60f976d0537b8d..af61ec01932a5cbb77391ea4331d13984b0040b9 100644 (file)
@@ -15,7 +15,7 @@
 #include "lib/ilist.h"
 
 #define EMPTY_REPLICATION_NAME ""
-#define BDR_SLOT_NAME_FORMAT "bdr_%u_%s_%u_%u__%s"
+#define BDR_SLOT_NAME_FORMAT "bdr_%u_"UINT64_FORMAT"_%u_%u__%s"
 #define BDR_NODE_ID_FORMAT "bdr_"UINT64_FORMAT"_%u_%u_%u_%s"
 
 /* A configured BDR connection from bdr_connections */
@@ -69,5 +69,8 @@ extern void bdr_free_connection_config(BdrConnectionConfig *cfg);
 
 extern void bdr_slot_name(Name slot_name, uint64 sysid, TimeLineID tlid,
                          Oid dboid, Oid local_dboid);
+extern void bdr_parse_slot_name(const char *name, uint64 *remote_sysid,
+                               Oid *remote_dboid, TimeLineID *remote_tli,
+                               Oid *local_dboid);
 
 #endif   /* BDR_INTERNAL_H */