forked from hpcc-systems/HPCC-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdafdesc.cpp
2912 lines (2653 loc) · 91.4 KB
/
dafdesc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*##############################################################################
HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
############################################################################## */
#define da_decl __declspec(dllexport)
#include "platform.h"
#include "portlist.h"
#include "jlib.hpp"
#include "jfile.hpp"
#include "jiter.ipp"
#include "jmisc.hpp"
#include "jexcept.hpp"
#include "jptree.hpp"
#include "jlzw.hpp"
#include "dafdesc.hpp"
#include "rmtfile.hpp"
#include "dautils.hpp"
#include "dasds.hpp"
#include "dafdesc.hpp"
#include "dadfs.hpp"
#define INCLUDE_1_OF_1 // whether to use 1_of_1 for single part files
#define SDS_CONNECT_TIMEOUT (1000*60*60*2) // better than infinite
#define SERIALIZATION_VERSION ((byte)0xd4)
#define SERIALIZATION_VERSION2 ((byte)0xd5) // with trailing superfile info
bool isMulti(const char *str)
{
if (str&&!isSpecialPath(str))
loop {
switch (*str) {
case ',':
case '*':
case '?':
return true;
case 0:
return false;
}
str++;
}
return false;
}
bool isCompressed(IPropertyTree &props, bool *blocked)
{
if (props.getPropBool("@blockCompressed"))
{
if (blocked) *blocked = true;
return true;
}
else
{
if (blocked) *blocked = false;
return props.getPropBool("@rowCompressed");
}
}
bool getCrcFromPartProps(IPropertyTree &fileattr,IPropertyTree &props, unsigned &crc)
{
if (props.hasProp("@fileCrc"))
{
crc = (unsigned)props.getPropInt64("@fileCrc");
return true;
}
// NB: old @crc keys and compressed were not crc of file but of data within.
const char *kind = props.queryProp("@kind");
if (kind&&strcmp(kind,"key")) // key part
return false;
bool blocked;
if (isCompressed(fileattr,&blocked)) {
if (!blocked)
return false;
crc = COMPRESSEDFILECRC;
return true;
}
if (!props.hasProp("@crc"))
return false;
crc = (unsigned)props.getPropInt64("@crc");
return true;
}
void ClusterPartDiskMapSpec::setRoxie (unsigned redundancy, unsigned channelsPerNode, int _replicateOffset)
{
flags = 0;
replicateOffset = _replicateOffset?_replicateOffset:1;
defaultCopies = redundancy+1;
if ((channelsPerNode>1)&&(redundancy==0)) {
flags |= CPDMSF_wrapToNextDrv;
flags |= CPDMSF_overloadedConfig;
maxDrvs = channelsPerNode;
}
else
maxDrvs = (redundancy>1)?(redundancy+1):2;
if (_replicateOffset==0)
flags |= CPDMSF_fillWidth;
startDrv = 0;
}
bool ClusterPartDiskMapSpec::calcPartLocation (unsigned part, unsigned maxparts, unsigned copy, unsigned clusterwidth, unsigned &node, unsigned &drv)
{
// this is more cryptic than it could be (e.g. by special casing)
// because it handles the cases that aren't going to ever happen, in a general way
node = 0;
drv = 0;
if (!clusterwidth||!maxparts)
return false;
if (part>=maxparts)
return false;
unsigned nc = numCopies(part,clusterwidth,maxparts);
if (copy>=nc)
return false;
unsigned dc=defaultCopies?defaultCopies:DFD_DefaultCopies;
drv = startDrv;
bool fw = (flags&CPDMSF_fillWidth)!=0;
if (fw&&(maxparts>clusterwidth/2))
fw = false;
// calc primary
node = part%clusterwidth;
unsigned repdrv = startDrv+1;
if (flags&CPDMSF_wrapToNextDrv) {
drv += startDrv+(part/clusterwidth)%maxDrvs;
repdrv = (1+(maxparts-1)/clusterwidth)%maxDrvs;
}
if (copy) {
if (fw) {
if (interleave>1)
ERRLOG("ClusterPartDiskMapSpec interleave not allowed if fill width set");
if (flags&CPDMSF_repeatedPart)
ERRLOG("ClusterPartDiskMapSpec repeated part not allowed if fill width set");
unsigned m = clusterwidth/maxparts;
drv = startDrv+(repdrv+(copy/m-1))%maxDrvs;
node += (copy%m)*maxparts;
}
else if ((flags&CPDMSF_repeatedPart)) {
if (flags&CPDMSF_wrapToNextDrv)
ERRLOG("ClusterPartDiskMapSpec repeated part not allowed if wrap to next drive set");
unsigned repnum = copy%dc;
unsigned nodenum = copy/dc;
drv = startDrv+repnum%maxDrvs;
if (interleave>1)
node = (node+nodenum+(replicateOffset*repnum*interleave))%clusterwidth;
else
node = (node+nodenum+(replicateOffset*repnum))%clusterwidth;
}
else {
drv = startDrv+(repdrv+copy-1)%maxDrvs;
if (interleave>1)
node = (node+(replicateOffset*copy*interleave))%clusterwidth;
else
node = (node+(replicateOffset*copy))%clusterwidth;
}
}
return true;
}
inline void setPropDef(IPropertyTree *tree,const char *prop,int val,int def)
{
if (val!=def)
tree->setPropInt(prop,val);
else
tree->removeProp(prop);
}
inline int getPropDef(IPropertyTree *tree,const char *prop,int def)
{
if (tree)
return tree->getPropInt(prop,def);
return def;
}
void ClusterPartDiskMapSpec::toProp(IPropertyTree *tree)
{
if (!tree)
return;
setPropDef(tree,"@replicateOffset",replicateOffset,1);
setPropDef(tree,"@redundancy",defaultCopies?(defaultCopies-1):1,1);
setPropDef(tree,"@maxDrvs",maxDrvs?maxDrvs:2,2);
setPropDef(tree,"@startDrv",startDrv,0);
setPropDef(tree,"@interleave",interleave,0);
setPropDef(tree,"@mapFlags",flags,0);
setPropDef(tree,"@repeatedPart",repeatedPart,(int)CPDMSRP_notRepeated);
if (defaultBaseDir.isEmpty())
tree->removeProp("@defaultBaseDir");
else
tree->setProp("@defaultBaseDir",defaultBaseDir);
if (defaultReplicateDir.isEmpty())
tree->removeProp("@defaultReplicateDir");
else
tree->setProp("@defaultReplicateDir",defaultReplicateDir);
}
void ClusterPartDiskMapSpec::fromProp(IPropertyTree *tree)
{
unsigned defrep = 1;
// if directory is specified then must match default base to be default replicated
StringBuffer dir;
if (tree&&tree->getProp("@directory",dir)) {
const char * base = queryBaseDirectory(grp_unknown, 0, SepCharBaseOs(getPathSepChar(dir.str())));
size32_t l = strlen(base);
if ((memcmp(base,dir.str(),l)!=0)||((l!=dir.length())&&!isPathSepChar(dir.charAt(l))))
defrep = 0;
}
replicateOffset = getPropDef(tree,"@replicateOffset",1);
defaultCopies = getPropDef(tree,"@redundancy",defrep)+1;
maxDrvs = (byte)getPropDef(tree,"@maxDrvs",2);
startDrv = (byte)getPropDef(tree,"@startDrv",defrep?0:getPathDrive(dir.str()));
interleave = getPropDef(tree,"@interleave",0);
flags = (byte)getPropDef(tree,"@mapFlags",0);
repeatedPart = (unsigned)getPropDef(tree,"@repeatedPart",(int)CPDMSRP_notRepeated);
setDefaultBaseDir(tree->queryProp("@defaultBaseDir"));
setDefaultReplicateDir(tree->queryProp("@defaultReplicateDir"));
}
void ClusterPartDiskMapSpec::serialize(MemoryBuffer &mb)
{
mb.append(flags);
mb.append(replicateOffset);
mb.append(defaultCopies);
mb.append(startDrv);
mb.append(maxDrvs);
mb.append(interleave);
if (flags&CPDMSF_repeatedPart)
mb.append(repeatedPart);
if (flags&CPDMSF_defaultBaseDir)
mb.append(defaultBaseDir);
if (flags&CPDMSF_defaultReplicateDir)
mb.append(defaultReplicateDir);
}
void ClusterPartDiskMapSpec::deserialize(MemoryBuffer &mb)
{
mb.read(flags);
mb.read(replicateOffset);
mb.read(defaultCopies);
mb.read(startDrv);
mb.read(maxDrvs);
mb.read(interleave);
if (flags&CPDMSF_repeatedPart)
mb.read(repeatedPart);
else
repeatedPart = CPDMSRP_notRepeated;
if (flags&CPDMSF_defaultBaseDir)
mb.read(defaultBaseDir);
else
defaultBaseDir.clear();
if (flags&CPDMSF_defaultReplicateDir)
mb.read(defaultReplicateDir);
else
defaultReplicateDir.clear();
}
void ClusterPartDiskMapSpec::ensureReplicate()
{
if (defaultCopies <= DFD_NoCopies)
defaultCopies = DFD_DefaultCopies;
}
bool ClusterPartDiskMapSpec::isReplicated() const
{
// If defaultCopies is zero (deprecated/legacy), the default value for replicated is true
// Else, if it has any copy (>= 2), than it is replicated
return defaultCopies != DFD_NoCopies;
}
unsigned ClusterPartDiskMapSpec::numCopies(unsigned part,unsigned clusterwidth, unsigned filewidth)
{
if (flags&CPDMSF_repeatedPart) {
if (repeatedPart&CPDMSRP_lastRepeated) {
if (part+1==filewidth)
return clusterwidth*defaultCopies;
}
else if ((part==(repeatedPart&CPDMSRP_partMask))||(repeatedPart&CPDMSRP_allRepeated))
return clusterwidth*defaultCopies;
if (repeatedPart&CPDMSRP_onlyRepeated)
return 0;
}
return defaultCopies;
}
void ClusterPartDiskMapSpec::setRepeatedCopies(unsigned partnum,bool onlyrepeats)
{
repeatedPart = partnum;
if (partnum!=CPDMSRP_notRepeated) {
flags |= CPDMSF_repeatedPart;
if (onlyrepeats)
repeatedPart |= CPDMSRP_onlyRepeated;
}
else
flags &= ~CPDMSF_repeatedPart;
}
void ClusterPartDiskMapSpec::setDefaultBaseDir(const char *dir)
{
defaultBaseDir.set(dir);
if (defaultBaseDir.isEmpty())
flags &= ~CPDMSF_defaultBaseDir;
else
flags |= CPDMSF_defaultBaseDir;
}
void ClusterPartDiskMapSpec::setDefaultReplicateDir(const char *dir)
{
defaultReplicateDir.set(dir);
if (defaultReplicateDir.isEmpty())
flags &= ~CPDMSF_defaultReplicateDir;
else
flags |= CPDMSF_defaultReplicateDir;
}
ClusterPartDiskMapSpec & ClusterPartDiskMapSpec::operator=(const ClusterPartDiskMapSpec &other)
{
replicateOffset = other.replicateOffset;
defaultCopies = other.defaultCopies;
maxDrvs = other.maxDrvs;
startDrv = other.startDrv;
flags = other.flags;
interleave = other.interleave;
repeatedPart = other.repeatedPart;
setDefaultBaseDir(other.defaultBaseDir);
setDefaultReplicateDir(other.defaultReplicateDir);
return *this;
}
// --------------------------------------------------------
static void removeDir(const char *name,const char *dir,StringBuffer &out)
{
const char *s=name;
const char *d=dir;
if (d&&*d) {
while (*s&&(toupper(*s)==toupper(*d))) {
s++;
d++;
}
if ((*d==0)&&isPathSepChar(*s)) // support cross OS
name = s+1;
}
out.append(name);
}
#define RO_SINGLE_PART (0x40000000) // used for singletons
struct CClusterInfo: public CInterface, implements IClusterInfo
{
Linked<IGroup> group;
StringAttr name; // group name
ClusterPartDiskMapSpec mspec;
void checkClusterName(INamedGroupStore *resolver)
{
// check name matches group
if (resolver&&group) {
if (!name.isEmpty()) {
StringBuffer defaultDir;
GroupType groupType;
Owned<IGroup> lgrp = resolver->lookup(name, defaultDir, groupType);
if (lgrp&&lgrp->equals(group))
{
if (mspec.defaultBaseDir.isEmpty())
{
mspec.setDefaultBaseDir(defaultDir); // MORE - should possibly set up the rest of the mspec info from the group info here
}
if (mspec.defaultCopies>1 && mspec.defaultReplicateDir.isEmpty())
{
mspec.setDefaultReplicateDir(queryBaseDirectory(groupType, 1)); // MORE - not sure this is strictly correct
}
return; // ok
}
name.clear();
}
StringBuffer gname;
if (resolver->find(group,gname,true)||(group->ordinality()>1))
name.set(gname);
}
}
public:
IMPLEMENT_IINTERFACE;
CClusterInfo(MemoryBuffer &mb,INamedGroupStore *resolver)
{
StringAttr grptext;
mb.read(grptext);
if (!grptext.isEmpty())
group.setown(createIGroup(grptext));
mspec.deserialize(mb);
mb.read(name);
checkClusterName(resolver);
}
CClusterInfo(const char *_name,IGroup *_group,const ClusterPartDiskMapSpec &_mspec,INamedGroupStore *resolver)
: name(_name),group(_group)
{
name.toLowerCase();
mspec =_mspec;
checkClusterName(resolver);
}
CClusterInfo(IPropertyTree *pt,INamedGroupStore *resolver,unsigned flags)
{
if (!pt)
return;
name.set(pt->queryProp("@name"));
mspec.fromProp(pt);
if ((((flags&IFDSF_EXCLUDE_GROUPS)==0)||name.isEmpty())&&pt->hasProp("Group"))
group.setown(createIGroup(pt->queryProp("Group")));
if (!name.isEmpty()&&!group.get()&&resolver)
{
StringBuffer defaultDir;
GroupType groupType;
group.setown(resolver->lookup(name.get(), defaultDir, groupType));
// MORE - common some of this with checkClusterName?
if (mspec.defaultBaseDir.isEmpty())
{
mspec.setDefaultBaseDir(defaultDir); // MORE - should possibly set up the rest of the mspec info from the group info here
}
if (mspec.defaultCopies>1 && mspec.defaultReplicateDir.isEmpty())
{
mspec.setDefaultReplicateDir(queryBaseDirectory(groupType, 1)); // MORE - not sure this is strictly correct
}
}
else
checkClusterName(resolver);
}
const char *queryGroupName()
{
return name.isEmpty()?NULL:name.get();
}
IGroup *queryGroup(IGroupResolver *resolver)
{
if (!group&&!name.isEmpty()&&resolver)
group.setown(resolver->lookup(name));
return group.get();
}
StringBuffer &getGroupName(StringBuffer &ret,IGroupResolver *resolver)
{
if (name.isEmpty()) {
if (group)
{
if (resolver)
resolver->find(group,ret,true); // this will set single node as well
else if (group->ordinality()==1)
group->getText(ret);
}
}
else
ret.append(name);
return ret;
}
void serialize(MemoryBuffer &mb)
{
StringBuffer grptext;
if (group)
group->getText(grptext);
mb.append(grptext);
mspec.serialize(mb);
mb.append(name);
}
INode *queryNode(unsigned idx,unsigned maxparts,unsigned copy)
{
if (!group.get())
return queryNullNode();
unsigned nn;
unsigned dn;
if (!mspec.calcPartLocation (idx,maxparts,copy, group->ordinality(), nn, dn))
return queryNullNode();
return &group->queryNode(nn);
}
unsigned queryDrive(unsigned idx,unsigned maxparts,unsigned copy)
{
if (!group.get())
return 0;
unsigned nn;
unsigned dn;
mspec.calcPartLocation (idx,maxparts,copy, group->ordinality(), nn, dn);
return dn;
}
void serializeTree(IPropertyTree *pt,unsigned flags)
{
mspec.toProp(pt);
if (group&&(((flags&IFDSF_EXCLUDE_GROUPS)==0)||name.isEmpty())) {
StringBuffer gs;
group->getText(gs);
pt->setProp("Group",gs.str());
}
if (!name.isEmpty()&&((flags&IFDSF_EXCLUDE_CLUSTERNAMES)==0))
pt->setProp("@name",name);
}
ClusterPartDiskMapSpec &queryPartDiskMapping()
{
return mspec;
}
void setGroupName(const char *_name)
{
name.set(_name);
name.toLowerCase();
}
void setGroup(IGroup *_group)
{
group.set(_group);
}
IGroup *queryGroup()
{
return group;
}
void getBaseDir(StringBuffer &basedir,DFD_OS os)
{
if (mspec.defaultBaseDir.isEmpty()) // assume current platform's default
basedir.append(queryBaseDirectory(grp_unknown, 0, os));
else
basedir.append(mspec.defaultBaseDir);
}
void getReplicateDir(StringBuffer &basedir,DFD_OS os)
{
if (mspec.defaultReplicateDir.isEmpty()) // assume current platform's default
basedir.append(queryBaseDirectory(grp_unknown, 1, os));
else
basedir.append(mspec.defaultReplicateDir);
}
StringBuffer &getClusterLabel(StringBuffer &ret)
{
return getGroupName(ret, NULL);
}
};
IClusterInfo *createClusterInfo(const char *name,
IGroup *grp,
const ClusterPartDiskMapSpec &mspec,
INamedGroupStore *resolver)
{
return new CClusterInfo(name,grp,mspec,resolver);
}
IClusterInfo *deserializeClusterInfo(MemoryBuffer &mb,
INamedGroupStore *resolver)
{
return new CClusterInfo(mb,resolver);
}
IClusterInfo *deserializeClusterInfo(IPropertyTree *pt,
INamedGroupStore *resolver,
unsigned flags)
{
return new CClusterInfo(pt,resolver,flags);
}
class CFileDescriptorBase: public CInterface
{
protected:
PointerArray parts; // of CPartDescriptor
public:
StringAttr tracename;
IArrayOf<IClusterInfo> clusters;
Owned<IPropertyTree> attr;
StringAttr directory;
StringAttr partmask;
virtual unsigned numParts() = 0; // number of parts
virtual unsigned numCopies(unsigned partnum) = 0; // number of copies
virtual INode *doQueryNode(unsigned partidx, unsigned copy, unsigned rn) = 0; // query machine node
virtual unsigned queryDrive(unsigned partidx, unsigned copy) = 0; // query drive
virtual StringBuffer &getPartTail(StringBuffer &name,unsigned idx) = 0;
virtual StringBuffer &getPartDirectory(StringBuffer &name,unsigned idx,unsigned copy = 0) = 0; // get filename dir
virtual void serializePart(MemoryBuffer &mb,unsigned idx) = 0;
virtual const char *queryDefaultDir() = 0;
virtual IFileDescriptor &querySelf() = 0;
virtual unsigned copyClusterNum(unsigned partidx, unsigned copy,unsigned *replicate=NULL) = 0;
};
class CPartDescriptor : implements IPartDescriptor
{
protected: friend class CFileDescriptor;
StringAttr overridename; // this may be a multi path - may or not be relative to directory
// if not set use parent mask (and is *not* multi in this case)
bool ismulti; // only set if overridename set (otherwise false)
CFileDescriptorBase &parent; // this is for the cluster *not* for the entire file
unsigned partIndex;
Owned<IPropertyTree> props;
public:
virtual void Link(void) const
{
parent.Link();
}
virtual bool Release(void) const
{
return parent.Release();
}
CPartDescriptor(CFileDescriptorBase &_parent,unsigned idx,IPropertyTree *pt)
: parent(_parent)
{
partIndex = idx;
ismulti = false;
if (!isEmptyPTree(pt)) {
if (pt->getPropInt("@num",idx+1)-1!=idx)
WARNLOG("CPartDescriptor part index mismatch");
overridename.set(pt->queryProp("@name"));
if (overridename.isEmpty())
overridename.clear();
else
ismulti = ::isMulti(overridename);
props.setown(createPTreeFromIPT(pt));
//props->removeProp("@num"); // keep these for legacy
//props->removeProp("@name");
props->removeProp("@node");
}
else
props.setown(createPTree("Part"));
}
void set(unsigned idx, const char *_tail, IPropertyTree *pt)
{
partIndex = idx;
setOverrideName(_tail);
props.setown(pt?createPTreeFromIPT(pt):createPTree("Part"));
}
CPartDescriptor(CFileDescriptorBase &_parent, unsigned idx, MemoryBuffer &mb)
: parent(_parent)
{
partIndex = idx;
mb.read(overridename);
if (overridename.isEmpty()) // shouldn't really need this
overridename.clear();
ismulti = ::isMulti(overridename);
props.setown(createPTree(mb));
}
unsigned queryPartIndex()
{
return partIndex;
}
unsigned numCopies()
{
return parent.numCopies(partIndex);
}
virtual INode *queryNode(unsigned copy)
{
return parent.doQueryNode(partIndex,copy,(props&&props->hasProp("@rn"))?props->getPropInt("@rn"):(unsigned)-1);
}
virtual unsigned queryDrive(unsigned copy)
{
return parent.queryDrive(partIndex,copy);
}
INode *getNode(unsigned copy=0)
{
return LINK(queryNode(copy));
}
IPropertyTree &queryProperties()
{
return *props;
}
IPropertyTree *getProperties()
{
return props.get();
}
bool getCrc(unsigned &crc)
{
return getCrcFromPartProps(*parent.attr,*props,crc);
}
IFileDescriptor &queryOwner()
{
return parent.querySelf();
}
RemoteFilename &getFilename(unsigned copy, RemoteFilename &rfn)
{
if (ismulti) {
RemoteMultiFilename rmfn;
getMultiFilename(copy, rmfn);
if (rmfn.ordinality()==1) {
rfn.set(rmfn.item(0));
return rfn;
}
throw MakeStringException(-1,"Remote Filename: Cannot resolve single part from wild/multi filename");
}
StringBuffer fullpath;
getPath(fullpath,copy);
rfn.setPath(queryNode(copy)->endpoint(),fullpath.str());
return rfn;
}
StringBuffer &getPath(StringBuffer &path,unsigned copy)
{
StringBuffer tail;
getTail(tail);
if (!tail.length()||!isPathSepChar(tail.charAt(0))) {
getDirectory(path,copy);
addPathSepChar(path);
}
path.append(tail);
return path;
}
StringBuffer &getTail(StringBuffer &name)
{
return parent.getPartTail(name,partIndex);
}
StringBuffer &getDirectory(StringBuffer &dir,unsigned copy)
{
return parent.getPartDirectory(dir,partIndex,copy);
}
bool isMulti()
{
return ismulti;
}
RemoteMultiFilename &getMultiFilename(unsigned copy, RemoteMultiFilename &rmfn)
{
if (ismulti) {
rmfn.setEp(queryNode(copy)->endpoint());
StringBuffer dir;
parent.getPartDirectory(dir,partIndex,copy);
StringBuffer tmp1;
StringBuffer tmp2;
splitDirMultiTail(overridename,tmp1,tmp2);
rmfn.append(tmp2, dir);
}
else {
RemoteFilename rfn;
getFilename(copy,rfn);
rmfn.append(rfn);
}
return rmfn;
}
void subserialize(MemoryBuffer &mb)
{
mb.append(overridename);
props->serialize(mb);
}
bool subserializeTree(IPropertyTree *pt)
{
bool ret = false;
if (props) {
Owned<IAttributeIterator> attriter = props->getAttributes();
ForEach(*attriter) {
const char *an = attriter->queryName();
if ((stricmp(an,"@num")!=0)&&(stricmp(an,"@name")!=0)) {
pt->setProp(an,attriter->queryValue());
ret = true;
}
}
Owned<IPropertyTreeIterator> iter = props->getElements("*");
ForEach(*iter) {
ret = true;
pt->addPropTree(iter->query().queryName(),createPTreeFromIPT(&iter->query()));
}
}
if (!overridename.isEmpty()) {
pt->setProp("@name",overridename);
ret = true;
}
if (ret)
pt->setPropInt("@num",partIndex+1);
if ((partIndex==0)&&(parent.numParts()==1)) { // more legacy
SocketEndpoint ep = queryNode(0)->endpoint();
StringBuffer tmp;
if (!ep.isNull())
pt->setProp("@node",ep.getUrlStr(tmp).str());
if (overridename.isEmpty()&&!parent.partmask.isEmpty()) {
expandMask(tmp.clear(), parent.partmask, 0, 1);
pt->setProp("@name",tmp.str());
}
}
return ret;
}
void setOverrideName(const char *_tail)
{
if (!_tail||!*_tail)
overridename.clear();
else
overridename.set(_tail);
ismulti = ::isMulti(_tail);
}
const char *queryOverrideName()
{
if (overridename.isEmpty())
return NULL;
return overridename;
}
void serialize(MemoryBuffer &mb)
{
parent.serializePart(mb,partIndex);
}
unsigned copyClusterNum(unsigned copy,unsigned *replicate=NULL)
{
return parent.copyClusterNum(partIndex,copy,replicate);
}
IReplicatedFile *getReplicatedFile()
{
IReplicatedFile *ret = createReplicatedFile();
RemoteFilenameArray &copies = ret->queryCopies();
unsigned nc = numCopies();
for (unsigned copy=0;copy<nc;copy++) {
RemoteFilename rfn;
copies.append(getFilename(copy,rfn));
}
return ret;
}
};
// --------------------------------------------------------
class CPartDescriptorArrayIterator : public CArrayIteratorOf<IPartDescriptor, IPartDescriptorIterator>
{
public:
CPartDescriptorArrayIterator() : CArrayIteratorOf<IPartDescriptor, IPartDescriptorIterator>(array) { }
CPartDescriptorArray array;
};
void getClusterInfo(IPropertyTree &pt, INamedGroupStore *resolver, unsigned flags, IArrayOf<IClusterInfo> &clusters)
{
unsigned nc = pt.getPropInt("@numclusters");
if (!nc) { // legacy format
unsigned np = pt.getPropInt("@numparts");
StringArray groups;
getFileGroups(&pt,groups);
unsigned gi = 0;
do {
Owned<IGroup> cgroup;
const char *grp = (gi<groups.ordinality())?groups.item(gi):NULL;
if (grp&&resolver)
cgroup.setown(resolver->lookup(grp));
// get nodes from parts if complete (and group 0)
if (gi==0) { // don't assume lookup name correct!
SocketEndpoint *eps = (SocketEndpoint *)calloc(np?np:1,sizeof(SocketEndpoint));
MemoryBuffer mb;
Owned<IPropertyTreeIterator> piter;
if (pt.getPropBin("Parts",mb))
piter.setown(deserializePartAttrIterator(mb));
else
piter.setown(pt.getElements("Part"));
ForEach(*piter) {
IPropertyTree &cpt = piter->query();
unsigned num = cpt.getPropInt("@num");
if (num>np) {
eps = (SocketEndpoint *)checked_realloc(eps,num*sizeof(SocketEndpoint),np*sizeof(SocketEndpoint),-21);
memset(eps+np,0,(num-np)*sizeof(SocketEndpoint));
np = num;
}
const char *node = cpt.queryProp("@node");
if (node&&*node)
eps[num-1].set(node);
}
unsigned i=0;
for (i=0;i<np;i++)
if (eps[i].isNull())
break;
if (i==np) {
Owned<IGroup> ngrp = createIGroup(np,eps);
if (!cgroup.get()||(ngrp->compare(cgroup)!=GRbasesubset))
cgroup.setown(ngrp.getClear());
}
free(eps);
}
ClusterPartDiskMapSpec mspec;
IClusterInfo *cluster = createClusterInfo(grp,cgroup,mspec,resolver);
clusters.append(*cluster);
gi++;
} while (gi<groups.ordinality());
}
else {
Owned<IPropertyTreeIterator> iter = pt.getElements("Cluster");
ForEach(*iter)
clusters.append(*deserializeClusterInfo(&iter->query(),resolver,flags));
}
}
class CFileDescriptor: public CFileDescriptorBase, implements ISuperFileDescriptor
{
SocketEndpointArray *pending; // for constructing cluster group
bool setupdone;
byte version;
IFileDescriptor &querySelf()
{
return *this;
}
void openPending()
{
if (!pending) {
pending = new SocketEndpointArray;
if (setupdone)
throw MakeStringException(-1,"IFileDescriptor - setup already done");
setupdone = true;
ClusterPartDiskMapSpec mspec;
clusters.append(*createClusterInfo(NULL,NULL,mspec));
}
}
void doClosePending()
{
// first sort out cluster
unsigned np = parts.ordinality();
unsigned n = pending->ordinality();
assertex(clusters.ordinality());
assertex(np>=n);
if (n==0) {
clusters.remove(clusters.ordinality()-1);
WARNLOG("CFileDescriptor: removing empty cluster");
}
else {
unsigned w;
for (w=1;w<n;w++) {
unsigned i;
for (i=w;i<n;i++)
if (!pending->item(i).equals(pending->item(i%w)))
break;
if (i==n)
break;
}
for (unsigned i=n;i>w;)
pending->remove(--i);
Owned<IGroup> newgrp = createIGroup(*pending);
clusters.item(clusters.ordinality()-1).setGroup(newgrp);
}
delete pending;
pending = NULL;
if ((n==1)&&(isSpecialPath(part(0)->overridename)))
return;
// now look for a directory
// this is a bit longwinded!
// expand all tails
StringBuffer tmp;
if (!directory.isEmpty()) {
StringBuffer fp;
ForEachItemIn(i,parts) {
CPartDescriptor *pt = part(i);