Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
inspector/proc: fix marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
AkihiroSuda committed Apr 7, 2016
1 parent 793a553 commit a0fe35d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
12 changes: 8 additions & 4 deletions earthquake/cli/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package cli

import (
"fmt"
"os"
"os/signal"

log "github.com/cihub/seelog"
mcli "github.com/mitchellh/cli"
Expand Down Expand Up @@ -64,12 +66,14 @@ func (cmd orchestratorCmd) Run(args []string) int {
log.Criticalf("%s", err)
return 1
}
log.Infof("Starting Orchestrator")
orchestrator.Start()
for {
}
log.Infof("Started Orchestrator")

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c

// TODO: support orchestrator.Shutdown() and get the trace
orchestrator.Shutdown()
return 0
}

Expand Down
17 changes: 15 additions & 2 deletions earthquake/explorepolicy/random/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,26 @@ import (
// parses *ProcSetEvent and returns array of PIDs.
//
// due to JSON nature, we use string for PID representation.
// FIXME: worst code ever
func parseProcSetEvent(event *signal.ProcSetEvent) ([]string, error) {
option := event.Option()
var procs []string
// for local
procs, ok := option["procs"].([]string)
if ok {
return procs, nil
}
// for rest
xprocs, ok := option["procs"].([]interface{})
if !ok {
// FIXME: this may not work with REST endpoint.
// we need to convert []interface{} to []string here..?
return nil, fmt.Errorf("no procs? this should be an implementation error. event=%#v", event)
}
for _, xproc := range xprocs {
proc, ok := xproc.(string)
if !ok {
return nil, fmt.Errorf("non-string proc? this should be an implementation error. xproc=%#v, event=%#v", xproc, event)
}
procs = append(procs, proc)
}
return procs, nil
}
40 changes: 35 additions & 5 deletions earthquake/inspector/proc/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/AkihiroSuda/go-linuxsched"
log "github.com/cihub/seelog"
"github.com/mitchellh/mapstructure"

"github.com/osrg/earthquake/earthquake/inspector/transceiver"
"github.com/osrg/earthquake/earthquake/signal"
procutil "github.com/osrg/earthquake/earthquake/util/proc"
Expand Down Expand Up @@ -100,18 +102,46 @@ func (this *ProcInspector) onWatch(procs []int) error {
switch action.(type) {
case *signal.ProcSetSchedAction:
return this.onAction(action.(*signal.ProcSetSchedAction))
case *signal.NopAction:
log.Debugf("nop action %s. ignoring.", action)
return nil
default:
return fmt.Errorf("unknown action %s. ignoring.", action)
}
}

func (this *ProcInspector) onAction(action *signal.ProcSetSchedAction) error {
// FIXME: this may not work with REST endpoint.
// we need to convert interface{} to linuxsched.SchedAttr here

// FIXME: due to JSON nature, we need complex type conversion, but it should be much simpler. this is the worst code ever
func parseAttrs(action *signal.ProcSetSchedAction) (map[string]linuxsched.SchedAttr, error) {
// for local
attrs, ok := action.Option()["attrs"].(map[string]linuxsched.SchedAttr)
if ok {
return attrs, nil
}
// for REST
xattrs, ok := action.Option()["attrs"].(map[string]interface{})
if !ok {
return fmt.Errorf("no attrs? this should be an implementation error. action=%#v", action)
return nil, fmt.Errorf("no attrs? this should be an implementation error. action=%#v", action)
}
attrs = make(map[string]linuxsched.SchedAttr)
for pidStr, xattr := range xattrs {
mattr, ok := xattr.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("bad attr? this should be an implementation error. xattr=%#v, action=%#v", xattr, action)
}
attr := linuxsched.SchedAttr{}
err := mapstructure.Decode(mattr, &attr)
if err != nil {
return nil, err
}
attrs[pidStr] = attr
}
return attrs, nil
}

func (this *ProcInspector) onAction(action *signal.ProcSetSchedAction) error {
attrs, err := parseAttrs(action)
if err != nil {
return err
}

for pidStr, attr := range attrs {
Expand Down

0 comments on commit a0fe35d

Please sign in to comment.