【问题标题】:Where does the Apache Thrift C++ GlobalOutput output to?Apache Thrift C++ GlobalOutput 输出到哪里?
【发布时间】:2015-08-18 07:59:20
【问题描述】:

我在我的 C++ 项目中使用 Apache Thrift TSimpleServer。一旦启动,服务器将监听客户端连接并处理请求。一切都很好,但偶尔服务器没有任何迹象就停止了。

我关注 thrift 库的来源,可以看到TTransportExceptionTException 被捕获时正在创建GlobalOutput(error_message)。我需要了解这一点,以便在服务器死机时创建恢复机制。

这里是我说的源代码:

void TSimpleServer::serve() {

  shared_ptr<TTransport> client;
  shared_ptr<TTransport> inputTransport;
  shared_ptr<TTransport> outputTransport;
  shared_ptr<TProtocol> inputProtocol;
  shared_ptr<TProtocol> outputProtocol;

  // Start the server listening
  serverTransport_->listen();

  // Run the preServe event
  if (eventHandler_) {
    eventHandler_->preServe();
  }

  // Fetch client from server
  while (!stop_) {
    try {
      client = serverTransport_->accept();
      inputTransport = inputTransportFactory_->getTransport(client);
      outputTransport = outputTransportFactory_->getTransport(client);
      inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
      outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
    } catch (TTransportException& ttx) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
          string errStr = string("TServerTransport died on accept: ") + ttx.what();
          GlobalOutput(errStr.c_str());
      }
      continue;
    } catch (TException& tx) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      string errStr = string("Some kind of accept exception: ") + tx.what();
      GlobalOutput(errStr.c_str());
      continue;
    } catch (string s) {
      if (inputTransport) { inputTransport->close(); }
      if (outputTransport) { outputTransport->close(); }
      if (client) { client->close(); }
      string errStr = string("Some kind of accept exception: ") + s;
      GlobalOutput(errStr.c_str());
      break;
    }

    // Get the processor
    shared_ptr<TProcessor> processor = getProcessor(inputProtocol,
                                                    outputProtocol, client);

    void* connectionContext = NULL;
    if (eventHandler_) {
      connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol);
    }
    try {
      for (;;) {
        if (eventHandler_) {
          eventHandler_->processContext(connectionContext, client);
        }
        if (!processor->process(inputProtocol, outputProtocol,
                                connectionContext) ||
          // Peek ahead, is the remote side closed?
            !inputProtocol->getTransport()->peek()) {
          break;
        }
      }
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer client died: ") + ttx.what();
      GlobalOutput(errStr.c_str());
    } catch (const std::exception& x) {
      GlobalOutput.printf("TSimpleServer exception: %s: %s",
                          typeid(x).name(), x.what());
    } catch (...) {
      GlobalOutput("TSimpleServer uncaught exception.");
    }
    if (eventHandler_) {
      eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol);
    }

    try {
      inputTransport->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer input close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    try {
      outputTransport->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer output close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    try {
      client->close();
    } catch (const TTransportException& ttx) {
      string errStr = string("TSimpleServer client close failed: ")
        + ttx.what();
      GlobalOutput(errStr.c_str());
    }
  }

  if (stop_) {
    try {
      serverTransport_->close();
    } catch (TTransportException &ttx) {
      string errStr = string("TServerTransport failed on close: ") + ttx.what();
      GlobalOutput(errStr.c_str());
    }
    stop_ = false;
  }
}

【问题讨论】:

    标签: c++ thrift


    【解决方案1】:

    在 TOutput.cpp 的深处,有一行 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); (source here),默认情况下,所有 Thrift GlobalOutput 消息都在此结束(标准错误)。

    但是您可以通过以函数指针的形式向 GlobalOutput 提供自己的处理程序来更改它(如果由于任何原因您不能使用 stderr):

    void myOutputFunction(const char* x)
    {
      fprintf(myLogFile, "Thrift internal message: %s\n", x);
    }
    
    // Inside some init function or main
    GlobalOutput.setOutputFunction(myOutputFunction);
    

    【讨论】:

    • 感谢您的回答和源链接。我从here 下载的 thrift 0.9.2 不包含 TOutput.cpp(和 TOutput.h)。但是您建议的源链接有它。这些源是否兼容,我可以将 TOutput.cpp 和 .h 复制到我的项目中并使用它们吗?
    • TOutput.xxx 是最近提取的,在 0.9.2 中应该有类似的代码在 Thrift.h 和 Thrift.cpp 中
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多