thkeydiff.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "jiface.hpp"
  14. #include "dadfs.hpp"
  15. #include "thorfile.hpp"
  16. #include "eclhelper.hpp"
  17. #include "thexception.hpp"
  18. #include "thkeydiff.ipp"
  19. class CKeyDiffMaster : public CMasterActivity
  20. {
  21. IHThorKeyDiffArg *helper;
  22. Owned<IDistributedFile> originalIndexFile, newIndexFile;
  23. bool local, copyTlk;
  24. unsigned width;
  25. Owned<IFileDescriptor> patchDesc, originalDesc, newIndexDesc;
  26. StringArray clusters;
  27. public:
  28. CKeyDiffMaster(CMasterGraphElement *info) : CMasterActivity(info)
  29. {
  30. helper = NULL;
  31. local = false;
  32. width = 0;
  33. copyTlk = globals->getPropBool("@diffCopyTlk", true); // because tlk can have meta data and diff/patch does not support
  34. }
  35. ~CKeyDiffMaster()
  36. {
  37. }
  38. void init()
  39. {
  40. helper = (IHThorKeyDiffArg *)queryHelper();
  41. OwnedRoxieString origName(helper->getOriginalName());
  42. OwnedRoxieString updatedName(helper->getUpdatedName());
  43. originalIndexFile.setown(queryThorFileManager().lookup(container.queryJob(), origName));
  44. newIndexFile.setown(queryThorFileManager().lookup(container.queryJob(), updatedName));
  45. if (originalIndexFile->numParts() != newIndexFile->numParts())
  46. throw MakeActivityException(this, TE_KeyDiffIndexSizeMismatch, "Index %s and %s differ in width", origName.get(), updatedName.get());
  47. if (originalIndexFile->querySuperFile() || newIndexFile->querySuperFile())
  48. throw MakeActivityException(this, 0, "Diffing super files not supported");
  49. width = originalIndexFile->numParts();
  50. originalDesc.setown(originalIndexFile->getFileDescriptor());
  51. newIndexDesc.setown(newIndexFile->getFileDescriptor());
  52. Owned<IPartDescriptor> tlkDesc = originalDesc->getPart(originalDesc->numParts()-1);
  53. const char *kind = tlkDesc->queryProperties().queryProp("@kind");
  54. local = NULL == kind || 0 != stricmp("topLevelKey", kind);
  55. if (!local)
  56. width--; // 1 part == No n distributed / Monolithic key
  57. if (width > container.queryJob().querySlaves())
  58. throw MakeActivityException(this, 0, "Unsupported: keydiff(%s, %s) - Cannot diff a key that's wider(%d) than the target cluster size(%d)", originalIndexFile->queryLogicalName(), newIndexFile->queryLogicalName(), width, container.queryJob().querySlaves());
  59. queryThorFileManager().noteFileRead(container.queryJob(), originalIndexFile);
  60. queryThorFileManager().noteFileRead(container.queryJob(), newIndexFile);
  61. IArrayOf<IGroup> groups;
  62. OwnedRoxieString outputName(helper->getOutputName());
  63. fillClusterArray(container.queryJob(), outputName, clusters, groups);
  64. patchDesc.setown(queryThorFileManager().create(container.queryJob(), outputName, clusters, groups, 0 != (KDPoverwrite & helper->getFlags()), 0, !local, width));
  65. patchDesc->queryProperties().setProp("@kind", "keydiff");
  66. }
  67. void serializeSlaveData(MemoryBuffer &dst, unsigned slave)
  68. {
  69. if (slave < width) // if false - due to mismatch width fitting - fill in with a blank entry
  70. {
  71. dst.append(true);
  72. Owned<IPartDescriptor> originalPartDesc = originalDesc->getPart(slave);
  73. originalPartDesc->serialize(dst);
  74. Owned<IPartDescriptor> newIndexPartDesc = newIndexDesc->getPart(slave);
  75. newIndexPartDesc->serialize(dst);
  76. patchDesc->queryPart(slave)->serialize(dst);
  77. if (0 == slave)
  78. {
  79. if (!local)
  80. {
  81. dst.append(true);
  82. Owned<IPartDescriptor> originalTlkPartDesc = originalDesc->getPart(originalDesc->numParts()-1);
  83. originalTlkPartDesc->serialize(dst);
  84. Owned<IPartDescriptor> newIndexTlkPartDesc = newIndexDesc->getPart(newIndexDesc->numParts()-1);
  85. newIndexTlkPartDesc->serialize(dst);
  86. patchDesc->queryPart(patchDesc->numParts()-1)->serialize(dst);
  87. }
  88. else
  89. dst.append(false);
  90. }
  91. }
  92. else
  93. dst.append(false); // no part
  94. }
  95. void slaveDone(size32_t slaveIdx, MemoryBuffer &mb)
  96. {
  97. if (mb.length()) // if 0 implies aborted out from this slave.
  98. {
  99. offset_t size;
  100. mb.read(size);
  101. CDateTime modifiedTime(mb);
  102. IPartDescriptor *partDesc = patchDesc->queryPart(slaveIdx);
  103. IPropertyTree &props = partDesc->queryProperties();
  104. StringBuffer timeStr;
  105. modifiedTime.getString(timeStr);
  106. props.setProp("@modified", timeStr.str());
  107. unsigned crc;
  108. mb.read(crc);
  109. props.setPropInt64("@fileCrc", crc);
  110. if (!local && 0 == slaveIdx)
  111. {
  112. IPartDescriptor *partDesc = patchDesc->queryPart(patchDesc->numParts()-1);
  113. IPropertyTree &props = partDesc->queryProperties();
  114. mb.read(size);
  115. props.setPropInt64("@size", size);
  116. CDateTime modifiedTime(mb);
  117. StringBuffer timeStr;
  118. modifiedTime.getString(timeStr);
  119. props.setProp("@modified", timeStr.str());
  120. if (copyTlk)
  121. {
  122. Owned<IPartDescriptor> tlkDesc = newIndexDesc->getPart(newIndexDesc->numParts()-1);
  123. if (partDesc->getCrc(crc))
  124. props.setPropInt64("@fileCrc", crc);
  125. props.setProp("@diffFormat", "copy");
  126. }
  127. else
  128. {
  129. mb.read(crc);
  130. props.setPropInt64("@fileCrc", crc);
  131. props.setProp("@diffFormat", "diffV1");
  132. }
  133. }
  134. }
  135. }
  136. void done()
  137. {
  138. StringBuffer scopedName;
  139. OwnedRoxieString outputName(helper->getOutputName());
  140. queryThorFileManager().addScope(container.queryJob(), outputName, scopedName);
  141. Owned<IWorkUnit> wu = &container.queryJob().queryWorkUnit().lock();
  142. Owned<IWUResult> r = wu->updateResultBySequence(helper->getSequence());
  143. r->setResultStatus(ResultStatusCalculated);
  144. r->setResultLogicalName(scopedName.str());
  145. r.clear();
  146. wu.clear();
  147. IPropertyTree &patchProps = patchDesc->queryProperties();
  148. setExpiryTime(patchProps, helper->getExpiryDays());
  149. IPropertyTree &originalProps = originalDesc->queryProperties();;
  150. if (originalProps.queryProp("ECL"))
  151. patchProps.setProp("ECL", originalProps.queryProp("ECL"));
  152. if (originalProps.getPropBool("@local"))
  153. patchProps.setPropBool("@local", true);
  154. container.queryTempHandler()->registerFile(outputName, container.queryOwner().queryGraphId(), 0, false, WUFileStandard, &clusters);
  155. Owned<IDistributedFile> patchFile;
  156. // set part sizes etc
  157. queryThorFileManager().publish(container.queryJob(), outputName, false, *patchDesc, &patchFile, 0, false);
  158. try { // set file size
  159. if (patchFile) {
  160. __int64 fs = patchFile->getFileSize(true,false);
  161. if (fs!=-1)
  162. patchFile->queryAttributes().setPropInt64("@size",fs);
  163. }
  164. }
  165. catch (IException *e) {
  166. EXCLOG(e,"keydiff setting file size");
  167. e->Release();
  168. }
  169. // Add a new 'Patch' description to the secondary key.
  170. DistributedFilePropertyLock lock(newIndexFile);
  171. IPropertyTree &fileProps = lock.queryAttributes();
  172. StringBuffer path("Patch[@name=\"");
  173. path.append(scopedName.str()).append("\"]");
  174. IPropertyTree *patch = fileProps.queryPropTree(path.str());
  175. if (!patch) patch = fileProps.addPropTree("Patch", createPTree());
  176. patch->setProp("@name", scopedName.str());
  177. unsigned checkSum;
  178. if (patchFile->getFileCheckSum(checkSum))
  179. patch->setPropInt64("@checkSum", checkSum);
  180. IPropertyTree *index = patch->setPropTree("Index", createPTree());
  181. index->setProp("@name", originalIndexFile->queryLogicalName());
  182. if (originalIndexFile->getFileCheckSum(checkSum))
  183. index->setPropInt64("@checkSum", checkSum);
  184. }
  185. void preStart(size32_t parentExtractSz, const byte *parentExtract)
  186. {
  187. CMasterActivity::preStart(parentExtractSz, parentExtract);
  188. IHThorKeyDiffArg *helper = (IHThorKeyDiffArg *) queryHelper();
  189. if (0==(KDPoverwrite & helper->getFlags()))
  190. {
  191. if (KDPvaroutputname & helper->getFlags()) return;
  192. OwnedRoxieString outputName(helper->getOutputName());
  193. Owned<IDistributedFile> file = queryThorFileManager().lookup(container.queryJob(), outputName, false, true);
  194. if (file)
  195. throw MakeActivityException(this, TE_OverwriteNotSpecified, "Cannot write %s, file already exists (missing OVERWRITE attribute?)", file->queryLogicalName());
  196. }
  197. }
  198. void kill()
  199. {
  200. CMasterActivity::kill();
  201. originalIndexFile.clear();
  202. newIndexFile.clear();
  203. }
  204. };
  205. CActivityBase *createKeyDiffActivityMaster(CMasterGraphElement *container)
  206. {
  207. return new CKeyDiffMaster(container);
  208. }