roxiecontrol.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. void checkRoxieControlExceptions(IPropertyTree *msg)
  17. {
  18. Owned<IMultiException> me = MakeMultiException();
  19. Owned<IPropertyTreeIterator> endpoints = msg->getElements("Endpoint");
  20. ForEach(*endpoints)
  21. {
  22. IPropertyTree &endp = endpoints->query();
  23. Owned<IPropertyTreeIterator> exceptions = endp.getElements("Exception");
  24. ForEach (*exceptions)
  25. {
  26. IPropertyTree &ex = exceptions->query();
  27. me->append(*MakeStringException(ex.getPropInt("Code"), "Endpoint %s: %s", endp.queryProp("@ep"), ex.queryProp("Message")));
  28. }
  29. }
  30. if (me->ordinality())
  31. throw me.getClear();
  32. }
  33. static inline unsigned waitMsToSeconds(unsigned wait)
  34. {
  35. if (wait==0 || wait==(unsigned)-1)
  36. return wait;
  37. return wait/1000;
  38. }
  39. IPropertyTree *sendRoxieControlQuery(ISocket *sock, const char *msg, unsigned wait)
  40. {
  41. size32_t msglen = strlen(msg);
  42. size32_t len = msglen;
  43. _WINREV(len);
  44. sock->write(&len, sizeof(len));
  45. sock->write(msg, msglen);
  46. StringBuffer resp;
  47. for (;;)
  48. {
  49. sock->read(&len, sizeof(len));
  50. if (!len)
  51. break;
  52. _WINREV(len);
  53. size32_t size_read;
  54. sock->read(resp.reserveTruncate(len), len, len, size_read, waitMsToSeconds(wait));
  55. if (size_read<len)
  56. throw MakeStringException(-1, "Error reading roxie control message response");
  57. }
  58. Owned<IPropertyTree> ret = createPTreeFromXMLString(resp.str());
  59. checkRoxieControlExceptions(ret);
  60. return ret.getClear();
  61. }
  62. bool sendRoxieControlLock(ISocket *sock, bool allOrNothing, unsigned wait)
  63. {
  64. Owned<IPropertyTree> resp = sendRoxieControlQuery(sock, "<control:lock/>", wait);
  65. if (allOrNothing)
  66. {
  67. int lockCount = resp->getPropInt("Lock", 0);
  68. int serverCount = resp->getPropInt("NumServers", 0);
  69. return (lockCount && (lockCount == serverCount));
  70. }
  71. return resp->getPropInt("Lock", 0) != 0;
  72. }
  73. static inline unsigned remainingMsWait(unsigned wait, unsigned start)
  74. {
  75. if (wait==0 || wait==(unsigned)-1)
  76. return wait;
  77. unsigned waited = msTick()-start;
  78. return (wait>waited) ? wait-waited : 0;
  79. }
  80. IPropertyTree *sendRoxieControlAllNodes(ISocket *sock, const char *msg, bool allOrNothing, unsigned wait)
  81. {
  82. unsigned start = msTick();
  83. if (!sendRoxieControlLock(sock, allOrNothing, wait))
  84. throw MakeStringException(-1, "Roxie control:lock failed");
  85. return sendRoxieControlQuery(sock, msg, remainingMsWait(wait, start));
  86. }
  87. IPropertyTree *sendRoxieControlAllNodes(const SocketEndpoint &ep, const char *msg, bool allOrNothing, unsigned wait)
  88. {
  89. Owned<ISocket> sock = ISocket::connect_timeout(ep, wait);
  90. return sendRoxieControlAllNodes(sock, msg, allOrNothing, wait);
  91. }