thkeydiff.cpp 10 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. virtual void init()
  36. {
  37. helper = (IHThorKeyDiffArg *)queryHelper();
  38. OwnedRoxieString origName(helper->getOriginalName());
  39. OwnedRoxieString updatedName(helper->getUpdatedName());
  40. originalIndexFile.setown(queryThorFileManager().lookup(container.queryJob(), origName));
  41. newIndexFile.setown(queryThorFileManager().lookup(container.queryJob(), updatedName));
  42. if (originalIndexFile->numParts() != newIndexFile->numParts())
  43. throw MakeActivityException(this, TE_KeyDiffIndexSizeMismatch, "Index %s and %s differ in width", origName.get(), updatedName.get());
  44. if (originalIndexFile->querySuperFile() || newIndexFile->querySuperFile())
  45. throw MakeActivityException(this, 0, "Diffing super files not supported");
  46. width = originalIndexFile->numParts();
  47. originalDesc.setown(originalIndexFile->getFileDescriptor());
  48. newIndexDesc.setown(newIndexFile->getFileDescriptor());
  49. Owned<IPartDescriptor> tlkDesc = originalDesc->getPart(originalDesc->numParts()-1);
  50. const char *kind = tlkDesc->queryProperties().queryProp("@kind");
  51. local = NULL == kind || 0 != stricmp("topLevelKey", kind);
  52. if (!local)
  53. width--; // 1 part == No n distributed / Monolithic key
  54. if (width > container.queryJob().querySlaves())
  55. 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());
  56. queryThorFileManager().noteFileRead(container.queryJob(), originalIndexFile);
  57. queryThorFileManager().noteFileRead(container.queryJob(), newIndexFile);
  58. IArrayOf<IGroup> groups;
  59. OwnedRoxieString outputName(helper->getOutputName());
  60. fillClusterArray(container.queryJob(), outputName, clusters, groups);
  61. patchDesc.setown(queryThorFileManager().create(container.queryJob(), outputName, clusters, groups, 0 != (KDPoverwrite & helper->getFlags()), 0, !local, width));
  62. patchDesc->queryProperties().setProp("@kind", "keydiff");
  63. }
  64. virtual void serializeSlaveData(MemoryBuffer &dst, unsigned slave)
  65. {
  66. if (slave < width) // if false - due to mismatch width fitting - fill in with a blank entry
  67. {
  68. dst.append(true);
  69. Owned<IPartDescriptor> originalPartDesc = originalDesc->getPart(slave);
  70. originalPartDesc->serialize(dst);
  71. Owned<IPartDescriptor> newIndexPartDesc = newIndexDesc->getPart(slave);
  72. newIndexPartDesc->serialize(dst);
  73. patchDesc->queryPart(slave)->serialize(dst);
  74. if (0 == slave)
  75. {
  76. if (!local)
  77. {
  78. dst.append(true);
  79. Owned<IPartDescriptor> originalTlkPartDesc = originalDesc->getPart(originalDesc->numParts()-1);
  80. originalTlkPartDesc->serialize(dst);
  81. Owned<IPartDescriptor> newIndexTlkPartDesc = newIndexDesc->getPart(newIndexDesc->numParts()-1);
  82. newIndexTlkPartDesc->serialize(dst);
  83. patchDesc->queryPart(patchDesc->numParts()-1)->serialize(dst);
  84. }
  85. else
  86. dst.append(false);
  87. }
  88. }
  89. else
  90. dst.append(false); // no part
  91. }
  92. virtual void slaveDone(size32_t slaveIdx, MemoryBuffer &mb)
  93. {
  94. if (mb.length()) // if 0 implies aborted out from this slave.
  95. {
  96. offset_t size;
  97. mb.read(size);
  98. CDateTime modifiedTime(mb);
  99. IPartDescriptor *partDesc = patchDesc->queryPart(slaveIdx);
  100. IPropertyTree &props = partDesc->queryProperties();
  101. StringBuffer timeStr;
  102. modifiedTime.getString(timeStr);
  103. props.setProp("@modified", timeStr.str());
  104. unsigned crc;
  105. mb.read(crc);
  106. props.setPropInt64("@fileCrc", crc);
  107. if (!local && 0 == slaveIdx)
  108. {
  109. IPartDescriptor *partDesc = patchDesc->queryPart(patchDesc->numParts()-1);
  110. IPropertyTree &props = partDesc->queryProperties();
  111. mb.read(size);
  112. props.setPropInt64("@size", size);
  113. CDateTime modifiedTime(mb);
  114. StringBuffer timeStr;
  115. modifiedTime.getString(timeStr);
  116. props.setProp("@modified", timeStr.str());
  117. if (copyTlk)
  118. {
  119. Owned<IPartDescriptor> tlkDesc = newIndexDesc->getPart(newIndexDesc->numParts()-1);
  120. if (partDesc->getCrc(crc))
  121. props.setPropInt64("@fileCrc", crc);
  122. props.setProp("@diffFormat", "copy");
  123. }
  124. else
  125. {
  126. mb.read(crc);
  127. props.setPropInt64("@fileCrc", crc);
  128. props.setProp("@diffFormat", "diffV1");
  129. }
  130. }
  131. }
  132. }
  133. virtual void done()
  134. {
  135. StringBuffer scopedName;
  136. OwnedRoxieString outputName(helper->getOutputName());
  137. queryThorFileManager().addScope(container.queryJob(), outputName, scopedName);
  138. Owned<IWorkUnit> wu = &container.queryJob().queryWorkUnit().lock();
  139. Owned<IWUResult> r = wu->updateResultBySequence(helper->getSequence());
  140. r->setResultStatus(ResultStatusCalculated);
  141. r->setResultLogicalName(scopedName.str());
  142. r.clear();
  143. wu.clear();
  144. IPropertyTree &patchProps = patchDesc->queryProperties();
  145. if (0 != (helper->getFlags() & KDPexpires))
  146. setExpiryTime(patchProps, helper->getExpiryDays());
  147. IPropertyTree &originalProps = originalDesc->queryProperties();;
  148. if (originalProps.queryProp("ECL"))
  149. patchProps.setProp("ECL", originalProps.queryProp("ECL"));
  150. if (originalProps.getPropBool("@local"))
  151. patchProps.setPropBool("@local", true);
  152. container.queryTempHandler()->registerFile(outputName, container.queryOwner().queryGraphId(), 0, false, WUFileStandard, &clusters);
  153. Owned<IDistributedFile> patchFile;
  154. // set part sizes etc
  155. queryThorFileManager().publish(container.queryJob(), outputName, false, *patchDesc, &patchFile, 0, false);
  156. try { // set file size
  157. if (patchFile) {
  158. __int64 fs = patchFile->getFileSize(true,false);
  159. if (fs!=-1)
  160. patchFile->queryAttributes().setPropInt64("@size",fs);
  161. }
  162. }
  163. catch (IException *e) {
  164. EXCLOG(e,"keydiff setting file size");
  165. e->Release();
  166. }
  167. // Add a new 'Patch' description to the secondary key.
  168. DistributedFilePropertyLock lock(newIndexFile);
  169. IPropertyTree &fileProps = lock.queryAttributes();
  170. StringBuffer path("Patch[@name=\"");
  171. path.append(scopedName.str()).append("\"]");
  172. IPropertyTree *patch = fileProps.queryPropTree(path.str());
  173. if (!patch) patch = fileProps.addPropTree("Patch", createPTree());
  174. patch->setProp("@name", scopedName.str());
  175. unsigned checkSum;
  176. if (patchFile->getFileCheckSum(checkSum))
  177. patch->setPropInt64("@checkSum", checkSum);
  178. IPropertyTree *index = patch->setPropTree("Index", createPTree());
  179. index->setProp("@name", originalIndexFile->queryLogicalName());
  180. if (originalIndexFile->getFileCheckSum(checkSum))
  181. index->setPropInt64("@checkSum", checkSum);
  182. originalIndexFile->setAccessed();
  183. newIndexFile->setAccessed();
  184. }
  185. virtual 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. virtual 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. }