roxiecontrol.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "roxiecontrol.hpp"
  14. #include "jmisc.hpp"
  15. const unsigned roxieQueryRoxieTimeOut = 60000;
  16. #define EMPTY_RESULT_FAILURE 1200
  17. static void checkRoxieControlExceptions(IPropertyTree *response)
  18. {
  19. Owned<IMultiException> me = MakeMultiException();
  20. Owned<IPropertyTreeIterator> endpoints = response->getElements("Endpoint");
  21. ForEach(*endpoints)
  22. {
  23. IPropertyTree &endp = endpoints->query();
  24. Owned<IPropertyTreeIterator> exceptions = endp.getElements("Exception");
  25. ForEach (*exceptions)
  26. {
  27. IPropertyTree &ex = exceptions->query();
  28. me->append(*MakeStringException(ex.getPropInt("Code"), "Endpoint %s: %s", endp.queryProp("@ep"), ex.queryProp("Message")));
  29. }
  30. }
  31. if (me->ordinality())
  32. throw me.getClear();
  33. }
  34. static inline unsigned waitMsToSeconds(unsigned wait)
  35. {
  36. if (wait==0 || wait==(unsigned)-1)
  37. return wait;
  38. return wait/1000;
  39. }
  40. IPropertyTree *sendRoxieControlQuery(ISocket *sock, const char *msg, unsigned wait)
  41. {
  42. size32_t msglen = strlen(msg);
  43. size32_t len = msglen;
  44. _WINREV(len);
  45. sock->write(&len, sizeof(len));
  46. sock->write(msg, msglen);
  47. StringBuffer resp;
  48. for (;;)
  49. {
  50. sock->read(&len, sizeof(len));
  51. if (!len)
  52. break;
  53. _WINREV(len);
  54. size32_t size_read;
  55. sock->read(resp.reserveTruncate(len), len, len, size_read, waitMsToSeconds(wait));
  56. if (size_read<len)
  57. throw MakeStringException(-1, "Error reading roxie control message response");
  58. }
  59. if (resp.isEmpty())
  60. throw MakeStringException(EMPTY_RESULT_FAILURE, "Empty response string for roxie control request(%s) wait(%d)", msg, wait);
  61. Owned<IPropertyTree> ret = createPTreeFromXMLString(resp.str());
  62. if (!ret)
  63. throw MakeStringException(EMPTY_RESULT_FAILURE, "Empty result tree for roxie control request(%s) wait(%d)", msg, wait);
  64. checkRoxieControlExceptions(ret);
  65. return ret.getClear();
  66. }
  67. bool sendRoxieControlLock(ISocket *sock, bool allOrNothing, unsigned wait)
  68. {
  69. Owned<IPropertyTree> resp = sendRoxieControlQuery(sock, "<control:lock/>", wait);
  70. if (allOrNothing)
  71. {
  72. int lockCount = resp->getPropInt("Lock", 0);
  73. int serverCount = resp->getPropInt("NumServers", 0);
  74. return (lockCount && (lockCount == serverCount));
  75. }
  76. return resp->getPropInt("Lock", 0) != 0;
  77. }
  78. static inline unsigned remainingMsWait(unsigned wait, unsigned start)
  79. {
  80. if (wait==0 || wait==(unsigned)-1)
  81. return wait;
  82. unsigned waited = msTick()-start;
  83. return (wait>waited) ? wait-waited : 0;
  84. }
  85. IPropertyTree *sendRoxieControlAllNodes(ISocket *sock, const char *msg, bool allOrNothing, unsigned wait)
  86. {
  87. unsigned start = msTick();
  88. if (!sendRoxieControlLock(sock, allOrNothing, wait))
  89. throw MakeStringException(-1, "Roxie is too busy (control:lock failed) - please try again later.");
  90. return sendRoxieControlQuery(sock, msg, remainingMsWait(wait, start));
  91. }
  92. IPropertyTree *sendRoxieControlAllNodes(const SocketEndpoint &ep, const char *msg, bool allOrNothing, unsigned wait)
  93. {
  94. Owned<ISocket> sock = ISocket::connect_timeout(ep, wait);
  95. return sendRoxieControlAllNodes(sock, msg, allOrNothing, wait);
  96. }