status.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "hdfspp/status.h"
  19. #include <cassert>
  20. #include <sstream>
  21. #include <cstring>
  22. #include <map>
  23. #include <set>
  24. namespace hdfs {
  25. // Server side exceptions that we capture from the RpcResponseHeaderProto
  26. const char * kStatusAccessControlException = "org.apache.hadoop.security.AccessControlException";
  27. const char * kPathIsNotDirectoryException = "org.apache.hadoop.fs.PathIsNotDirectoryException";
  28. const char * kSnapshotException = "org.apache.hadoop.hdfs.protocol.SnapshotException";
  29. const char * kStatusStandbyException = "org.apache.hadoop.ipc.StandbyException";
  30. const char * kStatusSaslException = "javax.security.sasl.SaslException";
  31. const char * kPathNotFoundException = "org.apache.hadoop.fs.InvalidPathException";
  32. const char * kPathNotFoundException2 = "java.io.FileNotFoundException";
  33. const char * kFileAlreadyExistsException = "org.apache.hadoop.fs.FileAlreadyExistsException";
  34. const char * kPathIsNotEmptyDirectoryException = "org.apache.hadoop.fs.PathIsNotEmptyDirectoryException";
  35. const static std::map<std::string, int> kKnownServerExceptionClasses = {
  36. {kStatusAccessControlException, Status::kAccessControlException},
  37. {kPathIsNotDirectoryException, Status::kNotADirectory},
  38. {kSnapshotException, Status::kSnapshotProtocolException},
  39. {kStatusStandbyException, Status::kStandbyException},
  40. {kStatusSaslException, Status::kAuthenticationFailed},
  41. {kPathNotFoundException, Status::kPathNotFound},
  42. {kPathNotFoundException2, Status::kPathNotFound},
  43. {kFileAlreadyExistsException, Status::kFileAlreadyExists},
  44. {kPathIsNotEmptyDirectoryException, Status::kPathIsNotEmptyDirectory}
  45. };
  46. // Errors that retry cannot fix. TODO: complete the list.
  47. const static std::set<int> noRetryExceptions = {
  48. Status::kPermissionDenied,
  49. Status::kAuthenticationFailed,
  50. Status::kAccessControlException
  51. };
  52. Status::Status(int code, const char *msg1)
  53. : code_(code) {
  54. if(msg1) {
  55. msg_ = msg1;
  56. }
  57. }
  58. Status::Status(int code, const char *exception_class_name, const char *exception_details)
  59. : code_(code) {
  60. // If we can assure this never gets nullptr args this can be
  61. // in the initializer list.
  62. if(exception_class_name)
  63. exception_class_ = exception_class_name;
  64. if(exception_details)
  65. msg_ = exception_details;
  66. std::map<std::string, int>::const_iterator it = kKnownServerExceptionClasses.find(exception_class_);
  67. if(it != kKnownServerExceptionClasses.end()) {
  68. code_ = it->second;
  69. }
  70. }
  71. Status Status::OK() {
  72. return Status();
  73. }
  74. Status Status::InvalidArgument(const char *msg) {
  75. return Status(kInvalidArgument, msg);
  76. }
  77. Status Status::PathNotFound(const char *msg){
  78. return Status(kPathNotFound, msg);
  79. }
  80. Status Status::ResourceUnavailable(const char *msg) {
  81. return Status(kResourceUnavailable, msg);
  82. }
  83. Status Status::PathIsNotDirectory(const char *msg) {
  84. return Status(kNotADirectory, msg);
  85. }
  86. Status Status::Unimplemented() {
  87. return Status(kUnimplemented, "");
  88. }
  89. Status Status::Exception(const char *exception_class_name, const char *error_message) {
  90. // Server side exception but can be represented by std::errc codes
  91. if (exception_class_name && (strcmp(exception_class_name, kStatusAccessControlException) == 0) )
  92. return Status(kPermissionDenied, error_message);
  93. else if (exception_class_name && (strcmp(exception_class_name, kStatusSaslException) == 0))
  94. return AuthenticationFailed();
  95. else if (exception_class_name && (strcmp(exception_class_name, kPathNotFoundException) == 0))
  96. return Status(kPathNotFound, error_message);
  97. else if (exception_class_name && (strcmp(exception_class_name, kPathNotFoundException2) == 0))
  98. return Status(kPathNotFound, error_message);
  99. else if (exception_class_name && (strcmp(exception_class_name, kPathIsNotDirectoryException) == 0))
  100. return Status(kNotADirectory, error_message);
  101. else if (exception_class_name && (strcmp(exception_class_name, kSnapshotException) == 0))
  102. return Status(kInvalidArgument, error_message);
  103. else if (exception_class_name && (strcmp(exception_class_name, kFileAlreadyExistsException) == 0))
  104. return Status(kFileAlreadyExists, error_message);
  105. else if (exception_class_name && (strcmp(exception_class_name, kPathIsNotEmptyDirectoryException) == 0))
  106. return Status(kPathIsNotEmptyDirectory, error_message);
  107. else
  108. return Status(kException, exception_class_name, error_message);
  109. }
  110. Status Status::Error(const char *error_message) {
  111. return Exception("Exception", error_message);
  112. }
  113. Status Status::AuthenticationFailed() {
  114. return Status::AuthenticationFailed(nullptr);
  115. }
  116. Status Status::AuthenticationFailed(const char *msg) {
  117. std::string formatted = "AuthenticationFailed";
  118. if(msg) {
  119. formatted += ": ";
  120. formatted += msg;
  121. }
  122. return Status(kAuthenticationFailed, formatted.c_str());
  123. }
  124. Status Status::AuthorizationFailed() {
  125. return Status::AuthorizationFailed(nullptr);
  126. }
  127. Status Status::AuthorizationFailed(const char *msg) {
  128. std::string formatted = "AuthorizationFailed";
  129. if(msg) {
  130. formatted += ": ";
  131. formatted += msg;
  132. }
  133. return Status(kPermissionDenied, formatted.c_str());
  134. }
  135. Status Status::Canceled() {
  136. return Status(kOperationCanceled, "Operation canceled");
  137. }
  138. Status Status::InvalidOffset(const char *msg){
  139. return Status(kInvalidOffset, msg);
  140. }
  141. std::string Status::ToString() const {
  142. if (code_ == kOk) {
  143. return "OK";
  144. }
  145. std::stringstream ss;
  146. if(!exception_class_.empty()) {
  147. ss << exception_class_ << ":";
  148. }
  149. ss << msg_;
  150. return ss.str();
  151. }
  152. bool Status::notWorthRetry() const {
  153. return noRetryExceptions.find(code_) != noRetryExceptions.end();
  154. }
  155. Status Status::MutexError(const char *msg) {
  156. std::string formatted = "MutexError";
  157. if(msg) {
  158. formatted += ": ";
  159. formatted += msg;
  160. }
  161. return Status(kBusy/*try_lock failure errno*/, msg);
  162. }
  163. }