pipeline.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. package core
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "runtime/debug"
  11. "sort"
  12. "strings"
  13. "time"
  14. "github.com/pkg/errors"
  15. "gopkg.in/src-d/go-git.v4"
  16. "gopkg.in/src-d/go-git.v4/plumbing"
  17. "gopkg.in/src-d/go-git.v4/plumbing/object"
  18. "gopkg.in/src-d/go-git.v4/plumbing/storer"
  19. "gopkg.in/src-d/hercules.v10/internal/pb"
  20. "gopkg.in/src-d/hercules.v10/internal/toposort"
  21. )
  22. // ConfigurationOptionType represents the possible types of a ConfigurationOption's value.
  23. type ConfigurationOptionType int
  24. const (
  25. // BoolConfigurationOption reflects the boolean value type.
  26. BoolConfigurationOption ConfigurationOptionType = iota
  27. // IntConfigurationOption reflects the integer value type.
  28. IntConfigurationOption
  29. // StringConfigurationOption reflects the string value type.
  30. StringConfigurationOption
  31. // FloatConfigurationOption reflects a floating point value type.
  32. FloatConfigurationOption
  33. // StringsConfigurationOption reflects the array of strings value type.
  34. StringsConfigurationOption
  35. // PathConfigurationOption reflects the file system path value type.
  36. PathConfigurationOption
  37. )
  38. // String() returns an empty string for the boolean type, "int" for integers and "string" for
  39. // strings. It is used in the command line interface to show the argument's type.
  40. func (opt ConfigurationOptionType) String() string {
  41. switch opt {
  42. case BoolConfigurationOption:
  43. return ""
  44. case IntConfigurationOption:
  45. return "int"
  46. case StringConfigurationOption:
  47. return "string"
  48. case FloatConfigurationOption:
  49. return "float"
  50. case StringsConfigurationOption:
  51. return "string"
  52. case PathConfigurationOption:
  53. return "path"
  54. }
  55. log.Panicf("Invalid ConfigurationOptionType value %d", opt)
  56. return ""
  57. }
  58. // ConfigurationOption allows for the unified, retrospective way to setup PipelineItem-s.
  59. type ConfigurationOption struct {
  60. // Name identifies the configuration option in facts.
  61. Name string
  62. // Description represents the help text about the configuration option.
  63. Description string
  64. // Flag corresponds to the CLI token with "--" prepended.
  65. Flag string
  66. // Type specifies the kind of the configuration option's value.
  67. Type ConfigurationOptionType
  68. // Default is the initial value of the configuration option.
  69. Default interface{}
  70. }
  71. // FormatDefault converts the default value of ConfigurationOption to string.
  72. // Used in the command line interface to show the argument's default value.
  73. func (opt ConfigurationOption) FormatDefault() string {
  74. if opt.Type == StringsConfigurationOption {
  75. return fmt.Sprintf("\"%s\"", strings.Join(opt.Default.([]string), ","))
  76. }
  77. if opt.Type != StringConfigurationOption {
  78. return fmt.Sprint(opt.Default)
  79. }
  80. return fmt.Sprintf("\"%s\"", opt.Default)
  81. }
  82. // PipelineItem is the interface for all the units in the Git commits analysis pipeline.
  83. type PipelineItem interface {
  84. // Name returns the name of the analysis.
  85. Name() string
  86. // Provides returns the list of keys of reusable calculated entities.
  87. // Other items may depend on them.
  88. Provides() []string
  89. // Requires returns the list of keys of needed entities which must be supplied in Consume().
  90. Requires() []string
  91. // ListConfigurationOptions returns the list of available options which can be consumed by Configure().
  92. ListConfigurationOptions() []ConfigurationOption
  93. // Configure performs the initial setup of the object by applying parameters from facts.
  94. // It allows to create PipelineItems in a universal way.
  95. Configure(facts map[string]interface{}) error
  96. // Initialize prepares and resets the item. Consume() requires Initialize()
  97. // to be called at least once beforehand.
  98. Initialize(*git.Repository) error
  99. // Consume processes the next commit.
  100. // deps contains the required entities which match Depends(). Besides, it always includes
  101. // DependencyCommit and DependencyIndex.
  102. // Returns the calculated entities which match Provides().
  103. Consume(deps map[string]interface{}) (map[string]interface{}, error)
  104. // Fork clones the item the requested number of times. The data links between the clones
  105. // are up to the implementation. Needed to handle Git branches. See also Merge().
  106. // Returns a slice with `n` fresh clones. In other words, it does not include the original item.
  107. Fork(n int) []PipelineItem
  108. // Merge combines several branches together. Each is supposed to have been created with Fork().
  109. // The result is stored in the called item, thus this function returns nothing.
  110. // Merge() must update all the branches, not only self. When several branches merge, some of
  111. // them may continue to live, hence this requirement.
  112. Merge(branches []PipelineItem)
  113. }
  114. // FeaturedPipelineItem enables switching the automatic insertion of pipeline items on or off.
  115. type FeaturedPipelineItem interface {
  116. PipelineItem
  117. // Features returns the list of names which enable this item to be automatically inserted
  118. // in Pipeline.DeployItem().
  119. Features() []string
  120. }
  121. // DisposablePipelineItem enables resources cleanup after finishing running the pipeline.
  122. type DisposablePipelineItem interface {
  123. PipelineItem
  124. // Dispose frees any previously allocated unmanaged resources. No Consume() calls are possible
  125. // afterwards. The item needs to be Initialize()-d again.
  126. // This method is invoked once for each item in the pipeline, **in a single forked instance**.
  127. // Thus it is the responsibility of the item's programmer to deal with forks and merges, if
  128. // necessary.
  129. Dispose()
  130. }
  131. // LeafPipelineItem corresponds to the top level pipeline items which produce the end results.
  132. type LeafPipelineItem interface {
  133. PipelineItem
  134. // Flag returns the cmdline switch to run the analysis. Should be dash-lower-case
  135. // without the leading dashes.
  136. Flag() string
  137. // Description returns the text which explains what the analysis is doing.
  138. // Should start with a capital letter and end with a dot.
  139. Description() string
  140. // Finalize returns the result of the analysis.
  141. Finalize() interface{}
  142. // Serialize encodes the object returned by Finalize() to YAML or Protocol Buffers.
  143. Serialize(result interface{}, binary bool, writer io.Writer) error
  144. }
  145. // ResultMergeablePipelineItem specifies the methods to combine several analysis results together.
  146. type ResultMergeablePipelineItem interface {
  147. LeafPipelineItem
  148. // Deserialize loads the result from Protocol Buffers blob.
  149. Deserialize(pbmessage []byte) (interface{}, error)
  150. // MergeResults joins two results together. Common-s are specified as the global state.
  151. MergeResults(r1, r2 interface{}, c1, c2 *CommonAnalysisResult) interface{}
  152. }
  153. // HibernateablePipelineItem is the interface to allow pipeline items to be frozen (compacted, unloaded)
  154. // while they are not needed in the hosting branch.
  155. type HibernateablePipelineItem interface {
  156. PipelineItem
  157. // Hibernate signals that the item is temporarily not needed and it's memory can be optimized.
  158. Hibernate() error
  159. // Boot signals that the item is needed again and must be de-hibernate-d.
  160. Boot() error
  161. }
  162. // CommonAnalysisResult holds the information which is always extracted at Pipeline.Run().
  163. type CommonAnalysisResult struct {
  164. // BeginTime is the time of the first commit in the analysed sequence.
  165. BeginTime int64
  166. // EndTime is the time of the last commit in the analysed sequence.
  167. EndTime int64
  168. // CommitsNumber is the number of commits in the analysed sequence.
  169. CommitsNumber int
  170. // RunTime is the duration of Pipeline.Run().
  171. RunTime time.Duration
  172. // RunTimePerItem is the time elapsed by each PipelineItem.
  173. RunTimePerItem map[string]float64
  174. }
  175. // Copy produces a deep clone of the object.
  176. func (car CommonAnalysisResult) Copy() CommonAnalysisResult {
  177. result := car
  178. result.RunTimePerItem = map[string]float64{}
  179. for key, val := range car.RunTimePerItem {
  180. result.RunTimePerItem[key] = val
  181. }
  182. return result
  183. }
  184. // BeginTimeAsTime converts the UNIX timestamp of the beginning to Go time.
  185. func (car *CommonAnalysisResult) BeginTimeAsTime() time.Time {
  186. return time.Unix(car.BeginTime, 0)
  187. }
  188. // EndTimeAsTime converts the UNIX timestamp of the ending to Go time.
  189. func (car *CommonAnalysisResult) EndTimeAsTime() time.Time {
  190. return time.Unix(car.EndTime, 0)
  191. }
  192. // Merge combines the CommonAnalysisResult with an other one.
  193. // We choose the earlier BeginTime, the later EndTime, sum the number of commits and the
  194. // elapsed run times.
  195. func (car *CommonAnalysisResult) Merge(other *CommonAnalysisResult) {
  196. if car.EndTime == 0 || other.BeginTime == 0 {
  197. panic("Merging with an uninitialized CommonAnalysisResult")
  198. }
  199. if other.BeginTime < car.BeginTime {
  200. car.BeginTime = other.BeginTime
  201. }
  202. if other.EndTime > car.EndTime {
  203. car.EndTime = other.EndTime
  204. }
  205. car.CommitsNumber += other.CommitsNumber
  206. car.RunTime += other.RunTime
  207. for key, val := range other.RunTimePerItem {
  208. car.RunTimePerItem[key] += val
  209. }
  210. }
  211. // FillMetadata copies the data to a Protobuf message.
  212. func (car *CommonAnalysisResult) FillMetadata(meta *pb.Metadata) *pb.Metadata {
  213. meta.BeginUnixTime = car.BeginTime
  214. meta.EndUnixTime = car.EndTime
  215. meta.Commits = int32(car.CommitsNumber)
  216. meta.RunTime = car.RunTime.Nanoseconds() / 1e6
  217. meta.RunTimePerItem = car.RunTimePerItem
  218. return meta
  219. }
  220. // Metadata is defined in internal/pb/pb.pb.go - header of the binary file.
  221. type Metadata = pb.Metadata
  222. // MetadataToCommonAnalysisResult copies the data from a Protobuf message.
  223. func MetadataToCommonAnalysisResult(meta *Metadata) *CommonAnalysisResult {
  224. return &CommonAnalysisResult{
  225. BeginTime: meta.BeginUnixTime,
  226. EndTime: meta.EndUnixTime,
  227. CommitsNumber: int(meta.Commits),
  228. RunTime: time.Duration(meta.RunTime * 1e6),
  229. RunTimePerItem: meta.RunTimePerItem,
  230. }
  231. }
  232. // Pipeline is the core Hercules entity which carries several PipelineItems and executes them.
  233. // See the extended example of how a Pipeline works in doc.go
  234. type Pipeline struct {
  235. // OnProgress is the callback which is invoked in Analyse() to output it's
  236. // progress. The first argument is the number of complete steps, the
  237. // second is the total number of steps and the third is some description of the current action.
  238. OnProgress func(int, int, string)
  239. // HibernationDistance is the minimum number of actions between two sequential usages of
  240. // a branch to activate the hibernation optimization (cpu-memory trade-off). 0 disables.
  241. HibernationDistance int
  242. // DryRun indicates whether the items are not executed.
  243. DryRun bool
  244. // DumpPlan indicates whether to print the execution plan to stderr.
  245. DumpPlan bool
  246. // PrintActions indicates whether to print the taken actions during the execution.
  247. PrintActions bool
  248. // Repository points to the analysed Git repository struct from go-git.
  249. repository *git.Repository
  250. // Items are the registered building blocks in the pipeline. The order defines the
  251. // execution sequence.
  252. items []PipelineItem
  253. // The collection of parameters to create items.
  254. facts map[string]interface{}
  255. // Feature flags which enable the corresponding items.
  256. features map[string]bool
  257. // The logger for printing output.
  258. l Logger
  259. }
  260. const (
  261. // ConfigPipelineDAGPath is the name of the Pipeline configuration option (Pipeline.Initialize())
  262. // which enables saving the items DAG to the specified file.
  263. ConfigPipelineDAGPath = "Pipeline.DAGPath"
  264. // ConfigPipelineDryRun is the name of the Pipeline configuration option (Pipeline.Initialize())
  265. // which disables Configure() and Initialize() invocation on each PipelineItem during the
  266. // Pipeline initialization.
  267. // Subsequent Run() calls are going to fail. Useful with ConfigPipelineDAGPath=true.
  268. ConfigPipelineDryRun = "Pipeline.DryRun"
  269. // ConfigPipelineCommits is the name of the Pipeline configuration option (Pipeline.Initialize())
  270. // which allows to specify the custom commit sequence. By default, Pipeline.Commits() is used.
  271. ConfigPipelineCommits = "Pipeline.Commits"
  272. // ConfigPipelineDumpPlan is the name of the Pipeline configuration option (Pipeline.Initialize())
  273. // which outputs the execution plan to stderr.
  274. ConfigPipelineDumpPlan = "Pipeline.DumpPlan"
  275. // ConfigPipelineHibernationDistance is the name of the Pipeline configuration option (Pipeline.Initialize())
  276. // which is the minimum number of actions between two sequential usages of
  277. // a branch to activate the hibernation optimization (cpu-memory trade-off). 0 disables.
  278. ConfigPipelineHibernationDistance = "Pipeline.HibernationDistance"
  279. // ConfigPipelinePrintActions is the name of the Pipeline configuration option (Pipeline.Initialize())
  280. // which enables printing the taken actions of the execution plan to stderr.
  281. ConfigPipelinePrintActions = "Pipeline.PrintActions"
  282. // DependencyCommit is the name of one of the three items in `deps` supplied to PipelineItem.Consume()
  283. // which always exists. It corresponds to the currently analyzed commit.
  284. DependencyCommit = "commit"
  285. // DependencyIndex is the name of one of the three items in `deps` supplied to PipelineItem.Consume()
  286. // which always exists. It corresponds to the currently analyzed commit's index.
  287. DependencyIndex = "index"
  288. // DependencyIsMerge is the name of one of the three items in `deps` supplied to PipelineItem.Consume()
  289. // which always exists. It indicates whether the analyzed commit is a merge commit.
  290. // Checking the number of parents is not correct - we remove the back edges during the DAG simplification.
  291. DependencyIsMerge = "is_merge"
  292. // MessageFinalize is the status text reported before calling LeafPipelineItem.Finalize()-s.
  293. MessageFinalize = "finalize"
  294. )
  295. // NewPipeline initializes a new instance of Pipeline struct.
  296. func NewPipeline(repository *git.Repository) *Pipeline {
  297. return &Pipeline{
  298. repository: repository,
  299. items: []PipelineItem{},
  300. facts: map[string]interface{}{},
  301. features: map[string]bool{},
  302. l: NewLogger(),
  303. }
  304. }
  305. // GetFact returns the value of the fact with the specified name.
  306. func (pipeline *Pipeline) GetFact(name string) interface{} {
  307. return pipeline.facts[name]
  308. }
  309. // SetFact sets the value of the fact with the specified name.
  310. func (pipeline *Pipeline) SetFact(name string, value interface{}) {
  311. pipeline.facts[name] = value
  312. }
  313. // GetFeature returns the state of the feature with the specified name (enabled/disabled) and
  314. // whether it exists. See also: FeaturedPipelineItem.
  315. func (pipeline *Pipeline) GetFeature(name string) (bool, bool) {
  316. val, exists := pipeline.features[name]
  317. return val, exists
  318. }
  319. // SetFeature sets the value of the feature with the specified name.
  320. // See also: FeaturedPipelineItem.
  321. func (pipeline *Pipeline) SetFeature(name string) {
  322. pipeline.features[name] = true
  323. }
  324. // SetFeaturesFromFlags enables the features which were specified through the command line flags
  325. // which belong to the given PipelineItemRegistry instance.
  326. // See also: AddItem().
  327. func (pipeline *Pipeline) SetFeaturesFromFlags(registry ...*PipelineItemRegistry) {
  328. var ffr *PipelineItemRegistry
  329. if len(registry) == 0 {
  330. ffr = Registry
  331. } else if len(registry) == 1 {
  332. ffr = registry[0]
  333. } else {
  334. panic("Zero or one registry is allowed to be passed.")
  335. }
  336. for _, feature := range ffr.featureFlags.Flags {
  337. pipeline.SetFeature(feature)
  338. }
  339. }
  340. // DeployItem inserts a PipelineItem into the pipeline. It also recursively creates all of it's
  341. // dependencies (PipelineItem.Requires()). Returns the same item as specified in the arguments.
  342. func (pipeline *Pipeline) DeployItem(item PipelineItem) PipelineItem {
  343. fpi, ok := item.(FeaturedPipelineItem)
  344. if ok {
  345. for _, f := range fpi.Features() {
  346. pipeline.SetFeature(f)
  347. }
  348. }
  349. queue := []PipelineItem{}
  350. queue = append(queue, item)
  351. added := map[string]PipelineItem{}
  352. for _, item := range pipeline.items {
  353. added[item.Name()] = item
  354. }
  355. added[item.Name()] = item
  356. pipeline.AddItem(item)
  357. for len(queue) > 0 {
  358. head := queue[0]
  359. queue = queue[1:]
  360. for _, dep := range head.Requires() {
  361. for _, sibling := range Registry.Summon(dep) {
  362. if _, exists := added[sibling.Name()]; !exists {
  363. disabled := false
  364. // If this item supports features, check them against the activated in pipeline.features
  365. if fpi, matches := sibling.(FeaturedPipelineItem); matches {
  366. for _, feature := range fpi.Features() {
  367. if !pipeline.features[feature] {
  368. disabled = true
  369. break
  370. }
  371. }
  372. }
  373. if disabled {
  374. continue
  375. }
  376. added[sibling.Name()] = sibling
  377. queue = append(queue, sibling)
  378. pipeline.AddItem(sibling)
  379. }
  380. }
  381. }
  382. }
  383. return item
  384. }
  385. // AddItem inserts a PipelineItem into the pipeline. It does not check any dependencies.
  386. // See also: DeployItem().
  387. func (pipeline *Pipeline) AddItem(item PipelineItem) PipelineItem {
  388. pipeline.items = append(pipeline.items, item)
  389. return item
  390. }
  391. // RemoveItem deletes a PipelineItem from the pipeline. It leaves all the rest of the items intact.
  392. func (pipeline *Pipeline) RemoveItem(item PipelineItem) {
  393. for i, reg := range pipeline.items {
  394. if reg == item {
  395. pipeline.items = append(pipeline.items[:i], pipeline.items[i+1:]...)
  396. return
  397. }
  398. }
  399. }
  400. // Len returns the number of items in the pipeline.
  401. func (pipeline *Pipeline) Len() int {
  402. return len(pipeline.items)
  403. }
  404. // Commits returns the list of commits from the history similar to `git log` over the HEAD.
  405. // `firstParent` specifies whether to leave only the first parent after each merge
  406. // (`git log --first-parent`) - effectively decreasing the accuracy but increasing performance.
  407. func (pipeline *Pipeline) Commits(firstParent bool) ([]*object.Commit, error) {
  408. var result []*object.Commit
  409. repository := pipeline.repository
  410. head, err := repository.Head()
  411. if err != nil {
  412. if err == plumbing.ErrReferenceNotFound {
  413. refs, errr := repository.References()
  414. if errr != nil {
  415. return nil, errors.Wrap(errr, "unable to list the references")
  416. }
  417. refs.ForEach(func(ref *plumbing.Reference) error {
  418. if strings.HasPrefix(ref.Name().String(), "refs/heads/HEAD/") {
  419. head = ref
  420. return storer.ErrStop
  421. }
  422. return nil
  423. })
  424. }
  425. if head == nil && err != nil {
  426. return nil, errors.Wrap(err, "unable to collect the commit history")
  427. }
  428. }
  429. if firstParent {
  430. commit, err := repository.CommitObject(head.Hash())
  431. if err != nil {
  432. panic(err)
  433. }
  434. // the first parent matches the head
  435. for ; err != io.EOF; commit, err = commit.Parents().Next() {
  436. if err != nil {
  437. panic(err)
  438. }
  439. result = append(result, commit)
  440. }
  441. // reverse the order
  442. for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
  443. result[i], result[j] = result[j], result[i]
  444. }
  445. return result, nil
  446. }
  447. cit, err := repository.Log(&git.LogOptions{From: head.Hash()})
  448. if err != nil {
  449. return nil, errors.Wrap(err, "unable to collect the commit history")
  450. }
  451. defer cit.Close()
  452. cit.ForEach(func(commit *object.Commit) error {
  453. result = append(result, commit)
  454. return nil
  455. })
  456. return result, nil
  457. }
  458. type sortablePipelineItems []PipelineItem
  459. func (items sortablePipelineItems) Len() int {
  460. return len(items)
  461. }
  462. func (items sortablePipelineItems) Less(i, j int) bool {
  463. return items[i].Name() < items[j].Name()
  464. }
  465. func (items sortablePipelineItems) Swap(i, j int) {
  466. items[i], items[j] = items[j], items[i]
  467. }
  468. func (pipeline *Pipeline) resolve(dumpPath string) {
  469. graph := toposort.NewGraph()
  470. sort.Sort(sortablePipelineItems(pipeline.items))
  471. name2item := map[string]PipelineItem{}
  472. ambiguousMap := map[string][]string{}
  473. nameUsages := map[string]int{}
  474. for _, item := range pipeline.items {
  475. nameUsages[item.Name()]++
  476. }
  477. counters := map[string]int{}
  478. for _, item := range pipeline.items {
  479. name := item.Name()
  480. if nameUsages[name] > 1 {
  481. index := counters[item.Name()] + 1
  482. counters[item.Name()] = index
  483. name = fmt.Sprintf("%s_%d", item.Name(), index)
  484. }
  485. graph.AddNode(name)
  486. name2item[name] = item
  487. for _, key := range item.Provides() {
  488. key = "[" + key + "]"
  489. graph.AddNode(key)
  490. if graph.AddEdge(name, key) > 1 {
  491. if ambiguousMap[key] != nil {
  492. fmt.Fprintln(os.Stderr, "Pipeline:")
  493. for _, item2 := range pipeline.items {
  494. if item2 == item {
  495. fmt.Fprint(os.Stderr, "> ")
  496. }
  497. fmt.Fprint(os.Stderr, item2.Name(), " [")
  498. for i, key2 := range item2.Provides() {
  499. fmt.Fprint(os.Stderr, key2)
  500. if i < len(item.Provides())-1 {
  501. fmt.Fprint(os.Stderr, ", ")
  502. }
  503. }
  504. fmt.Fprintln(os.Stderr, "]")
  505. }
  506. pipeline.l.Critical("Failed to resolve pipeline dependencies: ambiguous graph.")
  507. return
  508. }
  509. ambiguousMap[key] = graph.FindParents(key)
  510. }
  511. }
  512. }
  513. counters = map[string]int{}
  514. for _, item := range pipeline.items {
  515. name := item.Name()
  516. if nameUsages[name] > 1 {
  517. index := counters[item.Name()] + 1
  518. counters[item.Name()] = index
  519. name = fmt.Sprintf("%s_%d", item.Name(), index)
  520. }
  521. for _, key := range item.Requires() {
  522. key = "[" + key + "]"
  523. if graph.AddEdge(key, name) == 0 {
  524. pipeline.l.Criticalf("Unsatisfied dependency: %s -> %s", key, item.Name())
  525. return
  526. }
  527. }
  528. }
  529. // Try to break the cycles in some known scenarios.
  530. if len(ambiguousMap) > 0 {
  531. var ambiguous []string
  532. for key := range ambiguousMap {
  533. ambiguous = append(ambiguous, key)
  534. }
  535. sort.Strings(ambiguous)
  536. bfsorder := graph.BreadthSort()
  537. bfsindex := map[string]int{}
  538. for i, s := range bfsorder {
  539. bfsindex[s] = i
  540. }
  541. for len(ambiguous) > 0 {
  542. key := ambiguous[0]
  543. ambiguous = ambiguous[1:]
  544. pair := ambiguousMap[key]
  545. inheritor := pair[1]
  546. if bfsindex[pair[1]] < bfsindex[pair[0]] {
  547. inheritor = pair[0]
  548. }
  549. removed := graph.RemoveEdge(key, inheritor)
  550. cycle := map[string]bool{}
  551. for _, node := range graph.FindCycle(key) {
  552. cycle[node] = true
  553. }
  554. if len(cycle) == 0 {
  555. cycle[inheritor] = true
  556. }
  557. if removed {
  558. graph.AddEdge(key, inheritor)
  559. }
  560. graph.RemoveEdge(inheritor, key)
  561. graph.ReindexNode(inheritor)
  562. // for all nodes key links to except those in cycle, put the link from inheritor
  563. for _, node := range graph.FindChildren(key) {
  564. if _, exists := cycle[node]; !exists {
  565. graph.AddEdge(inheritor, node)
  566. graph.RemoveEdge(key, node)
  567. }
  568. }
  569. graph.ReindexNode(key)
  570. }
  571. }
  572. var graphCopy *toposort.Graph
  573. if dumpPath != "" {
  574. graphCopy = graph.Copy()
  575. }
  576. strplan, ok := graph.Toposort()
  577. if !ok {
  578. pipeline.l.Critical("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
  579. return
  580. }
  581. pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
  582. for _, key := range strplan {
  583. if item, ok := name2item[key]; ok {
  584. pipeline.items = append(pipeline.items, item)
  585. }
  586. }
  587. if dumpPath != "" {
  588. // If there is a floating difference, uncomment this:
  589. // fmt.Fprint(os.Stderr, graphCopy.DebugDump())
  590. ioutil.WriteFile(dumpPath, []byte(graphCopy.Serialize(strplan)), 0666)
  591. absPath, _ := filepath.Abs(dumpPath)
  592. pipeline.l.Infof("Wrote the DAG to %s\n", absPath)
  593. }
  594. }
  595. // Initialize prepares the pipeline for the execution (Run()). This function
  596. // resolves the execution DAG, Configure()-s and Initialize()-s the items in it in the
  597. // topological dependency order. `facts` are passed inside Configure(). They are mutable.
  598. func (pipeline *Pipeline) Initialize(facts map[string]interface{}) error {
  599. cleanReturn := false
  600. defer func() {
  601. if !cleanReturn {
  602. remotes, _ := pipeline.repository.Remotes()
  603. if len(remotes) > 0 {
  604. pipeline.l.Errorf("Failed to initialize the pipeline on %s", remotes[0].Config().URLs)
  605. }
  606. }
  607. }()
  608. if facts == nil {
  609. facts = map[string]interface{}{}
  610. }
  611. // set logger from facts, otherwise set the pipeline's logger as the logger
  612. // to be used by all analysis tasks by setting the fact
  613. if l, exists := facts[ConfigLogger].(Logger); exists {
  614. pipeline.l = l
  615. } else {
  616. facts[ConfigLogger] = pipeline.l
  617. }
  618. if _, exists := facts[ConfigPipelineCommits]; !exists {
  619. var err error
  620. facts[ConfigPipelineCommits], err = pipeline.Commits(false)
  621. if err != nil {
  622. pipeline.l.Errorf("failed to list the commits: %v", err)
  623. return err
  624. }
  625. }
  626. pipeline.PrintActions, _ = facts[ConfigPipelinePrintActions].(bool)
  627. if val, exists := facts[ConfigPipelineHibernationDistance].(int); exists {
  628. if val < 0 {
  629. err := fmt.Errorf("--hibernation-distance cannot be negative (got %d)", val)
  630. pipeline.l.Error(err)
  631. return err
  632. }
  633. pipeline.HibernationDistance = val
  634. }
  635. dumpPath, _ := facts[ConfigPipelineDAGPath].(string)
  636. pipeline.resolve(dumpPath)
  637. if dumpPlan, exists := facts[ConfigPipelineDumpPlan].(bool); exists {
  638. pipeline.DumpPlan = dumpPlan
  639. }
  640. if dryRun, exists := facts[ConfigPipelineDryRun].(bool); exists {
  641. pipeline.DryRun = dryRun
  642. if dryRun {
  643. cleanReturn = true
  644. return nil
  645. }
  646. }
  647. for _, item := range pipeline.items {
  648. err := item.Configure(facts)
  649. if err != nil {
  650. cleanReturn = true
  651. return errors.Wrapf(err, "%s failed to configure", item.Name())
  652. }
  653. }
  654. for _, item := range pipeline.items {
  655. err := item.Initialize(pipeline.repository)
  656. if err != nil {
  657. cleanReturn = true
  658. return errors.Wrapf(err, "%s failed to initialize", item.Name())
  659. }
  660. }
  661. if pipeline.HibernationDistance > 0 {
  662. // if we want hibernation, then we want to minimize RSS
  663. debug.SetGCPercent(20) // the default is 100
  664. }
  665. cleanReturn = true
  666. return nil
  667. }
  668. // Run method executes the pipeline.
  669. //
  670. // `commits` is a slice with the git commits to analyse. Multiple branches are supported.
  671. //
  672. // Returns the mapping from each LeafPipelineItem to the corresponding analysis result.
  673. // There is always a "nil" record with CommonAnalysisResult.
  674. func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]interface{}, error) {
  675. startRunTime := time.Now()
  676. cleanReturn := false
  677. defer func() {
  678. if !cleanReturn {
  679. remotes, _ := pipeline.repository.Remotes()
  680. if len(remotes) > 0 {
  681. pipeline.l.Errorf("Failed to run the pipeline on %s", remotes[0].Config().URLs)
  682. }
  683. }
  684. }()
  685. onProgress := pipeline.OnProgress
  686. if onProgress == nil {
  687. onProgress = func(int, int, string) {}
  688. }
  689. plan := prepareRunPlan(commits, pipeline.HibernationDistance, pipeline.DumpPlan)
  690. progressSteps := len(plan) + 2
  691. branches := map[int][]PipelineItem{}
  692. // we will need rootClone if there is more than one root branch
  693. var rootClone []PipelineItem
  694. if !pipeline.DryRun {
  695. rootClone = cloneItems(pipeline.items, 1)[0]
  696. }
  697. var newestTime int64
  698. runTimePerItem := map[string]float64{}
  699. isMerge := func(index int, commit plumbing.Hash) bool {
  700. match := false
  701. // look for the same hash backward
  702. for i := index - 1; i > 0; i-- {
  703. switch plan[i].Action {
  704. case runActionHibernate, runActionBoot:
  705. continue
  706. case runActionCommit:
  707. match = plan[i].Commit.Hash == commit
  708. fallthrough
  709. default:
  710. i = 0
  711. }
  712. }
  713. if match {
  714. return true
  715. }
  716. // look for the same hash forward
  717. for i := index + 1; i < len(plan); i++ {
  718. switch plan[i].Action {
  719. case runActionHibernate, runActionBoot:
  720. continue
  721. case runActionCommit:
  722. match = plan[i].Commit.Hash == commit
  723. fallthrough
  724. default:
  725. i = len(plan)
  726. }
  727. }
  728. return match
  729. }
  730. commitIndex := 0
  731. for index, step := range plan {
  732. onProgress(index+1, progressSteps, step.String())
  733. if pipeline.DryRun {
  734. continue
  735. }
  736. if pipeline.PrintActions {
  737. printAction(step)
  738. }
  739. if index > 0 && index%100 == 0 && pipeline.HibernationDistance > 0 {
  740. debug.FreeOSMemory()
  741. }
  742. firstItem := step.Items[0]
  743. switch step.Action {
  744. case runActionCommit:
  745. state := map[string]interface{}{
  746. DependencyCommit: step.Commit,
  747. DependencyIndex: commitIndex,
  748. DependencyIsMerge: isMerge(index, step.Commit.Hash),
  749. }
  750. for _, item := range branches[firstItem] {
  751. startTime := time.Now()
  752. update, err := item.Consume(state)
  753. runTimePerItem[item.Name()] += time.Now().Sub(startTime).Seconds()
  754. if err != nil {
  755. pipeline.l.Errorf("%s failed on commit #%d (%d) %s: %v\n",
  756. item.Name(), commitIndex+1, index+1, step.Commit.Hash.String(), err)
  757. return nil, err
  758. }
  759. for _, key := range item.Provides() {
  760. val, ok := update[key]
  761. if !ok {
  762. err := fmt.Errorf("%s: Consume() did not return %s", item.Name(), key)
  763. pipeline.l.Critical(err)
  764. return nil, err
  765. }
  766. state[key] = val
  767. }
  768. }
  769. commitTime := step.Commit.Committer.When.Unix()
  770. if commitTime > newestTime {
  771. newestTime = commitTime
  772. }
  773. commitIndex++
  774. case runActionFork:
  775. startTime := time.Now()
  776. for i, clone := range cloneItems(branches[firstItem], len(step.Items)-1) {
  777. branches[step.Items[i+1]] = clone
  778. }
  779. runTimePerItem["*.Fork"] += time.Now().Sub(startTime).Seconds()
  780. case runActionMerge:
  781. startTime := time.Now()
  782. merged := make([][]PipelineItem, len(step.Items))
  783. for i, b := range step.Items {
  784. merged[i] = branches[b]
  785. }
  786. mergeItems(merged)
  787. runTimePerItem["*.Merge"] += time.Now().Sub(startTime).Seconds()
  788. case runActionEmerge:
  789. if firstItem == rootBranchIndex {
  790. branches[firstItem] = pipeline.items
  791. } else {
  792. branches[firstItem] = cloneItems(rootClone, 1)[0]
  793. }
  794. case runActionDelete:
  795. delete(branches, firstItem)
  796. case runActionHibernate:
  797. for _, item := range step.Items {
  798. for _, item := range branches[item] {
  799. if hi, ok := item.(HibernateablePipelineItem); ok {
  800. startTime := time.Now()
  801. err := hi.Hibernate()
  802. if err != nil {
  803. pipeline.l.Errorf("Failed to hibernate %s: %v\n", item.Name(), err)
  804. return nil, err
  805. }
  806. runTimePerItem[item.Name()+".Hibernation"] += time.Now().Sub(startTime).Seconds()
  807. }
  808. }
  809. }
  810. case runActionBoot:
  811. for _, item := range step.Items {
  812. for _, item := range branches[item] {
  813. if hi, ok := item.(HibernateablePipelineItem); ok {
  814. startTime := time.Now()
  815. err := hi.Boot()
  816. if err != nil {
  817. pipeline.l.Errorf("Failed to boot %s: %v\n", item.Name(), err)
  818. return nil, err
  819. }
  820. runTimePerItem[item.Name()+".Hibernation"] += time.Now().Sub(startTime).Seconds()
  821. }
  822. }
  823. }
  824. }
  825. }
  826. onProgress(len(plan)+1, progressSteps, MessageFinalize)
  827. result := map[LeafPipelineItem]interface{}{}
  828. if !pipeline.DryRun {
  829. for index, item := range getMasterBranch(branches) {
  830. if casted, ok := item.(DisposablePipelineItem); ok {
  831. casted.Dispose()
  832. }
  833. if casted, ok := item.(LeafPipelineItem); ok {
  834. result[pipeline.items[index].(LeafPipelineItem)] = casted.Finalize()
  835. }
  836. }
  837. }
  838. onProgress(progressSteps, progressSteps, "")
  839. result[nil] = &CommonAnalysisResult{
  840. BeginTime: plan[0].Commit.Committer.When.Unix(),
  841. EndTime: newestTime,
  842. CommitsNumber: len(commits),
  843. RunTime: time.Since(startRunTime),
  844. RunTimePerItem: runTimePerItem,
  845. }
  846. cleanReturn = true
  847. return result, nil
  848. }
  849. // LoadCommitsFromFile reads the file by the specified FS path and generates the sequence of commits
  850. // by interpreting each line as a Git commit hash.
  851. func LoadCommitsFromFile(path string, repository *git.Repository) ([]*object.Commit, error) {
  852. var file io.ReadCloser
  853. if path != "-" {
  854. var err error
  855. file, err = os.Open(path)
  856. if err != nil {
  857. return nil, err
  858. }
  859. defer file.Close()
  860. } else {
  861. file = os.Stdin
  862. }
  863. scanner := bufio.NewScanner(file)
  864. var commits []*object.Commit
  865. for scanner.Scan() {
  866. hash := plumbing.NewHash(scanner.Text())
  867. if len(hash) != 20 {
  868. return nil, errors.New("invalid commit hash " + scanner.Text())
  869. }
  870. commit, err := repository.CommitObject(hash)
  871. if err != nil {
  872. return nil, err
  873. }
  874. commits = append(commits, commit)
  875. }
  876. return commits, nil
  877. }
  878. // GetSensibleRemote extracts a remote URL of the repository to identify it.
  879. func GetSensibleRemote(repository *git.Repository) string {
  880. if r, err := repository.Remotes(); err == nil && len(r) > 0 {
  881. return r[0].Config().URLs[0]
  882. }
  883. return "<no remote>"
  884. }