ctdb: hold transaction locks during freeze, mark during recover.
[sahlberg/ctdb.git] / common / ctdb_util.c
index 21810924d0ba5b59d8872d6cedc22cbefe236238..1ff4c1f9aa2052cb97b46c54af6c579ab9ed430f 100644 (file)
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/wait.h"
+#include "system/shmem.h"
 #include "../include/ctdb_private.h"
 
 int LogLevel = DEBUG_NOTICE;
@@ -73,7 +74,8 @@ int ctdb_parse_address(struct ctdb_context *ctdb,
        endservent();
        
        address->address = talloc_strdup(mem_ctx, str);
-       if (address->address == NULL) return -1;
+       CTDB_NO_MEMORY(ctdb, address->address);
+
        if (se == NULL) {
                address->port = CTDB_PORT;
        } else {
@@ -97,14 +99,7 @@ bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
 */
 uint32_t ctdb_hash(const TDB_DATA *key)
 {
-       uint32_t value; /* Used to compute the hash value.  */
-       uint32_t i;     /* Used to cycle through random values. */
-
-       /* Set the initial value from the key size. */
-       for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
-               value = (value + (key->dptr[i] << (i*5 % 24)));
-
-       return (1103515243 * value + 12345);  
+       return tdb_jenkins_hash(discard_const(key));
 }
 
 /*
@@ -121,46 +116,14 @@ static void *_idr_find_type(struct idr_context *idp, int id, const char *type, c
        return p;
 }
 
-
-/*
-  update a max latency number
- */
-void ctdb_latency(struct ctdb_db_context *ctdb_db, const char *name, double *latency, struct timeval t)
-{
-       double l = timeval_elapsed(&t);
-       if (l > *latency) {
-               *latency = l;
-       }
-
-       if (ctdb_db->ctdb->tunable.log_latency_ms !=0) {
-               if (l*1000 > ctdb_db->ctdb->tunable.log_latency_ms) {
-                       DEBUG(DEBUG_WARNING, ("High latency %fs for operation %s on database %s\n", l*1000000, name, ctdb_db->db_name));
-               }
-       }
-}
-
-/*
-  update a reclock latency number
- */
-void ctdb_reclock_latency(struct ctdb_context *ctdb, const char *name, double *latency, double l)
-{
-       if (l > *latency) {
-               *latency = l;
-       }
-
-       if (ctdb->tunable.reclock_latency_ms !=0) {
-               if (l*1000 > ctdb->tunable.reclock_latency_ms) {
-                       DEBUG(DEBUG_ERR, ("High RECLOCK latency %fs for operation %s\n", l, name));
-               }
-       }
-}
-
 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
 {
-       uint32_t id;
-
-       id  = ctdb->idr_cnt++ & 0xFFFF;
-       id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
+       int id = idr_get_new_above(ctdb->idr, state, ctdb->lastid+1, INT_MAX);
+       if (id < 0) {
+               DEBUG(DEBUG_DEBUG, ("Reqid wrap!\n"));
+               id = idr_get_new(ctdb->idr, state, INT_MAX);
+       }
+       ctdb->lastid = id;
        return id;
 }
 
@@ -168,7 +131,7 @@ void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *ty
 {
        void *p;
 
-       p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
+       p = _idr_find_type(ctdb->idr, reqid, type, location);
        if (p == NULL) {
                DEBUG(DEBUG_WARNING, ("Could not find idr:%u\n",reqid));
        }
@@ -181,7 +144,7 @@ void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
 {
        int ret;
 
-       ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
+       ret = idr_remove(ctdb->idr, reqid);
        if (ret != 0) {
                DEBUG(DEBUG_ERR, ("Removing idr that does not exist\n"));
        }
@@ -370,6 +333,23 @@ void ctdb_restore_scheduler(struct ctdb_context *ctdb)
 #endif
 }
 
+/*
+ * This function forks a child process and drops the realtime 
+ * scheduler for the child process.
+ */
+pid_t ctdb_fork(struct ctdb_context *ctdb)
+{
+       pid_t pid;
+
+       pid = fork();
+       if (pid == 0) {
+               if (ctdb->do_setsched) {
+                       ctdb_restore_scheduler(ctdb);
+               }
+       }
+       return pid;
+}
+
 void set_nonblocking(int fd)
 {
        unsigned v;
@@ -398,7 +378,7 @@ bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
        return true;
 }
 
-static bool parse_ipv6(const char *s, const char *iface, unsigned port, ctdb_sock_addr *saddr)
+static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
 {
        saddr->ip6.sin6_family   = AF_INET6;
        saddr->ip6.sin6_port     = htons(port);
@@ -410,8 +390,14 @@ static bool parse_ipv6(const char *s, const char *iface, unsigned port, ctdb_soc
                return false;
        }
 
-       if (iface && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
-               saddr->ip6.sin6_scope_id = if_nametoindex(iface);
+       if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
+               if (strchr(ifaces, ',')) {
+                       DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
+                                         "is specified for multiple ifaces %s\n",
+                                         s, ifaces));
+                       return false;
+               }
+               saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
        }
 
        return true;
@@ -461,7 +447,7 @@ bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
 /*
   parse an ip
  */
-bool parse_ip(const char *addr, const char *iface, unsigned port, ctdb_sock_addr *saddr)
+bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
 {
        char *p;
        bool ret;
@@ -471,7 +457,7 @@ bool parse_ip(const char *addr, const char *iface, unsigned port, ctdb_sock_addr
        if (p == NULL) {
                ret = parse_ipv4(addr, port, &saddr->ip);
        } else {
-               ret = parse_ipv6(addr, iface, port, saddr);
+               ret = parse_ipv6(addr, ifaces, port, saddr);
        }
 
        return ret;
@@ -480,7 +466,7 @@ bool parse_ip(const char *addr, const char *iface, unsigned port, ctdb_sock_addr
 /*
   parse a ip/mask pair
  */
-bool parse_ip_mask(const char *str, const char *iface, ctdb_sock_addr *addr, unsigned *mask)
+bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
 {
        TALLOC_CTX *tmp_ctx = talloc_new(NULL);
        char *s, *p;
@@ -513,7 +499,7 @@ bool parse_ip_mask(const char *str, const char *iface, ctdb_sock_addr *addr, uns
 
 
        /* now is this a ipv4 or ipv6 address ?*/
-       ret = parse_ip(s, iface, 0, addr);
+       ret = parse_ip(s, ifaces, 0, addr);
 
        talloc_free(tmp_ctx);
        return ret;
@@ -623,3 +609,79 @@ void ctdb_unblock_signal(int signum)
        sigaddset(&set,signum);
        sigprocmask(SIG_UNBLOCK,&set,NULL);
 }
+
+struct debug_levels debug_levels[] = {
+       {DEBUG_EMERG,   "EMERG"},
+       {DEBUG_ALERT,   "ALERT"},
+       {DEBUG_CRIT,    "CRIT"},
+       {DEBUG_ERR,     "ERR"},
+       {DEBUG_WARNING, "WARNING"},
+       {DEBUG_NOTICE,  "NOTICE"},
+       {DEBUG_INFO,    "INFO"},
+       {DEBUG_DEBUG,   "DEBUG"},
+       {0, NULL}
+};
+
+const char *get_debug_by_level(int32_t level)
+{
+       int i;
+
+       for (i=0; debug_levels[i].description != NULL; i++) {
+               if (debug_levels[i].level == level) {
+                       return debug_levels[i].description;
+               }
+       }
+       return "Unknown";
+}
+
+int32_t get_debug_by_desc(const char *desc)
+{
+       int i;
+
+       for (i=0; debug_levels[i].description != NULL; i++) {
+               if (!strcmp(debug_levels[i].description, desc)) {
+                       return debug_levels[i].level;
+               }
+       }
+
+       return DEBUG_ERR;
+}
+
+/* we don't lock future pages here; it would increase the chance that
+ * we'd fail to mmap later on. */
+void ctdb_lockdown_memory(struct ctdb_context *ctdb)
+{
+#ifdef HAVE_MLOCKALL
+       /* Extra stack, please! */
+       char dummy[10000];
+       memset(dummy, 0, sizeof(dummy));
+
+       if (ctdb->valgrinding) {
+               return;
+       }
+
+       /* Avoid compiler optimizing out dummy. */
+       mlock(dummy, sizeof(dummy));
+       if (mlockall(MCL_CURRENT) != 0) {
+               DEBUG(DEBUG_WARNING,("Failed to lock memory: %s'\n",
+                                    strerror(errno)));
+       }
+#endif
+}
+
+const char *ctdb_eventscript_call_names[] = {
+       "init",
+       "setup",
+       "startup",
+       "startrecovery",
+       "recovered",
+       "takeip",
+       "releaseip",
+       "stopped",
+       "monitor",
+       "status",
+       "shutdown",
+       "reload",
+       "updateip",
+       "ipreallocated"
+};