Merge in version util.
[m6w6/libmemcached] / libmemcached / connect.c
index 24328f4c4bb7c244b2bc358faeef953317af7207..a43de9563a89d2017b4658a3404abc8ad534bfd8 100644 (file)
@@ -2,15 +2,16 @@
 #include <netdb.h>
 #include <poll.h>
 #include <sys/time.h>
+#include <time.h>
 
 static memcached_return_t set_hostinfo(memcached_server_st *server)
 {
   struct addrinfo *ai;
   struct addrinfo hints;
-  int e;
   char str_port[NI_MAXSERV];
+  uint32_t counter= 5;
 
-  sprintf(str_port, "%u", server->port);
+  snprintf(str_port, NI_MAXSERV, "%u", (uint32_t)server->port);
 
   memset(&hints, 0, sizeof(hints));
 
@@ -26,12 +27,31 @@ static memcached_return_t set_hostinfo(memcached_server_st *server)
     hints.ai_protocol= IPPROTO_TCP;
   }
 
-  e= getaddrinfo(server->hostname, str_port, &hints, &ai);
-  if (e != 0)
+  while (--counter)
   {
-    WATCHPOINT_STRING(server->hostname);
-    WATCHPOINT_STRING(gai_strerror(e));
-    return MEMCACHED_HOST_LOOKUP_FAILURE;
+    int e= getaddrinfo(server->hostname, str_port, &hints, &ai);
+
+    if (e == 0)
+    {
+      break;
+    }
+    else if (e == EAI_AGAIN)
+    {
+      struct timespec dream, rem;
+
+      dream.tv_nsec= 1000;
+      dream.tv_sec= 0;
+
+      nanosleep(&dream, &rem);
+
+      continue;
+    }
+    else
+    {
+      WATCHPOINT_STRING(server->hostname);
+      WATCHPOINT_STRING(gai_strerror(e));
+      return MEMCACHED_HOST_LOOKUP_FAILURE;
+    }
   }
 
   if (server->address_info)
@@ -63,6 +83,8 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
     error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDTIMEO,
                       &waittime, (socklen_t)sizeof(struct timeval));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
   }
 #endif
 
@@ -78,6 +100,23 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
     error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVTIMEO,
                       &waittime, (socklen_t)sizeof(struct timeval));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
+  }
+#endif
+
+
+#if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
+  {
+    int set = 1;
+    int error= setsockopt(ptr->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
+
+    // This is not considered a fatal error
+    if (error == -1)
+    {
+      WATCHPOINT_ERRNO(errno);
+      perror("setsockopt(SO_NOSIGPIPE)");
+    }
   }
 #endif
 
@@ -91,6 +130,8 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
     error= setsockopt(ptr->fd, SOL_SOCKET, SO_LINGER,
                       &linger, (socklen_t)sizeof(struct linger));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
   }
 
   if (ptr->root->flags.tcp_nodelay)
@@ -101,24 +142,55 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
     error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_NODELAY,
                       &flag, (socklen_t)sizeof(int));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
+  }
+
+  if (ptr->root->flags.tcp_keepalive)
+  {
+    int flag= 1;
+    int error;
+
+    error= setsockopt(ptr->fd, SOL_SOCKET, SO_KEEPALIVE,
+                      &flag, (socklen_t)sizeof(int));
+    WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
   }
 
-  if (ptr->root->send_size)
+#ifdef TCP_KEEPIDLE
+  if (ptr->root->tcp_keepidle > 0)
+  {
+    int error;
+
+    error= setsockopt(ptr->fd, IPPROTO_TCP, TCP_KEEPIDLE,
+                      &ptr->root->tcp_keepidle, (socklen_t)sizeof(int));
+    WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
+  }
+#endif
+
+  if (ptr->root->send_size > 0)
   {
     int error;
 
     error= setsockopt(ptr->fd, SOL_SOCKET, SO_SNDBUF,
                       &ptr->root->send_size, (socklen_t)sizeof(int));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
   }
 
-  if (ptr->root->recv_size)
+  if (ptr->root->recv_size > 0)
   {
     int error;
 
     error= setsockopt(ptr->fd, SOL_SOCKET, SO_RCVBUF,
                       &ptr->root->recv_size, (socklen_t)sizeof(int));
     WATCHPOINT_ASSERT(error == 0);
+    if (error)
+      return MEMCACHED_FAILURE;
   }
 
   /* libmemcached will always use nonblocking IO to avoid write deadlocks */
@@ -129,7 +201,9 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
   while (flags == -1 && (errno == EINTR || errno == EAGAIN));
 
   unlikely (flags == -1)
+  {
     return MEMCACHED_CONNECTION_FAILURE;
+  }
   else if ((flags & O_NONBLOCK) == 0)
   {
     int rval;
@@ -139,7 +213,9 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
     while (rval == -1 && (errno == EINTR || errno == EAGAIN));
 
     unlikely (rval == -1)
+    {
       return MEMCACHED_CONNECTION_FAILURE;
+    }
   }
 
   return MEMCACHED_SUCCESS;
@@ -148,7 +224,6 @@ static memcached_return_t set_socket_options(memcached_server_st *ptr)
 static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
 {
   struct sockaddr_un servAddr;
-  socklen_t addrlen;
 
   if (ptr->fd == -1)
   {
@@ -162,8 +237,6 @@ static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
     servAddr.sun_family= AF_UNIX;
     strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */
 
-    addrlen= (socklen_t) (strlen(servAddr.sun_path) + sizeof(servAddr.sun_family));
-
 test_connect:
     if (connect(ptr->fd,
                 (struct sockaddr *)&servAddr,
@@ -186,6 +259,7 @@ test_connect:
   }
 
   WATCHPOINT_ASSERT(ptr->fd != -1);
+
   return MEMCACHED_SUCCESS;
 }
 
@@ -195,7 +269,9 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
   {
     struct addrinfo *use;
 
-    if (!ptr->sockaddr_inited ||
+    WATCHPOINT_ASSERT(ptr->cursor_active == 0);
+
+    if (! ptr->options.sockaddr_inited ||
         (!(ptr->root->flags.use_cache_lookups)))
     {
       memcached_return_t rc;
@@ -203,7 +279,7 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
       rc= set_hostinfo(ptr);
       if (rc != MEMCACHED_SUCCESS)
         return rc;
-      ptr->sockaddr_inited= true;
+      ptr->options.sockaddr_inited= true;
     }
 
     use= ptr->address_info;
@@ -229,8 +305,7 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
       (void)set_socket_options(ptr);
 
       /* connect to server */
-      while (ptr->fd != -1 &&
-             connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
+      if ((connect(ptr->fd, use->ai_addr, use->ai_addrlen) == -1))
       {
         ptr->cached_errno= errno;
         if (errno == EINPROGRESS || /* nonblocking mode - first return, */
@@ -239,20 +314,50 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
           struct pollfd fds[1];
           fds[0].fd = ptr->fd;
           fds[0].events = POLLOUT;
-          int error= poll(fds, 1, ptr->root->connect_timeout);
 
-          if (error != 1 || fds[0].revents & POLLERR)
+          int timeout= ptr->root->connect_timeout;
+          if (ptr->root->flags.no_block == true)
+            timeout= -1;
+
+          size_t loop_max= 5;
+          while (--loop_max)
           {
-            if (fds[0].revents & POLLERR)
+            int error= poll(fds, 1, timeout);
+
+            switch (error)
             {
-              int err;
-              socklen_t len = sizeof (err);
-              (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
-              ptr->cached_errno= (err == 0) ? errno : err;
+            case 1:
+              loop_max= 1;
+              break;
+            case 0:
+              if (loop_max==1) 
+                return MEMCACHED_TIMEOUT;
+              continue;
+              // A real error occurred and we need to completely bail
+            default:
+              WATCHPOINT_ERRNO(errno);
+              switch (errno)
+              {
+#ifdef TARGET_OS_LINUX
+              case ERESTART:
+#endif
+              case EINTR:
+                continue;
+              default:
+                if (fds[0].revents & POLLERR)
+                {
+                  int err;
+                  socklen_t len= sizeof (err);
+                  (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len);
+                  ptr->cached_errno= (err == 0) ? errno : err;
+                }
+
+                (void)close(ptr->fd);
+                ptr->fd= -1;
+
+                break;
+              }
             }
-
-            (void)close(ptr->fd);
-            ptr->fd= -1;
           }
         }
         else if (errno == EISCONN) /* we are connected :-) */
@@ -267,10 +372,23 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
         }
       }
 
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+      if (ptr->fd != -1 && ptr->root->sasl && ptr->root->sasl->callbacks)
+      {
+        memcached_return rc= memcached_sasl_authenticate_connection(ptr);
+        if (rc != MEMCACHED_SUCCESS)
+        {
+          (void)close(ptr->fd);
+          ptr->fd= -1;
+
+          return rc;
+        }
+      }
+#endif
+
+
       if (ptr->fd != -1)
       {
-        WATCHPOINT_ASSERT(ptr->cursor_active == 0);
-        ptr->server_failure_counter= 0;
         return MEMCACHED_SUCCESS;
       }
       use = use->ai_next;
@@ -279,6 +397,8 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
 
   if (ptr->fd == -1)
   {
+    WATCHPOINT_STRING("Never got a good file descriptor");
+
     /* Failed to connect. schedule next retry */
     if (ptr->root->retry_timeout)
     {
@@ -287,46 +407,68 @@ static memcached_return_t network_connect(memcached_server_st *ptr)
       if (gettimeofday(&next_time, NULL) == 0)
         ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
     }
-    ptr->server_failure_counter++;
+
     if (ptr->cached_errno == 0)
       return MEMCACHED_TIMEOUT;
 
     return MEMCACHED_ERRNO; /* The last error should be from connect() */
   }
 
-  ptr->server_failure_counter= 0;
   return MEMCACHED_SUCCESS; /* The last error should be from connect() */
 }
 
+void set_last_disconnected_host(memcached_server_write_instance_st ptr)
+{
+  // const_cast
+  memcached_st *root= (memcached_st *)ptr->root;
 
-memcached_return_t memcached_connect(memcached_server_st *ptr)
+#if 0
+  WATCHPOINT_STRING(ptr->hostname);
+  WATCHPOINT_NUMBER(ptr->port);
+  WATCHPOINT_ERRNO(ptr->cached_errno);
+#endif
+  if (root->last_disconnected_server)
+    memcached_server_free(root->last_disconnected_server);
+  root->last_disconnected_server= memcached_server_clone(NULL, ptr);
+}
+
+memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
 {
   memcached_return_t rc= MEMCACHED_NO_SERVERS;
   LIBMEMCACHED_MEMCACHED_CONNECT_START();
 
   /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */
   WATCHPOINT_ASSERT(ptr->root);
-  if (ptr->root->retry_timeout && ptr->root->server_failure_limit)
+  if (ptr->root->retry_timeout && ptr->next_retry)
   {
     struct timeval curr_time;
 
     gettimeofday(&curr_time, NULL);
 
-    /* if we've had too many consecutive errors on this server, mark it dead. */
-    if (ptr->server_failure_counter >= ptr->root->server_failure_limit)
+    // We should optimize this to remove the allocation if the server was
+    // the last server to die
+    if (ptr->next_retry > curr_time.tv_sec)
     {
-      ptr->next_retry= curr_time.tv_sec + ptr->root->retry_timeout;
-      ptr->server_failure_counter= 0;
+      set_last_disconnected_host(ptr);
+
+      return MEMCACHED_SERVER_MARKED_DEAD;
     }
+  }
 
-    if (curr_time.tv_sec < ptr->next_retry)
-    {
-      if (memcached_behavior_get(ptr->root, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS))
-        run_distribution(ptr->root);
+  // If we are over the counter failure, we just fail. Reject host only
+  // works if you have a set number of failures.
+  if (ptr->root->server_failure_limit && ptr->server_failure_counter >= ptr->root->server_failure_limit)
+  {
+    set_last_disconnected_host(ptr);
 
-      ptr->root->last_disconnected_server = ptr;
-      return MEMCACHED_SERVER_MARKED_DEAD;
+    // @todo fix this by fixing behavior to no longer make use of
+    // memcached_st
+    if (_is_auto_eject_host(ptr->root))
+    {
+      run_distribution((memcached_st *)ptr->root);
     }
+
+    return MEMCACHED_SERVER_MARKED_DEAD;
   }
 
   /* We need to clean up the multi startup piece */
@@ -343,11 +485,22 @@ memcached_return_t memcached_connect(memcached_server_st *ptr)
   case MEMCACHED_CONNECTION_UNIX_SOCKET:
     rc= unix_socket_connect(ptr);
     break;
+  case MEMCACHED_CONNECTION_MAX:
   default:
     WATCHPOINT_ASSERT(0);
   }
 
-  unlikely ( rc != MEMCACHED_SUCCESS) ptr->root->last_disconnected_server = ptr;
+  if (rc == MEMCACHED_SUCCESS)
+  {
+    ptr->server_failure_counter= 0;
+    ptr->next_retry= 0;
+  }
+  else
+  {
+    ptr->server_failure_counter++;
+
+    set_last_disconnected_host(ptr);
+  }
 
   LIBMEMCACHED_MEMCACHED_CONNECT_END();