Commit 4782985e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

fix fnordmetric server build

parent 0d029034
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ all: build test

build:
	mkdir -p build
	(cd build && cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../cmake && ninja)
	(cd build && cmake -DCMAKE_BUILD_TYPE=Release ../cmake && make)

build-dbg:
	mkdir -p build-dbg
+32 −20
Original line number Diff line number Diff line
@@ -13,7 +13,32 @@ project(fnordmetric)

option(ENABLE_TESTS "Build unit tests [default: off]" OFF)

include_directories(${PROJECT_BINARY_DIR})
include_directories(../3rdparty/libfnord)
include_directories(../src)

add_subdirectory(../3rdparty/libfnord/fnord-base "${CMAKE_CURRENT_BINARY_DIR}/libfnord-base")
add_subdirectory(../3rdparty/libfnord/fnord-chart "${CMAKE_CURRENT_BINARY_DIR}/libfnord-chart")
add_subdirectory(../3rdparty/libfnord/fnord-http "${CMAKE_CURRENT_BINARY_DIR}/libfnord-http")
add_subdirectory(../3rdparty/libfnord/fnord-json "${CMAKE_CURRENT_BINARY_DIR}/libfnord-json")
add_subdirectory(../3rdparty/libfnord/fnord-metricdb "${CMAKE_CURRENT_BINARY_DIR}/libfnord-metricdb")
add_subdirectory(../3rdparty/libfnord/fnord-sstable "${CMAKE_CURRENT_BINARY_DIR}/libfnord-sstable")

find_package(Threads)

if(APPLE)
  set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++ ${CMAKE_CXX_FLAGS}")
else()
  set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
endif()

set(FNORDMETRIC_SOURCES
    $<TARGET_OBJECTS:fnord-base>
    $<TARGET_OBJECTS:fnord-chart>
    $<TARGET_OBJECTS:fnord-http>
    $<TARGET_OBJECTS:fnord-json>
    $<TARGET_OBJECTS:fnord-metricdb>
    $<TARGET_OBJECTS:fnord-sstable>
    ../src/environment.cc)
    #stage/src/fnordmetric/util/assets.cc
    #stage/src/fnordmetric/sql/backends/csv/csvbackend.cc
@@ -55,33 +80,20 @@ set(FNORDMETRIC_SOURCES
    #stage/src/fnordmetric/query/query.cc
    #stage/src/fnordmetric/query/queryservice.cc

include_directories(${PROJECT_BINARY_DIR})
include_directories(../3rdparty/libfnord)
include_directories(../src)


if(APPLE)
  set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++ ${CMAKE_CXX_FLAGS}")
else()
  set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
endif()

add_executable(fnordmetric-cli ${FNORDMETRIC_SOURCES} ../src/cli.cc)
target_link_libraries(fnordmetric-cli m)
install(TARGETS fnordmetric-cli DESTINATION bin)
#add_executable(fnordmetric-cli ${FNORDMETRIC_SOURCES} ../src/cli.cc)
#target_link_libraries(fnordmetric-cli m)
#target_link_libraries(fnordmetric-cli ${CMAKE_THREAD_LIBS_INIT})
#install(TARGETS fnordmetric-cli DESTINATION bin)

add_executable(fnordmetric-server ${FNORDMETRIC_SOURCES} ../src/server.cc)
add_executable(fnordmetric-server ${FNORDMETRIC_SOURCES} ../src/fnordmetric-server.cc)
target_link_libraries(fnordmetric-server m)
install(TARGETS fnordmetric-server DESTINATION bin)

find_package(Threads)
target_link_libraries(fnordmetric-cli ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(fnordmetric-server ${CMAKE_THREAD_LIBS_INIT})
install(TARGETS fnordmetric-server DESTINATION bin)

find_package(MySQL)
if(MYSQL_FOUND)
  set(FNORD_ENABLE_MYSQL true)
  target_link_libraries(fnordmetric-cli ${MYSQL_CLIENT_LIBS})
  #target_link_libraries(fnordmetric-cli ${MYSQL_CLIENT_LIBS})
  target_link_libraries(fnordmetric-server ${MYSQL_CLIENT_LIBS})
else()
  message("WARNING: libmysqlclient not found, FnordMetric will be compiled without MySQL support")
+30 −91
Original line number Diff line number Diff line
@@ -9,48 +9,38 @@
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <fnord-base/application.h>
#include <fnord-base/cli/flagparser.h>
#include <fnord-base/exception.h>
#include <fnord-base/exceptionhandler.h>
#include <fnord-base/inspect.h>
#include <fnord-base/random.h>
#include <fnord-http/httpserver.h>
#include <fnord-base/io/fileutil.h>
#include <fnord-base/io/inputstream.h>
#include <fnord-base/io/outputstream.h>
#include <fnord-base/logging.h>
#include <fnord-base/net/udpserver.h>
#include <fnord-base/stats/statsd.h>
#include <fnord-base/stdtypes.h>
#include <fnord-base/thread/threadpool.h>
#include <fnord-http/httpserver.h>
#include <fnord-metricdb/metricservice.h>
#include <fnord-base/cli/flagparser.h>
#include <environment.h>
#include <httpapi.h>

using fnord::metric_service::MetricService;
using namespace fnordmetric;

static const char kCrashErrorMsg[] =
    "FnordMetric crashed :( -- Please report a bug at "
    "github.com/paulasmuth/fnordmetric";

using fnord::json::JSONRPC;
using fnord::json::JSONRPCHTTPAdapter;
using fnord::metric_service::MetricService;
using fnord::metric_service::MetricServiceAdapter;
using fnord::thread::Task;
using fnord::thread::TaskScheduler;
using fnord::statsd::StatsdServer;

static MetricService makeMetricService(
    const std::string& backend_type,
    TaskScheduler* backend_scheduler) {

  /* open inmemory backend */
  if (backend_type == "inmemory") {
    env()->logger()->printf(
        "INFO",
        "Opening new inmemory backend -- SHOULD ONlY BE USED FOR TESTING");
    fnord::logInfo("fnordmetric-server", "Opening new inmemory backend");
    return MetricService::newWithInMemoryBackend();
  }

@@ -63,75 +53,35 @@ static MetricService makeMetricService(
    }

    auto datadir = env()->flags()->getString("datadir");
    env()->logger()->printf(
        "INFO",
        "Opening disk backend at %s",
        datadir.c_str());

    return MetricService::newWithDiskBackend(datadir, backend_scheduler);
    if (!fnord::FileUtil::exists(datadir)) {
      RAISEF(kIOError, "File $0 does not exist", datadir);
    }

  RAISE(
      kUsageError,
      "unknown backend type: %s",
      backend_type.c_str());
    if (!fnord::FileUtil::isDirectory(datadir)) {
      RAISEF(kIOError, "File $0 is not a directory", datadir);
    }

static int startServer() {
  fnord::thread::ThreadPool server_pool(
      std::unique_ptr<fnord::ExceptionHandler>(
          new fnord::CatchAndAbortExceptionHandler(kCrashErrorMsg)));

  fnord::thread::ThreadPool worker_pool(
      std::unique_ptr<fnord::ExceptionHandler>(
          new fnord::CatchAndPrintExceptionHandler(nullptr)));

  JSONRPC json_rpc;

  /* setup MetricService */
  if (env()->flags()->isSet("datadir")) {
    auto datadir = env()->flags()->getString("datadir");

    if (!fnord::io::FileUtil::exists(datadir)) {
      env()->logger()->printf(
          "FATAL",
          "File %s does not exist",
          datadir.c_str());

      return 1;
    fnord::logInfo("fnordmetric-server", "Opening disk backend at $0", datadir);
    return MetricService::newWithDiskBackend(datadir, backend_scheduler);
  }

    if (!fnord::io::FileUtil::isDirectory(datadir)) {
      env()->logger()->printf(
          "FATAL",
          "File %s is not a directory",
          datadir.c_str());

      return 1;
    }
  RAISEF(kUsageError, "unknown backend type: $0", backend_type);
}

static int startServer() {
  fnord::thread::ThreadPool server_pool;
  fnord::thread::ThreadPool worker_pool;

  /* setup MetricService */
  auto metric_service = makeMetricService(
      env()->flags()->getString("storage_backend"),
      &server_pool);

  MetricServiceAdapter::registerJSONRPC(&metric_service, &json_rpc);

  /* Setup GroupsService */
  GroupsService groups_service;
  GroupsServiceAdapter::registerJSONRPC(&groups_service, &json_rpc);

  /* Setup KeyValueService */
  KeyValueService keyvalue_service;
  KeyValueServiceAdapter::registerJSONRPC(&keyvalue_service, &json_rpc);

  /* Setup statsd server */
  if (env()->flags()->isSet("statsd_port")) {
  /*if (env()->flags()->isSet("statsd_port")) {
    auto port = env()->flags()->getInt("statsd_port");
    env()->logger()->printf(
        "INFO",
        "Starting statsd server on port %i",
        port);
    fnord::logInfo("Starting statsd server on port $0", port);

    auto statsd_server = new StatsdServer(&server_pool, &worker_pool);
    statsd_server->onSample([&metric_service] (
@@ -139,9 +89,8 @@ static int startServer() {
        double value,
        const std::vector<std::pair<std::string, std::string>>& labels) {
      if (env()->verbose()) {
        env()->logger()->printf(
            "DEBUG",
            "statsd sample: %s=%f %s",
        fnord::logDebug(
            "statsd sample: $0=$1 $2",
            key.c_str(),
            value,
            fnord::inspect(labels).c_str());
@@ -151,11 +100,11 @@ static int startServer() {
    });

    statsd_server->listen(port);
  }
  }*/


  /* Setup http server */
  if (env()->flags()->isSet("http_port")) {
  /*if (env()->flags()->isSet("http_port")) {
    auto port = env()->flags()->getInt("http_port");
    env()->logger()->printf(
        "INFO",
@@ -172,13 +121,13 @@ static int startServer() {
    http_server->addHandler(JSONRPCHTTPAdapter::make(&json_rpc));
    http_server->addHandler(std::unique_ptr<http::HTTPHandler>(http_api));
    http_server->listen(port);
  }
  }*/

  return 0;
}

static void printUsage() {
  auto err_stream = fnord::io::OutputStream::getStderr();
  auto err_stream = fnord::OutputStream::getStderr();
  err_stream->printf("usage: fnordmetric-server [options]\n");
  err_stream->printf("\noptions:\n");
  env()->flags()->printUsage(err_stream.get());
@@ -187,11 +136,8 @@ static void printUsage() {
}

int main(int argc, const char** argv) {
  fnord::CatchAndAbortExceptionHandler ehandler(kCrashErrorMsg);
  ehandler.installGlobalHandlers();
  fnord::system::SignalHandler::ignoreSIGHUP();
  fnord::system::SignalHandler::ignoreSIGPIPE();
  fnord::Random::init();
  fnord::Application::init();
  fnord::Application::logToStderr();

  env()->flags()->defineFlag(
      "http_port",
@@ -263,17 +209,10 @@ int main(int argc, const char** argv) {
  try {
    return startServer();
  } catch (const fnord::Exception& e) {
    auto err_stream = fnord::io::OutputStream::getStderr();
    auto msg = e.getMessage();
    err_stream->printf("[ERROR] ");
    err_stream->write(msg.c_str(), msg.size());
    err_stream->printf("\n");
    fnord::logError("fnordmetric-server", e, "FATAL ERROR");

    if (e.getTypeName() == kUsageError) {
      err_stream->printf("\n");
      printUsage();
    } else {
      env()->logger()->exception("FATAL", "Fatal error", e);
    }

    return 1;