You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by al...@apache.org on 2016/11/03 12:40:21 UTC

[3/5] mesos git commit: Added "mesos-tcp-connect" binary.

Added "mesos-tcp-connect" binary.

To remove dependency on `bash` for TCP health checks, introduce
a separate light-weight binary (without libmesos dependency) for
probing TCP connections.

Review: https://reviews.apache.org/r/51605


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/abecad3c
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/abecad3c
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/abecad3c

Branch: refs/heads/master
Commit: abecad3c5a3728d738b13c7726d1f9afd4118cc2
Parents: c5555e3
Author: Alexander Rukletsov <al...@apache.org>
Authored: Fri Sep 2 17:04:37 2016 +0200
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Thu Nov 3 11:34:29 2016 +0100

----------------------------------------------------------------------
 src/CMakeLists.txt               |   1 +
 src/Makefile.am                  |   5 ++
 src/health-check/CMakeLists.txt  |  49 ++++++++++++
 src/health-check/tcp_connect.cpp | 146 ++++++++++++++++++++++++++++++++++
 4 files changed, 201 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/abecad3c/src/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 639f867..75c3a2b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -522,6 +522,7 @@ target_link_libraries(${MESOS_PROTOBUF_TARGET} ${AGENT_LIBS})
 ##############################
 add_subdirectory(cli/)
 add_subdirectory(docker/)
+add_subdirectory(health-check/)
 add_subdirectory(launcher/)
 add_subdirectory(local/)
 add_subdirectory(log/)

http://git-wip-us.apache.org/repos/asf/mesos/blob/abecad3c/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index c2f9e44..c031e98 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1394,6 +1394,11 @@ mesos_docker_executor_SOURCES = docker/executor.cpp
 mesos_docker_executor_CPPFLAGS = $(MESOS_CPPFLAGS)
 mesos_docker_executor_LDADD = libmesos.la $(LDADD)
 
+pkglibexec_PROGRAMS += mesos-tcp-connect
+mesos_tcp_connect_SOURCES = health-check/tcp_connect.cpp
+mesos_tcp_connect_CPPFLAGS = $(MESOS_CPPFLAGS)
+mesos_tcp_connect_LDADD = libmesos.la $(LDADD)
+
 bin_PROGRAMS += mesos-log
 mesos_log_SOURCES = log/main.cpp
 mesos_log_CPPFLAGS = $(MESOS_CPPFLAGS)

http://git-wip-us.apache.org/repos/asf/mesos/blob/abecad3c/src/health-check/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/health-check/CMakeLists.txt b/src/health-check/CMakeLists.txt
new file mode 100644
index 0000000..ca57789
--- /dev/null
+++ b/src/health-check/CMakeLists.txt
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# TODO(alexr): Enable this on Windows once it is supported.
+if (NOT WIN32)
+
+# THE MESOS TCP CONNECT SOURCE.
+###############################
+set(MESOS_TCP_CONNECT_SRC
+  ${MESOS_TCP_CONNECT_SRC}
+  tcp_connect.cpp
+  )
+
+# INCLUDE DIRECTIVES FOR MESOS TCP CONNECT EXECUTABLE (generates, e.g.,
+# -I/path/to/thing on Linux).
+#######################################################################
+include_directories(${AGENT_INCLUDE_DIRS})
+
+# LINKING LIBRARIES BY DIRECTORY (might generate, e.g., -L/path/to/thing on
+# Linux).
+###########################################################################
+link_directories(${AGENT_LIB_DIRS})
+
+# THE MESOS TCP CONNECT EXECUTABLE.
+###################################
+add_executable(${MESOS_TCP_CONNECT} ${MESOS_TCP_CONNECT_SRC})
+
+# ADD LINKER FLAGS (generates, e.g., -lglog on Linux).
+######################################################
+target_link_libraries(${MESOS_TCP_CONNECT} ${AGENT_LIBS} ${MESOS_TARGET})
+
+# ADD BINARY DEPENDENCIES (tells CMake what to compile/build first).
+####################################################################
+add_dependencies(${MESOS_TCP_CONNECT} ${MESOS_TARGET})
+
+endif (NOT WIN32)

http://git-wip-us.apache.org/repos/asf/mesos/blob/abecad3c/src/health-check/tcp_connect.cpp
----------------------------------------------------------------------
diff --git a/src/health-check/tcp_connect.cpp b/src/health-check/tcp_connect.cpp
new file mode 100644
index 0000000..ad1e932
--- /dev/null
+++ b/src/health-check/tcp_connect.cpp
@@ -0,0 +1,146 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __WINDOWS__
+#include <unistd.h>
+
+#include <arpa/inet.h> // for `inet_pton()`
+
+#include <netinet/in.h> // for `sockaddr_in`
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#endif // __WINDOWS__
+
+#include <cerrno>
+#include <cstring>
+#include <iostream>
+#include <string>
+
+#include <stout/flags.hpp>
+#include <stout/option.hpp>
+#include <stout/path.hpp>
+
+using std::cerr;
+using std::cout;
+using std::endl;
+using std::string;
+
+// This binary tries to establish a TCP connection to given <ip>:<port>
+// and can be used as a TCP health checker. It returns `EXIT_SUCCESS` iff
+// the TCP handshake was successful and `EXIT_FAILURE` otherwise.
+//
+// TODO(alexr): Support TCP half-open, see MESOS-6116.
+//
+// TODO(alexr): Add support for Windows, see MESOS-6117.
+//
+// TODO(alexr): Support IPv6, see MESOS-6120.
+//
+// NOTE: Consider using stout network abstractions instead of raw system
+// sockets. Once stout supports IPv6 migrating to it will buy us implicit
+// Windows and IPv6 compatibilities.
+
+
+class Flags : public virtual flags::FlagsBase
+{
+public:
+  Flags()
+  {
+    add(&Flags::ip,
+        "ip",
+        "IP of the target host. Only IPv4 is supported.");
+
+    add(&Flags::port,
+        "port",
+        "Port to connect to.");
+  }
+
+  Option<string> ip;
+  Option<int> port;
+};
+
+
+// Tries to establish a TCP connection to `ip`:`port`.
+// If the TCP handshake is successful, returns `EXIT_SUCCESS`.
+int testTCPConnect(const string& ip, int port)
+{
+  // Set up destination address.
+  struct sockaddr_in to;
+  memset(&to, 0, sizeof(to));
+  to.sin_family = AF_INET;
+  to.sin_port = htons(port);
+  if (inet_pton(AF_INET, ip.c_str(), &to.sin_addr) != 1) {
+    cerr << "Cannot convert '" << ip << "' into a network address" << endl;
+    return EXIT_FAILURE;
+  }
+
+  // Create a TCP socket.
+  int socket = ::socket(AF_INET, SOCK_STREAM, 0);
+  if (socket < 0) {
+    cerr << "Failed to create socket: " << strerror(errno) << endl;
+    return EXIT_FAILURE;
+  }
+
+  // Try to connect to socket. If the connection succeeds,
+  // zero is returned, indicating the remote port is open.
+  cout << "Connecting to " << ip << ":" << port << endl;
+  if (connect(socket, reinterpret_cast<sockaddr*>(&to), sizeof(to)) < 0) {
+    cerr << "Connection failed: " << strerror(errno) << endl;
+    close(socket);
+    return EXIT_FAILURE;
+  }
+
+  cout << "Successfully established TCP connection" << endl;
+
+  shutdown(socket, SHUT_RDWR);
+  close(socket);
+
+  return EXIT_SUCCESS;
+}
+
+
+int main(int argc, char *argv[])
+{
+  Flags flags;
+
+  Try<flags::Warnings> load = flags.load(None(), argc, argv);
+
+  if (load.isError()) {
+    cerr << flags.usage(load.error()) << endl;
+    return EXIT_FAILURE;
+  }
+
+  foreach (const flags::Warning& warning, load->warnings) {
+    cerr << warning.message << endl;
+  }
+
+  if (flags.help) {
+    cout << flags.usage() << endl;
+    return EXIT_SUCCESS;
+  }
+
+  if (flags.ip.isNone()) {
+    cerr << flags.usage("Missing required option --ip") << endl;
+    return EXIT_FAILURE;
+  }
+
+  if (flags.port.isNone()) {
+    cerr << flags.usage("Missing required option --port") << endl;
+    return EXIT_FAILURE;
+  }
+
+  return testTCPConnect(flags.ip.get(), flags.port.get());
+}