-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from msolo/zk-name-service
Zk name service
- Loading branch information
Showing
9 changed files
with
1,001 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// +build linux | ||
|
||
// Only build on Linux, since the use of procfs is platform specific. | ||
package executil | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"strconv" | ||
) | ||
|
||
// Incomplete interpretation of the /proc/pid/stat file. | ||
type procStat struct { | ||
pid int | ||
cmd string | ||
state string | ||
ppid int | ||
pgrp int | ||
} | ||
|
||
func readProcStats(pid int) (*procStat, error) { | ||
fname := fmt.Sprintf("/proc/%v/stat", pid) | ||
data, err := ioutil.ReadFile(fname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
i := bytes.Index(data, []byte(" (")) | ||
j := bytes.Index(data, []byte(") ")) | ||
stats := procStat{} | ||
stats.pid, err = strconv.Atoi(string(data[:i])) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid pid in %v %v", fname, err) | ||
} | ||
stats.cmd = string(data[i+2 : j]) | ||
fields := string(data[j+2:]) | ||
_, err = fmt.Sscanf(fields, "%s %d %d", &stats.state, &stats.ppid, &stats.pgrp) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid scan in %v %v \"%v\"", fname, err, fields) | ||
} | ||
return &stats, nil | ||
} | ||
|
||
// Is there no better way to scan child / group processes? | ||
// For now, we have to load everything we can read see if it | ||
// matches. | ||
func readProcGroup(pgrp int) ([]*procStat, error) { | ||
dirEntries, err := ioutil.ReadDir("/proc") | ||
if err != nil { | ||
return nil, err | ||
} | ||
groupStats := make([]*procStat, 0, 256) | ||
for _, ent := range dirEntries { | ||
if pid, err := strconv.Atoi(ent.Name()); err == nil { | ||
pidStats, err := readProcStats(pid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if pidStats.pgrp == pgrp { | ||
groupStats = append(groupStats, pidStats) | ||
} | ||
} | ||
} | ||
return groupStats, nil | ||
} | ||
|
||
// GetPgrpPids return a list of all pids in a given process group. | ||
// Not as cheap as you think, you have to scan all the pids on the system. | ||
func GetPgrpPids(pgrp int) ([]int, error) { | ||
stats, err := readProcGroup(pgrp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pids := make([]int, len(stats)) | ||
for i, st := range stats { | ||
pids[i] = st.pid | ||
} | ||
return pids, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package netutil | ||
|
||
import ( | ||
"math/rand" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func checkDistribution(t *testing.T, data []*net.SRV, margin float64) { | ||
sum := 0 | ||
for _, srv := range data { | ||
sum += int(srv.Weight) | ||
} | ||
|
||
results := make(map[string]int) | ||
|
||
count := 1000 | ||
for j := 0; j < count; j++ { | ||
d := make([]*net.SRV, len(data)) | ||
copy(d, data) | ||
byPriorityWeight(d).shuffleByWeight() | ||
key := d[0].Target | ||
results[key] = results[key] + 1 | ||
} | ||
|
||
actual := results[data[0].Target] | ||
expected := float64(count) * float64(data[0].Weight) / float64(sum) | ||
diff := float64(actual) - expected | ||
t.Logf("actual: %v diff: %v e: %v m: %v", actual, diff, expected, margin) | ||
if diff < 0 { | ||
diff = -diff | ||
} | ||
if diff > (expected * margin) { | ||
t.Errorf("missed target weight: expected %v, %v", expected, actual) | ||
} | ||
} | ||
|
||
func testUniformity(t *testing.T, size int, margin float64) { | ||
rand.Seed(1) | ||
data := make([]*net.SRV, size) | ||
for i := 0; i < size; i++ { | ||
data[i] = &net.SRV{Target: string('a' + i), Weight: 1} | ||
} | ||
checkDistribution(t, data, margin) | ||
} | ||
|
||
func TestUniformity(t *testing.T) { | ||
testUniformity(t, 2, 0.05) | ||
testUniformity(t, 3, 0.10) | ||
testUniformity(t, 10, 0.20) | ||
testWeighting(t, 0.05) | ||
} | ||
|
||
func testWeighting(t *testing.T, margin float64) { | ||
rand.Seed(1) | ||
data := []*net.SRV{ | ||
&net.SRV{Target: "a", Weight: 60}, | ||
&net.SRV{Target: "b", Weight: 30}, | ||
&net.SRV{Target: "c", Weight: 10}, | ||
} | ||
checkDistribution(t, data, margin) | ||
} | ||
|
||
func TestWeighting(t *testing.T) { | ||
testWeighting(t, 0.05) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.