^cHdZddlZddlZddlmZddlmZddlmZm Z m Z ddZ dd Z Gd d Z e Ze jd d e Z ddZdS)a; Algorithm works in the following way. You have two repository: local and remote. They both contains a DAG of changelists. The goal of the discovery protocol is to find one set of node *common*, the set of nodes shared by local and remote. One of the issue with the original protocol was latency, it could potentially require lots of roundtrips to discover that the local repo was a subset of remote (which is a very common case, you usually have few changes compared to upstream, while upstream probably had lots of development). The new protocol only requires one interface for the remote repo: `known()`, which given a set of changelists tells you if they are present in the DAG. The algorithm then works as follow: - We will be using three sets, `common`, `missing`, `unknown`. Originally all nodes are in `unknown`. - Take a sample from `unknown`, call `remote.known(sample)` - For each node that remote knows, move it and all its ancestors to `common` - For each node that remote doesn't know, move it and all its descendants to `missing` - Iterate until `unknown` is empty There are a couple optimizations, first is instead of starting with a random sample of missing, start by sending all heads, in the case where the local repo is a subset, you computed the answer in one round trip. Then you can do something similar to the bisecting strategy used when finding faulty changesets. Instead of random samples, you can try picking nodes that will maximize the number of nodes that will be classified with it (since all ancestors or descendants will be marked as well). N)_nullrev)errorpolicyutilci}tj|}t}d}|r|} | |vr|| d} | |kr|dz}| |kr,|| |rt ||krdS|| || D]A} | tkr4|r| |vr.|| | dz|| B|dSdS)a[update an existing sample to match the expected size The sample is updated with revs exponentially distant from each head of the set. (H~1, H~2, H~4, H~8, etc). If a target size is specified, the sampling will stop once this size is reached. Otherwise sampling will happen until roots of the set are reached. :revs: set of revs we want to discover (if None, assume the whole dag) :heads: set of DAG head revs :sample: a sample to update :parentfn: a callable to resolve parents for a revision :quicksamplesize: optional target size of the samplerN) collectionsdequesetpopleft setdefaultaddlenrappend) revsheadssampleparentfnquicksamplesizedistvisitseenfactorcurrdps 8/usr/lib/python3/dist-packages/mercurial/setdiscovery.py _updatesampler!8s& D  e $ $E 55D F  }} 4<<  OOD! $ $ v:: aKF ;; JJt    CKK?$B$B $  AG||T|Q$YY1q5))) Q!      Tct||kr|St|}|r"ttj||S|t|d|S)zreturn a random subset of sample of at most desiredlen item. If randomize is False, though, a deterministic subset is returned. This is meant for integration tests. N)rlistrrandomrsort)r desiredlen randomizes r _limitsampler)^sk  6{{j   &\\F66=44555 KKMMM vkzk" # ##r"creZdZdZddZdZdZdZdZdZ e d Z d Z d Z d Zd ZdZdZdS)partialdiscoveryaban object representing ongoing discovery Feed with data from the remote repository, this object keep track of the current set of changeset in various states: - common: revs also known remotely - undecided: revs we don't have information on yet - missing: revs missing remotely (all tracked revisions are known locally) Tc||_||_|j|_d|_t |_d|_||_ ||_ dSN) _repo _targetheads changelogincrementalmissingrevs_common _undecidedrmissing _childrenmap _respectsizer()selfrepo targetheads respectsizer(s r __init__zpartialdiscovery.__init__ysS '~<<>> uu  '"r"c|j||j!|j|jdSdS)zregister nodes known as commonN)r2addbasesr3removeancestorsfrom)r7commonss r addcommonszpartialdiscovery.addcommonssF g&&& ? & L , ,T_ = = = = = ' &r"c|jd||j}|r6|j||j|dSdS)zregister some nodes as missings%ld::%ldN)r.r undecidedr4updatedifference_update)r7missings newmissings r addmissingszpartialdiscovery.addmissingss_Z__[(DNKK  9 L   + + + N , ,Z 8 8 8 8 8 9 9r"ct}t}|D]2\}}|r||||3|r|||r||dSdS)z*consume an iterable of (rev, known) tuplesN)rrr@rG)r7rcommonr4revknowns r addinfozpartialdiscovery.addinfos%%  ! !JC ! 3 C     $ OOF # # #  &   W % % % % % & &r"c4|jS)z6return True is we have any clue about the remote state)r2hasbasesr7s r hasinfozpartialdiscovery.hasinfos|$$&&&r"c$|jduo|j S)z1True if all the necessary data have been gatheredN)r3rOs r iscompletezpartialdiscovery.iscompletesd*B4?/BBr"c|j|jSt|j|j|_|jSr-)r3rr2missingancestorsr/rOs r rBzpartialdiscovery.undecideds< ? &? "dl;;D|jjjjfd}|S)Nc*|ddS)N)rgetrevs r getparentsz3partialdiscovery._parentsgetter..getparentss6!99QqS> !r")r.r0index __getitem__)r7rar`s @r _parentsgetterzpartialdiscovery._parentsgetters4%+7 " " " " "r"c8|j |jjSix|_}|}|j}t |D]M}g||<||D]:}|t kr||}|||;N|jSr-)r5rcrdrBsortedrgetr)r7children parentrevsrrJprevcs r _childrengetterz partialdiscovery._childrengetters   ( $0 0 (*)H((** ~$<< " "CHSM" 3 " "7??LL&&=HHSMMM  " ##r"cH|j}t||krt|St|jd|}t||krt |||jStd||| ||S)atakes a quick sample of size It is meant for initial sampling and focuses on querying heads and close ancestors of heads. :headrevs: set of head revisions in local DAG to consider :size: the maximum size of the sample heads(%ld)r(N)r) rBrr$rr.rr)r(r!rd)r7headrevssizerrs r takequicksamplez partialdiscovery.takequicksamples~ t99  :: TZ__]D99:: v;;$  GGG G (FD$7$7$9$94     r"c |j}t||krt|S|j}t |d|}|}|}t||||t |d|}| } t|||| |sJ|j s8t|tt|t|}t|||j}t||kr|t|z } t||z } |jr)|t!j| | n1| || d| |S)Nrns roots(%ld)ro)rBrr$r.rrrdcopyr!rlr6maxminr)r(rCr%rr&) r7rprqrr8rri revsheads revsroots childrenrevsmoretakefroms r takefullsamplezpartialdiscovery.takefullsamples~ t99  :: zTYY}d3344((** KKMM dIvz::: -6677 ++-- dIv|<<<   BtSYY@@AADfddnEEE v;;  #f++%DD6M**H~ / fmHd;;<<<<  huuo... r"NT)__name__ __module__ __qualname____doc__r;r@rGrLrPrRpropertyrBrVrYrdrlrrr|r^r"r r+r+ms  ####>>> 999 & & &'''CCCX    ))) $$$<*r"r+ discoveryPartialDiscovery)memberdefaultc()t|dd}|d|d<tj}d}|j} | j(| j)|)fd|D} nd| D} |dd} | dd } | dd } | r7|j r t| | }t|}n| }| d |d z }|5}|d i}||dxxt!|z cc<|dd(fd|Di}dddn #1swxYwY||}}|d |d<| t&kr"|| jgkr | jgd|fS| jgdgfSnV|5}|d i}dddn #1swxYwY|}|t-dg}|D]B}|| jkr |)|.#t0j$rY?wxYw| rt!|t!|kr| d|d|fSt!|t!| krDt5|r5|t-d(fd| D}|d|fS|jj}|dd}|o|j }|dd}|p|j }|dd}| jjrt>}nt@}||| ||}| r8|!||"tG||| }|$t-dt-d}|%s|s|&rX|r#|t-dn| d |j'} | }!|rtQ| |z} n| d!|j)} | }!| | |!}|d z }|*||+}"| d"||"d#t!|fzt|}|5}||dxxt!|z cc<|dd(fd$|Di}dddn #1swxYwYd}|"tG|||%|,}#tj|z }$|-| d%||$fzd&}%t]|#t]|z }&|/d'|%t!|#t!|&||$|||d<|#s\|| jgkrP|r!t1j0t-d(|1t-d)| jhd|fS|| jgk}'(fd*|#D}#|#|'|fS)+aReturn a tuple (common, anyincoming, remoteheads) used to identify missing nodes from or in remote. The audit argument is an optional dictionnary that a caller can pass. it will be updated with extra data about the discovery, this is useful for debug. sdevelsdiscovery.grow-sample.rateNrs total-queriesc&g|] }|Sr^r^).0nclrevs r z#findcommonheads..8s!222EE!HH222r"c(g|]}|tk |Sr^r)rrJs r rz#findcommonheads..:sCCCCC7NNCNNNr"sdiscovery.exchange-headssdiscovery.sample-size.initialsdiscovery.sample-sizesquery 1; heads rsheadssknownsnodesc&g|] }|Sr^r^rr_clnodes r rz#findcommonheads..!999Qvvayy999r"stotal-roundtripsTFssearching for changes sall remote heads known locally s$all local changesets known remotely c&g|] }|Sr^r^rs r rz#findcommonheads..s!9991VVAYY999r"sdiscovery.grow-samplesdiscovery.grow-sample.dynamicsdiscovery.randomizeros searchingsqueries)unitssampling from both directions staking initial sample staking quick initial sample s2query %i; still undecided: %i, sample size is: %i rBc&g|] }|Sr^r^rs r rz#findcommonheads..rr"s%d total queries in %.4fs sDfound %d common and %d unknown server heads, %d roundtrips in %.4fs s discoverysrepository is unrelateds!warning: repository is unrelated c&h|] }|Sr^r^rs r z"findcommonheads.. s! ( ( (AffQii ( ( (r")2floatconfigr timerr0noderJrp configbool configintlimitedargumentsr)r$debugcommandexecutor callcommandrresulttiprevrnullidstatusrrr LookupErrorallnoteuirbrust_ext_compatr+pure_partialdiscoveryr@rLzip makeprogressrRrPr|intrrrCrVrYcompleterlogAbortwarn)*rlocalremoteabortwhenunrelated ancestorsofaudit samplegrowthstart roundtripsclownheadsinitial_head_exchangeinitialsamplesizefullsamplesizerefheadsfknown srvheadhashesyesno knownsrvheadsr ownheadhashesr grow_sampledynamic_samplehard_limit_sampler(pddiscofullprogress samplefunc targetsizerVrelapsedmsgr4 anyincomingrrs* @@r findcommonheadsrs 8-JKKLLL "# JLLEJ B WF FE2222k222CC2;;==CCCMM(4OPP X/OPP\\(,DEENh"(  " !(,=>>F&\\FFF $%%%a  # # % % ]]8R00F &'''3v;;6''']]9999&999F                &}} u  )*E% & 99;;' ! ! ++ {D-77I;r) ) "  # # % % 1]]8R00F 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  IIa*++,,,M 29      t - - - -     H  6 }  ]!3!3 3 3 HH8 9 9 9 %6 6 v;;#h-- ' 'CJJ ' GGA>?? @ @ @9999999M $ 5 5 $J*X'?@@K=f&="=KZ*JKKN+Fv/FG h(>??I x#  " Buh 1Y G G GE* ''' c&%(()))$ $DqQz]]CCH  )*  +5==?? + 5<==>>>>3444-J'J D!$^l%B!C!C HH5 6 6 6.J*JHj11a  ###   B5-s6{{; <   f  # # % %  &'''3v;;6'''MM9999&999 fhh                 c&%(()))S  )*V   FjllU"G HH +z7.C CDDD %&kkC ...GFF<c&kk3w<<WMMM %/!"   m {22  >+a :;;<< < GGA;<< = = = YK     BI;.K ( ( ( ( ( ( (F ; --sJ"AFF F H99H=H= J,,J>=J>3AWWW)rr})TNN)rr r%i18nrrrrrr r!r)r+r importrustrr^r"r rs1!!H # # # # L $ $ $ $ddddddddN)$6$*4D  s.s.s.s.s.s.r"