thkeydiffslave.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "jiface.hpp"
  15. #include "jtime.hpp"
  16. #include "jfile.ipp"
  17. #include "keydiff.hpp"
  18. #include "backup.hpp"
  19. #include "thexception.hpp"
  20. #include "thmfilemanager.hpp"
  21. #include "thbuf.hpp"
  22. #include "slave.ipp"
  23. #include "thactivityutil.ipp"
  24. class CKeyDiffSlave : public ProcessSlaveActivity
  25. {
  26. IHThorKeyDiffArg *helper;
  27. Owned<IPartDescriptor> originalIndexPart, updatedIndexPart, patchPart;
  28. Owned<IPartDescriptor> originalIndexTlkPart, updatedIndexTlkPart, patchTlkPart;
  29. Owned<IKeyDiffGenerator> diffGenerator, tlkDiffGenerator;
  30. bool tlk, copyTlk;
  31. unsigned patchCrc, tlkPatchCrc;
  32. public:
  33. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  34. CKeyDiffSlave(CGraphElementBase *container) : ProcessSlaveActivity(container)
  35. {
  36. helper = NULL;
  37. tlk = false;
  38. copyTlk = globals->getPropBool("@diffCopyTlk", true); // because tlk can have meta data and diff/patch does not support
  39. }
  40. ~CKeyDiffSlave()
  41. {
  42. }
  43. void init(MemoryBuffer &data, MemoryBuffer &slaveData)
  44. {
  45. helper = (IHThorKeyDiffArg *)queryHelper();
  46. bool active;
  47. data.read(active);
  48. if (!active)
  49. {
  50. abortSoon = true;
  51. return;
  52. }
  53. originalIndexPart.setown(deserializePartFileDescriptor(data));
  54. updatedIndexPart.setown(deserializePartFileDescriptor(data));
  55. patchPart.setown(deserializePartFileDescriptor(data));
  56. if (1 == container.queryJob().queryMyRank())
  57. {
  58. data.read(tlk);
  59. if (tlk)
  60. {
  61. originalIndexTlkPart.setown(deserializePartFileDescriptor(data));
  62. updatedIndexTlkPart.setown(deserializePartFileDescriptor(data));
  63. patchTlkPart.setown(deserializePartFileDescriptor(data));
  64. }
  65. }
  66. StringBuffer originalFilePart, updatedFilePart;
  67. locateFilePartPath(this, helper->queryOriginalName(), *originalIndexPart, originalFilePart);
  68. locateFilePartPath(this, helper->queryUpdatedName(), *updatedIndexPart, updatedFilePart);
  69. StringBuffer patchFilePath;
  70. getPartFilename(*patchPart, 0, patchFilePath);
  71. if (globals->getPropBool("@replicateAsync", true))
  72. cancelReplicates(this, *patchPart);
  73. ensureDirectoryForFile(patchFilePath.str());
  74. diffGenerator.setown(createKeyDiffGenerator(originalFilePart.str(), updatedFilePart.str(), patchFilePath.str(), 0, true, COMPRESS_METHOD_LZMA));
  75. ActPrintLog("KEYPATCH: handling original index = %s, updated index = %s, output patch = %s", originalFilePart.str(), updatedFilePart.str(), patchFilePath.str());
  76. if (tlk)
  77. {
  78. if (globals->getPropBool("@replicateAsync", true))
  79. cancelReplicates(this, *patchTlkPart);
  80. if (!copyTlk)
  81. {
  82. StringBuffer tmp;
  83. locateFilePartPath(this, tmp.clear().append(helper->queryOriginalName()).append(" [TLK]").str(), *originalIndexTlkPart, originalFilePart.clear());
  84. locateFilePartPath(this, tmp.clear().append(helper->queryUpdatedName()).append(" [TLK]").str(), *updatedIndexTlkPart, updatedFilePart.clear());
  85. getPartFilename(*patchTlkPart, 0, tmp.clear());
  86. tlkDiffGenerator.setown(createKeyDiffGenerator(originalFilePart.str(), updatedFilePart.str(), tmp.str(), 0, true, COMPRESS_METHOD_LZMA));
  87. }
  88. }
  89. }
  90. virtual void process()
  91. {
  92. processed = THORDATALINK_STARTED;
  93. if (abortSoon) return;
  94. try
  95. {
  96. diffGenerator->run();
  97. diffGenerator->logStats(); // JCSMORE - may want some into svg graph.
  98. patchCrc = diffGenerator->queryPatchFileCRC();
  99. diffGenerator.clear();
  100. if (tlk && !copyTlk)
  101. {
  102. tlkDiffGenerator->run();
  103. tlkDiffGenerator->logStats();
  104. tlkPatchCrc = tlkDiffGenerator->queryPatchFileCRC();
  105. tlkDiffGenerator.clear();
  106. }
  107. try
  108. {
  109. if (patchPart->numCopies() > 1)
  110. doReplicate(this, *patchPart);
  111. if (tlk && copyTlk)
  112. {
  113. StringBuffer patchFilePathTlk, updatedFilePartTlk, tmp;
  114. getPartFilename(*patchTlkPart, 0, patchFilePathTlk);
  115. locateFilePartPath(this, tmp.append(helper->queryUpdatedName()).append(" [TLK]").str(), *updatedIndexTlkPart, updatedFilePartTlk);
  116. OwnedIFile dstIFileTlk = createIFile(patchFilePathTlk.str());
  117. OwnedIFile updatedIFileTlk = createIFile(updatedFilePartTlk.str());
  118. copyFile(dstIFileTlk, updatedIFileTlk);
  119. if (patchTlkPart->numCopies() > 1)
  120. doReplicate(this, *patchTlkPart);
  121. }
  122. }
  123. catch (IException *e)
  124. {
  125. ActPrintLog(e, "Failure to create backup patch files");
  126. throw;
  127. }
  128. }
  129. catch (IException *e)
  130. {
  131. ActPrintLog(e, "KEYPATH exception");
  132. throw;
  133. }
  134. }
  135. virtual void endProcess()
  136. {
  137. if (processed & THORDATALINK_STARTED)
  138. processed |= THORDATALINK_STOPPED;
  139. }
  140. virtual void processDone(MemoryBuffer &mb)
  141. {
  142. if (abortSoon)
  143. return;
  144. StringBuffer tmpStr;
  145. Owned<IFile> ifile = createIFile(getPartFilename(*patchPart, 0, tmpStr).str());
  146. offset_t sz = ifile->size();
  147. if (-1 != sz)
  148. container.queryJob().queryIDiskUsage().increase(sz);
  149. mb.append(sz);
  150. CDateTime createTime, modifiedTime, accessedTime;
  151. ifile->getTime(&createTime, &modifiedTime, &accessedTime);
  152. // round file time down to nearest sec. Nanosec accuracy is not preserved elsewhere and can lead to mismatch later.
  153. unsigned hour, min, sec, nanosec;
  154. modifiedTime.getTime(hour, min, sec, nanosec);
  155. modifiedTime.setTime(hour, min, sec, 0);
  156. modifiedTime.serialize(mb);
  157. mb.append(patchCrc);
  158. if (tlk)
  159. {
  160. Owned<IFile> ifile = createIFile(getPartFilename(*patchTlkPart, 0, tmpStr.clear()).str());
  161. offset_t sz = ifile->size();
  162. if (-1 != sz)
  163. container.queryJob().queryIDiskUsage().increase(sz);
  164. mb.append(sz);
  165. CDateTime createTime, modifiedTime, accessedTime;
  166. ifile->getTime(&createTime, &modifiedTime, &accessedTime);
  167. // round file time down to nearest sec. Nanosec accuracy is not preserved elsewhere and can lead to mismatch later.
  168. unsigned hour, min, sec, nanosec;
  169. modifiedTime.getTime(hour, min, sec, nanosec);
  170. modifiedTime.setTime(hour, min, sec, 0);
  171. modifiedTime.serialize(mb);
  172. if (!copyTlk)
  173. mb.append(tlkPatchCrc);
  174. }
  175. }
  176. };
  177. activityslaves_decl CActivityBase *createKeyDiffSlave(CGraphElementBase *container)
  178. {
  179. return new CKeyDiffSlave(container);
  180. }