pipeline.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package hercules
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "reflect"
  10. "gopkg.in/src-d/go-git.v4"
  11. "gopkg.in/src-d/go-git.v4/plumbing"
  12. "gopkg.in/src-d/go-git.v4/plumbing/object"
  13. "gopkg.in/src-d/hercules.v2/toposort"
  14. )
  15. type PipelineItem interface {
  16. // Name returns the name of the analysis.
  17. Name() string
  18. // Provides returns the list of keys of reusable calculated entities.
  19. // Other items may depend on them.
  20. Provides() []string
  21. // Requires returns the list of keys of needed entities which must be supplied in Consume().
  22. Requires() []string
  23. // Construct performs the initial creation of the object by taking parameters from facts.
  24. // It allows to create PipelineItems in a universal way.
  25. Construct(facts map[string]interface{})
  26. // Initialize prepares and resets the item. Consume() requires Initialize()
  27. // to be called at least once beforehand.
  28. Initialize(*git.Repository)
  29. // Consume processes the next commit.
  30. // deps contains the required entities which match Depends(). Besides, it always includes
  31. // "commit" and "index".
  32. // Returns the calculated entities which match Provides().
  33. Consume(deps map[string]interface{}) (map[string]interface{}, error)
  34. // Finalize returns the result of the analysis.
  35. Finalize() interface{}
  36. }
  37. type FeaturedPipelineItem interface {
  38. PipelineItem
  39. // Features returns the list of names which enable this item to be automatically inserted
  40. // in Pipeline.DeployItem().
  41. Features() []string
  42. }
  43. type PipelineItemRegistry struct {
  44. provided map[string][]reflect.Type
  45. }
  46. func (registry *PipelineItemRegistry) Register(example PipelineItem) {
  47. if registry.provided == nil {
  48. registry.provided = map[string][]reflect.Type{}
  49. }
  50. t := reflect.TypeOf(example)
  51. for _, dep := range example.Provides() {
  52. ts := registry.provided[dep]
  53. if ts == nil {
  54. ts = []reflect.Type{}
  55. }
  56. ts = append(ts, t)
  57. registry.provided[dep] = ts
  58. }
  59. }
  60. func (registry *PipelineItemRegistry) Summon(provides string) []PipelineItem {
  61. if registry.provided == nil {
  62. return []PipelineItem{}
  63. }
  64. ts := registry.provided[provides]
  65. items := []PipelineItem{}
  66. for _, t := range ts {
  67. items = append(items, reflect.New(t.Elem()).Interface().(PipelineItem))
  68. }
  69. return items
  70. }
  71. var Registry = &PipelineItemRegistry{}
  72. type wrappedPipelineItem struct {
  73. Item PipelineItem
  74. Children []wrappedPipelineItem
  75. }
  76. type Pipeline struct {
  77. // OnProgress is the callback which is invoked in Analyse() to output it's
  78. // progress. The first argument is the number of processed commits and the
  79. // second is the total number of commits.
  80. OnProgress func(int, int)
  81. // repository points to the analysed Git repository struct from go-git.
  82. repository *git.Repository
  83. // items are the registered building blocks in the pipeline. The order defines the
  84. // execution sequence.
  85. items []PipelineItem
  86. // the collection of parameters to create items.
  87. facts map[string]interface{}
  88. // Feature flags which enable the corresponding items.
  89. features map[string]bool
  90. }
  91. func NewPipeline(repository *git.Repository) *Pipeline {
  92. return &Pipeline{
  93. repository: repository,
  94. items: []PipelineItem{},
  95. facts: map[string]interface{}{},
  96. features: map[string]bool{},
  97. }
  98. }
  99. func (pipeline *Pipeline) GetFact(name string) interface{} {
  100. return pipeline.facts[name]
  101. }
  102. func (pipeline *Pipeline) SetFact(name string, value interface{}) {
  103. pipeline.facts[name] = value
  104. }
  105. func (pipeline *Pipeline) GetFeature(name string) bool {
  106. return pipeline.features[name]
  107. }
  108. func (pipeline *Pipeline) SetFeature(name string) {
  109. pipeline.features[name] = true
  110. }
  111. func (pipeline *Pipeline) DeployItem(item PipelineItem) PipelineItem {
  112. queue := []PipelineItem{}
  113. queue = append(queue, item)
  114. added := map[string]PipelineItem{}
  115. added[item.Name()] = item
  116. pipeline.AddItem(item)
  117. for len(queue) > 0 {
  118. head := queue[0]
  119. queue = queue[1:]
  120. for _, dep := range head.Requires() {
  121. for _, sibling := range Registry.Summon(dep) {
  122. if _, exists := added[sibling.Name()]; !exists {
  123. disabled := false
  124. // If this item supports features, check them against the activated in pipeline.features
  125. if fpi, matches := interface{}(sibling).(FeaturedPipelineItem); matches {
  126. for _, feature := range fpi.Features() {
  127. if !pipeline.features[feature] {
  128. disabled = true
  129. break
  130. }
  131. }
  132. }
  133. if disabled {
  134. continue
  135. }
  136. added[sibling.Name()] = sibling
  137. queue = append(queue, sibling)
  138. pipeline.AddItem(sibling)
  139. }
  140. }
  141. }
  142. }
  143. return item
  144. }
  145. func (pipeline *Pipeline) AddItem(item PipelineItem) PipelineItem {
  146. pipeline.items = append(pipeline.items, item)
  147. return item
  148. }
  149. func (pipeline *Pipeline) RemoveItem(item PipelineItem) {
  150. for i, reg := range pipeline.items {
  151. if reg == item {
  152. pipeline.items = append(pipeline.items[:i], pipeline.items[i+1:]...)
  153. return
  154. }
  155. }
  156. }
  157. // Commits returns the critical path in the repository's history. It starts
  158. // from HEAD and traces commits backwards till the root. When it encounters
  159. // a merge (more than one parent), it always chooses the first parent.
  160. func (pipeline *Pipeline) Commits() []*object.Commit {
  161. result := []*object.Commit{}
  162. repository := pipeline.repository
  163. head, err := repository.Head()
  164. if err != nil {
  165. panic(err)
  166. }
  167. commit, err := repository.CommitObject(head.Hash())
  168. if err != nil {
  169. panic(err)
  170. }
  171. // the first parent matches the head
  172. for ; err != io.EOF; commit, err = commit.Parents().Next() {
  173. if err != nil {
  174. panic(err)
  175. }
  176. result = append(result, commit)
  177. }
  178. // reverse the order
  179. for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
  180. result[i], result[j] = result[j], result[i]
  181. }
  182. return result
  183. }
  184. func (pipeline *Pipeline) resolve(dumpPath string) {
  185. graph := toposort.NewGraph()
  186. name2item := map[string]PipelineItem{}
  187. ambiguousMap := map[string][]string{}
  188. nameUsages := map[string]int{}
  189. for _, item := range pipeline.items {
  190. nameUsages[item.Name()]++
  191. }
  192. counters := map[string]int{}
  193. for _, item := range pipeline.items {
  194. name := item.Name()
  195. if nameUsages[name] > 1 {
  196. index := counters[item.Name()] + 1
  197. counters[item.Name()] = index
  198. name = fmt.Sprintf("%s_%d", item.Name(), index)
  199. }
  200. graph.AddNode(name)
  201. name2item[name] = item
  202. for _, key := range item.Provides() {
  203. key = "[" + key + "]"
  204. graph.AddNode(key)
  205. if graph.AddEdge(name, key) > 1 {
  206. if ambiguousMap[key] != nil {
  207. panic("Failed to resolve pipeline dependencies.")
  208. }
  209. ambiguousMap[key] = graph.FindParents(key)
  210. }
  211. }
  212. }
  213. counters = map[string]int{}
  214. for _, item := range pipeline.items {
  215. name := item.Name()
  216. if nameUsages[name] > 1 {
  217. index := counters[item.Name()] + 1
  218. counters[item.Name()] = index
  219. name = fmt.Sprintf("%s_%d", item.Name(), index)
  220. }
  221. for _, key := range item.Requires() {
  222. key = "[" + key + "]"
  223. if graph.AddEdge(key, name) == 0 {
  224. panic(fmt.Sprintf("Unsatisfied dependency: %s -> %s", key, item.Name()))
  225. }
  226. }
  227. }
  228. if len(ambiguousMap) > 0 {
  229. ambiguous := []string{}
  230. for key := range ambiguousMap {
  231. ambiguous = append(ambiguous, key)
  232. }
  233. bfsorder := graph.BreadthSort()
  234. bfsindex := map[string]int{}
  235. for i, s := range bfsorder {
  236. bfsindex[s] = i
  237. }
  238. for len(ambiguous) > 0 {
  239. key := ambiguous[0]
  240. ambiguous = ambiguous[1:]
  241. pair := ambiguousMap[key]
  242. inheritor := pair[1]
  243. if bfsindex[pair[1]] < bfsindex[pair[0]] {
  244. inheritor = pair[0]
  245. }
  246. removed := graph.RemoveEdge(key, inheritor)
  247. cycle := map[string]bool{}
  248. for _, node := range graph.FindCycle(key) {
  249. cycle[node] = true
  250. }
  251. if len(cycle) == 0 {
  252. cycle[inheritor] = true
  253. }
  254. if removed {
  255. graph.AddEdge(key, inheritor)
  256. }
  257. graph.RemoveEdge(inheritor, key)
  258. graph.ReindexNode(inheritor)
  259. // for all nodes key links to except those in cycle, put the link from inheritor
  260. for _, node := range graph.FindChildren(key) {
  261. if _, exists := cycle[node]; !exists {
  262. graph.AddEdge(inheritor, node)
  263. graph.RemoveEdge(key, node)
  264. }
  265. }
  266. graph.ReindexNode(key)
  267. }
  268. }
  269. var graphCopy *toposort.Graph
  270. if dumpPath != "" {
  271. graphCopy = graph.Copy()
  272. }
  273. strplan, ok := graph.Toposort()
  274. if !ok {
  275. panic("Failed to resolve pipeline dependencies.")
  276. }
  277. pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
  278. for _, key := range strplan {
  279. if item, ok := name2item[key]; ok {
  280. pipeline.items = append(pipeline.items, item)
  281. }
  282. }
  283. if dumpPath != "" {
  284. ioutil.WriteFile(dumpPath, []byte(graphCopy.Serialize(strplan)), 0666)
  285. }
  286. }
  287. func (pipeline *Pipeline) Initialize(facts map[string]interface{}) {
  288. pipeline.resolve(facts["Pipeline.DumpPath"].(string))
  289. if facts["Pipeline.DryRun"].(bool) {
  290. return
  291. }
  292. for _, item := range pipeline.items {
  293. item.Construct(facts)
  294. }
  295. for _, item := range pipeline.items {
  296. item.Initialize(pipeline.repository)
  297. }
  298. }
  299. // Run executes the pipeline.
  300. //
  301. // commits is a slice with the sequential commit history. It shall start from
  302. // the root (ascending order).
  303. func (pipeline *Pipeline) Run(commits []*object.Commit) (map[PipelineItem]interface{}, error) {
  304. onProgress := pipeline.OnProgress
  305. if onProgress == nil {
  306. onProgress = func(int, int) {}
  307. }
  308. for index, commit := range commits {
  309. onProgress(index, len(commits))
  310. state := map[string]interface{}{"commit": commit, "index": index}
  311. for _, item := range pipeline.items {
  312. update, err := item.Consume(state)
  313. if err != nil {
  314. fmt.Fprintf(os.Stderr, "%s failed on commit #%d %s\n",
  315. item.Name(), index, commit.Hash.String())
  316. return nil, err
  317. }
  318. for _, key := range item.Provides() {
  319. val, ok := update[key]
  320. if !ok {
  321. panic(fmt.Sprintf("%s: Consume() did not return %s", item.Name(), key))
  322. }
  323. state[key] = val
  324. }
  325. }
  326. }
  327. onProgress(len(commits), len(commits))
  328. result := map[PipelineItem]interface{}{}
  329. for _, item := range pipeline.items {
  330. result[item] = item.Finalize()
  331. }
  332. return result, nil
  333. }
  334. func LoadCommitsFromFile(path string, repository *git.Repository) ([]*object.Commit, error) {
  335. var file io.ReadCloser
  336. if path != "-" {
  337. var err error
  338. file, err = os.Open(path)
  339. if err != nil {
  340. return nil, err
  341. }
  342. defer file.Close()
  343. } else {
  344. file = os.Stdin
  345. }
  346. scanner := bufio.NewScanner(file)
  347. commits := []*object.Commit{}
  348. for scanner.Scan() {
  349. hash := plumbing.NewHash(scanner.Text())
  350. if len(hash) != 20 {
  351. return nil, errors.New("invalid commit hash " + scanner.Text())
  352. }
  353. commit, err := repository.CommitObject(hash)
  354. if err != nil {
  355. return nil, err
  356. }
  357. commits = append(commits, commit)
  358. }
  359. return commits, nil
  360. }