events.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef HDFSPP_EVENTS
  19. #define HDFSPP_EVENTS
  20. #include "hdfspp/status.h"
  21. #include <functional>
  22. namespace hdfs {
  23. /*
  24. * Supported event names. These names will stay consistent in libhdfs callbacks.
  25. *
  26. * Other events not listed here may be seen, but they are not stable and
  27. * should not be counted on. May need to be broken up into more components
  28. * as more events are added.
  29. */
  30. static constexpr const char * FS_NN_CONNECT_EVENT = "NN::connect";
  31. static constexpr const char * FS_NN_READ_EVENT = "NN::read";
  32. static constexpr const char * FS_NN_WRITE_EVENT = "NN::write";
  33. static constexpr const char * FILE_DN_CONNECT_EVENT = "DN::connect";
  34. static constexpr const char * FILE_DN_READ_EVENT = "DN::read";
  35. static constexpr const char * FILE_DN_WRITE_EVENT = "DN::write";
  36. // NN failover event due to issues with the current NN; might be standby, might be dead.
  37. // Invokes the fs_event_callback using the nameservice name in the cluster string.
  38. // The uint64_t value argument holds an address that can be reinterpreted as a const char *
  39. // and provides the full URI of the node the failover will attempt to connect to next.
  40. static constexpr const char * FS_NN_FAILOVER_EVENT = "NN::failover";
  41. // Invoked when RpcConnection tries to use an empty set of endpoints to figure out
  42. // which NN in a HA cluster to connect to.
  43. static constexpr const char * FS_NN_EMPTY_ENDPOINTS_EVENT = "NN::bad_failover::no_endpoints";
  44. // Invoked prior to determining if failed NN rpc calls should be retried or discarded.
  45. static constexpr const char * FS_NN_PRE_RPC_RETRY_EVENT = "NN::rpc::get_retry_action";
  46. class event_response {
  47. public:
  48. // Helper factories
  49. // The default ok response; libhdfspp should continue normally
  50. static event_response make_ok() {
  51. return event_response(kOk);
  52. }
  53. static event_response make_caught_std_exception(const char *what) {
  54. return event_response(kCaughtStdException, what);
  55. }
  56. static event_response make_caught_unknown_exception() {
  57. return event_response(kCaughtUnknownException);
  58. }
  59. // High level classification of responses
  60. enum event_response_type {
  61. kOk = 0,
  62. // User supplied callback threw.
  63. // Std exceptions will copy the what() string
  64. kCaughtStdException = 1,
  65. kCaughtUnknownException = 2,
  66. // Responses to be used in testing only
  67. kTest_Error = 100
  68. };
  69. event_response_type response_type() { return response_type_; }
  70. private:
  71. // Use factories to construct for now
  72. event_response();
  73. event_response(event_response_type type)
  74. : response_type_(type)
  75. {
  76. if(type == kCaughtUnknownException) {
  77. status_ = Status::Exception("c++ unknown exception", "");
  78. }
  79. }
  80. event_response(event_response_type type, const char *what)
  81. : response_type_(type),
  82. exception_msg_(what==nullptr ? "" : what)
  83. {
  84. status_ = Status::Exception("c++ std::exception", exception_msg_.c_str());
  85. }
  86. event_response_type response_type_;
  87. // use to hold what str if event handler threw
  88. std::string exception_msg_;
  89. ///////////////////////////////////////////////
  90. //
  91. // Testing support
  92. //
  93. // The consumer can stimulate errors
  94. // within libhdfdspp by returning a Status from the callback.
  95. ///////////////////////////////////////////////
  96. public:
  97. static event_response test_err(const Status &status) {
  98. return event_response(status);
  99. }
  100. Status status() { return status_; }
  101. private:
  102. event_response(const Status & status) :
  103. response_type_(event_response_type::kTest_Error), status_(status) {}
  104. Status status_; // To be used with kTest_Error
  105. };
  106. /* callback signature */
  107. typedef std::function<event_response (const char * event,
  108. const char * cluster,
  109. int64_t value)> fs_event_callback;
  110. typedef std::function<event_response (const char * event,
  111. const char * cluster,
  112. const char * file,
  113. int64_t value)>file_event_callback;
  114. }
  115. #endif