diff --git a/cmd/barak/main.go b/cmd/barak/main.go index 4c056e6be..5c2195780 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -14,6 +14,7 @@ import ( "os" "reflect" "sync" + "time" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/cmd/barak/types" @@ -41,12 +42,14 @@ var barak = struct { nonce uint64 processes map[string]*pcm.Process validators []Validator + rootDir string }{ mtx: sync.Mutex{}, pid: os.Getpid(), nonce: 0, processes: make(map[string]*pcm.Process), validators: nil, + rootDir: "", } func main() { @@ -72,6 +75,10 @@ func main() { } barak.nonce = options.StartNonce barak.validators = options.Validators + barak.rootDir = os.Getenv("BRKROOT") + if barak.rootDir == "" { + barak.rootDir = os.Getenv("HOME") + "/.barak" + } // Debug. fmt.Printf("Options: %v\n", options) @@ -174,7 +181,12 @@ func RunProcess(wait bool, label string, execPath string, args []string, input s } // Otherwise, create one. - proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input) + err := EnsureDir(barak.rootDir + "/outputs") + if err != nil { + return nil, fmt.Errorf("Failed to create outputs dir: %v", err) + } + outPath := Fmt("%v/outputs/%v_%v.out", barak.rootDir, label, time.Now().Format("2006_01_02_15_04_05_MST")) + proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input, outPath) if err == nil { barak.processes[label] = proc } diff --git a/common/os.go b/common/os.go index 2d65683b8..7a5ec0fa2 100644 --- a/common/os.go +++ b/common/os.go @@ -50,3 +50,13 @@ func AtomicWriteFile(filePath string, newBytes []byte) error { } return nil } + +func EnsureDir(dir string) error { + if _, err := os.Stat(dir); os.IsNotExist(err) { + err := os.MkdirAll(dir, 0700) + if err != nil { + return fmt.Errorf("Could not create directory %v. %v", dir, err) + } + } + return nil +} diff --git a/process/process.go b/process/process.go index 1bbe686e6..3f18378e4 100644 --- a/process/process.go +++ b/process/process.go @@ -10,16 +10,6 @@ import ( "time" ) -func makeFile(prefix string) (string, *os.File) { - now := time.Now() - path := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST")) - file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - panic(err) - } - return path, file -} - type Process struct { Label string ExecPath string @@ -40,8 +30,11 @@ const ( // execPath: command name // args: args to command. (should not include name) -func Create(mode int, label string, execPath string, args []string, input string) (*Process, error) { - outPath, outFile := makeFile("output_" + label) +func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) { + outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + return nil, err + } cmd := exec.Command(execPath, args...) switch mode { case ProcessModeStd: diff --git a/state/validator_set.go b/state/validator_set.go index 6220f2667..2d247313b 100644 --- a/state/validator_set.go +++ b/state/validator_set.go @@ -45,10 +45,6 @@ func NewValidatorSet(vals []*Validator) *ValidatorSet { // TODO: mind the overflow when times and votingPower shares too large. func (valSet *ValidatorSet) IncrementAccum(times uint) { - log.Debug("IncrementAccum", "times", times) - - log.Debug(Fmt("IncrementAccum prior to accum: %v\n", valSet)) - // Add VotingPower * times to each validator and order into heap. validatorsHeap := NewHeap() for _, val := range valSet.Validators { @@ -56,8 +52,6 @@ func (valSet *ValidatorSet) IncrementAccum(times uint) { validatorsHeap.Push(val, accumComparable(val.Accum)) } - log.Debug(Fmt("IncrementAccum after accum: %v\n", valSet)) - // Decrement the validator with most accum, times times. for i := uint(0); i < times; i++ { mostest := validatorsHeap.Peek().(*Validator) @@ -67,9 +61,6 @@ func (valSet *ValidatorSet) IncrementAccum(times uint) { mostest.Accum -= int64(valSet.TotalVotingPower()) validatorsHeap.Update(mostest, accumComparable(mostest.Accum)) } - - log.Debug(Fmt("IncrementAccum after decrements: %v\n", valSet)) - log.Debug(Fmt("IncrementAccum chose proposer: %v\n", valSet.proposer)) } func (valSet *ValidatorSet) Copy() *ValidatorSet {