RpcClient.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 _HDFS_LIBHDFS3_RPC_RPCCLIENT_H_
  19. #define _HDFS_LIBHDFS3_RPC_RPCCLIENT_H_
  20. #include "RpcAuth.h"
  21. #include "RpcCall.h"
  22. #include "RpcChannel.h"
  23. #include "RpcChannelKey.h"
  24. #include "RpcConfig.h"
  25. #include "RpcProtocolInfo.h"
  26. #include "RpcServerInfo.h"
  27. #include "SharedPtr.h"
  28. #include "Thread.h"
  29. #include "UnorderedMap.h"
  30. #include <stdint.h>
  31. #include <string>
  32. #include <vector>
  33. #ifdef MOCK
  34. #include "TestRpcChannelStub.h"
  35. #endif
  36. namespace hdfs {
  37. namespace internal {
  38. class RpcClient {
  39. public:
  40. /**
  41. * Destroy an RpcClient instance.
  42. */
  43. virtual ~RpcClient() {
  44. }
  45. /**
  46. * Get a RPC channel, create a new one if necessary.
  47. * @param auth Authentication information used to setup RPC connection.
  48. * @param protocol The RPC protocol used in this call.
  49. * @param server Remote server information.
  50. * @param conf RPC connection configuration.
  51. * @param once If true, the RPC channel will not be reused.
  52. */
  53. virtual RpcChannel &getChannel(const RpcAuth &auth,
  54. const RpcProtocolInfo &protocol,
  55. const RpcServerInfo &server, const RpcConfig &conf) = 0;
  56. /**
  57. * Check the RpcClient is still running.
  58. * @return true if the RpcClient is still running.
  59. */
  60. virtual bool isRunning() = 0;
  61. virtual std::string getClientId() const = 0;
  62. virtual int32_t getCallId() = 0;
  63. public:
  64. static RpcClient &getClient();
  65. static void createSinglten();
  66. private:
  67. static once_flag once;
  68. static shared_ptr<RpcClient> client;
  69. };
  70. class RpcClientImpl: public RpcClient {
  71. public:
  72. /**
  73. * Construct a RpcClient.
  74. */
  75. RpcClientImpl();
  76. /**
  77. * Destroy an RpcClient instance.
  78. */
  79. ~RpcClientImpl();
  80. /**
  81. * Get a RPC channel, create a new one if necessary.
  82. * @param auth Authentication information used to setup RPC connection.
  83. * @param protocol The RPC protocol used in this call.
  84. * @param server Remote server information.
  85. * @param conf RPC connection configuration.
  86. * @param once If true, the RPC channel will not be reused.
  87. */
  88. RpcChannel &getChannel(const RpcAuth &auth,
  89. const RpcProtocolInfo &protocol, const RpcServerInfo &server,
  90. const RpcConfig &conf);
  91. /**
  92. * Close the RPC channel.
  93. */
  94. void close();
  95. /**
  96. * Check the RpcClient is still running.
  97. * @return true if the RpcClient is still running.
  98. */
  99. bool isRunning();
  100. std::string getClientId() const {
  101. return clientId;
  102. }
  103. int32_t getCallId() {
  104. static mutex mutid;
  105. lock_guard<mutex> lock(mutid);
  106. ++count;
  107. count = count < std::numeric_limits<int32_t>::max() ? count : 0;
  108. return count;
  109. }
  110. private:
  111. shared_ptr<RpcChannel> createChannelInternal(
  112. const RpcChannelKey &key);
  113. void clean();
  114. private:
  115. atomic<bool> cleaning;
  116. atomic<bool> running;
  117. condition_variable cond;
  118. int64_t count;
  119. mutex mut;
  120. std::string clientId;
  121. thread cleaner;
  122. unordered_map<RpcChannelKey, shared_ptr<RpcChannel> > allChannels;
  123. #ifdef MOCK
  124. private:
  125. hdfs::mock::TestRpcChannelStub *stub;
  126. #endif
  127. };
  128. }
  129. }
  130. #endif /* _HDFS_LIBHDFS3_RPC_RPCCLIENT_H_ */