Merge "Need to catch the errors when closing and flushing." into main

This commit is contained in:
Yu Liu 2024-05-24 22:41:19 +00:00 committed by Gerrit Code Review
commit b83d6420e2
3 changed files with 56 additions and 36 deletions

View file

@ -28,6 +28,7 @@ import (
"strings" "strings"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
) )
type Args struct { type Args struct {
@ -190,14 +191,7 @@ func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, conf
providerValidationErrors := <-providersValidationChan providerValidationErrors := <-providersValidationChan
if providerValidationErrors != nil { if providerValidationErrors != nil {
var sb strings.Builder return nil, proptools.MergeErrors(providerValidationErrors)
for i, err := range providerValidationErrors {
if i != 0 {
sb.WriteString("\n")
}
sb.WriteString(err.Error())
}
return nil, errors.New(sb.String())
} }
if args.Memprofile != "" { if args.Memprofile != "" {

View file

@ -38,7 +38,6 @@ import (
"sync/atomic" "sync/atomic"
"text/scanner" "text/scanner"
"text/template" "text/template"
"time"
"unsafe" "unsafe"
"github.com/google/blueprint/metrics" "github.com/google/blueprint/metrics"
@ -4536,42 +4535,52 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter, shardNinja bool, ninjaF
} }
if shardNinja { if shardNinja {
var wg sync.WaitGroup
errorCh := make(chan error) errorCh := make(chan error)
fileNames := GetNinjaShardFiles(ninjaFileName) files := GetNinjaShardFiles(ninjaFileName)
shardedModules := proptools.ShardByCount(modules, len(fileNames)) shardedModules := proptools.ShardByCount(modules, len(files))
ninjaShardCnt := len(shardedModules)
for i, batchModules := range shardedModules { for i, batchModules := range shardedModules {
go func(i int, batchModules []*moduleInfo) { file := files[i]
f, err := os.OpenFile(filepath.Join(c.SrcDir(), fileNames[i]), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions) wg.Add(1)
go func(file string, batchModules []*moduleInfo) {
defer wg.Done()
f, err := os.OpenFile(filepath.Join(c.SrcDir(), file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, OutFilePermissions)
if err != nil { if err != nil {
errorCh <- fmt.Errorf("error opening Ninja file: %s", err) errorCh <- fmt.Errorf("error opening Ninja file: %s", err)
return return
} }
defer f.Close() defer func() {
buf := bufio.NewWriterSize(f, 16*1024*1024) err := f.Close()
defer buf.Flush()
writer := newNinjaWriter(buf)
errorCh <- c.writeModuleAction(batchModules, writer, headerTemplate)
}(i, batchModules)
nw.Subninja(fileNames[i])
}
if ninjaShardCnt > 0 {
afterCh := time.After(60 * time.Second)
count := 1
for {
select {
case err := <-errorCh:
if err != nil { if err != nil {
return err errorCh <- err
} else if count == ninjaShardCnt {
return nil
} }
count++ }()
case <-afterCh: buf := bufio.NewWriterSize(f, 16*1024*1024)
return fmt.Errorf("timed out when writing ninja file") defer func() {
err := buf.Flush()
if err != nil {
errorCh <- err
}
}()
writer := newNinjaWriter(buf)
err = c.writeModuleAction(batchModules, writer, headerTemplate)
if err != nil {
errorCh <- err
} }
} }(file, batchModules)
nw.Subninja(file)
}
go func() {
wg.Wait()
close(errorCh)
}()
var errors []error
for newErrors := range errorCh {
errors = append(errors, newErrors)
}
if len(errors) > 0 {
return proptools.MergeErrors(errors)
} }
return nil return nil
} else { } else {

View file

@ -1,7 +1,9 @@
package proptools package proptools
import ( import (
"errors"
"math" "math"
"strings"
) )
func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T { func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T {
@ -23,3 +25,18 @@ func ShardBySize[T ~[]E, E any](toShard T, shardSize int) []T {
func ShardByCount[T ~[]E, E any](toShard T, shardCount int) []T { func ShardByCount[T ~[]E, E any](toShard T, shardCount int) []T {
return ShardBySize(toShard, int(math.Ceil(float64(len(toShard))/float64(shardCount)))) return ShardBySize(toShard, int(math.Ceil(float64(len(toShard))/float64(shardCount))))
} }
// MergeErrors merges a list of errors into a single error.
func MergeErrors(errs []error) error {
if errs != nil {
var sb strings.Builder
for i, err := range errs {
if i != 0 {
sb.WriteString("\n")
}
sb.WriteString(err.Error())
}
return errors.New(sb.String())
}
return nil
}