Example #1
0
// CLIInfo outputs information about the ring or builder such as node count,
// partition count, etc.
//
// Provide either the ring or the builder, but not both; set the other to nil.
// Normally the results from RingOrBuilder.
func CLIInfo(r Ring, b *Builder, output io.Writer) error {
	if r != nil {
		// TODO: Indication of how risky the assignments are: Replicas not in
		// distinct tiers, nodes.
		s := r.Stats()
		report := [][]string{
			[]string{brimtext.ThousandsSep(int64(s.PartitionCount), ","), "Partitions"},
			[]string{brimtext.ThousandsSep(int64(s.PartitionBitCount), ","), "Partition Bits"},
			[]string{brimtext.ThousandsSep(int64(r.ReplicaCount()), ","), "Replicas"},
			[]string{brimtext.ThousandsSep(int64(s.ActiveNodeCount), ","), "Active Nodes"},
			[]string{brimtext.ThousandsSep(int64(s.InactiveNodeCount), ","), "Inactive Nodes"},
			[]string{brimtext.ThousandsSepU(s.ActiveCapacity, ","), "Active Capacity"},
			[]string{brimtext.ThousandsSepU(s.InactiveCapacity, ","), "Inactive Capacity"},
			[]string{brimtext.ThousandsSep(int64(len(r.Tiers())), ","), "Tier Levels"},
			[]string{fmt.Sprintf("%.02f%%", s.MaxUnderNodePercentage), fmt.Sprintf("Worst Underweight Node (ID %d)", s.MaxUnderNodeID)},
			[]string{fmt.Sprintf("%.02f%%", s.MaxOverNodePercentage), fmt.Sprintf("Worst Overweight Node (ID %d)", s.MaxOverNodeID)},
			[]string{"Version", fmt.Sprintf("%d   %s", r.Version(), time.Unix(0, r.Version()).Format("2006-01-02 15:04:05.000"))},
		}
		reportOpts := brimtext.NewDefaultAlignOptions()
		reportOpts.Alignments = []brimtext.Alignment{brimtext.Right, brimtext.Left}
		fmt.Fprint(output, brimtext.Align(report, reportOpts))
	}
	if b != nil {
		var activeNodes int64
		var activeCapacity int64
		var inactiveNodes int64
		var inactiveCapacity int64
		for _, n := range b.Nodes() {
			if n.Active() {
				activeNodes++
				activeCapacity += int64(n.Capacity())
			} else {
				inactiveNodes++
				inactiveCapacity += int64(n.Capacity())
			}
		}
		report := [][]string{
			[]string{brimtext.ThousandsSep(activeNodes, ","), "Active Nodes"},
			[]string{brimtext.ThousandsSep(inactiveNodes, ","), "Inactive Nodes"},
			[]string{brimtext.ThousandsSep(activeCapacity, ","), "Active Capacity"},
			[]string{brimtext.ThousandsSep(inactiveCapacity, ","), "Inactive Capacity"},
			[]string{brimtext.ThousandsSep(int64(b.ReplicaCount()), ","), "Replicas"},
			[]string{brimtext.ThousandsSep(int64(len(b.Tiers())), ","), "Tier Levels"},
			[]string{brimtext.ThousandsSep(int64(b.PointsAllowed()), ","), "Points Allowed"},
			[]string{brimtext.ThousandsSep(int64(b.MaxPartitionBitCount()), ","), "Max Partition Bits"},
			[]string{brimtext.ThousandsSep(int64(b.MoveWait()), ","), "Move Wait"},
			[]string{brimtext.ThousandsSep(int64(b.IDBits()), ","), "ID Bits"},
		}
		reportOpts := brimtext.NewDefaultAlignOptions()
		reportOpts.Alignments = []brimtext.Alignment{brimtext.Right, brimtext.Left}
		fmt.Fprint(output, brimtext.Align(report, reportOpts))
		return nil
	}
	return nil
}
Example #2
0
// CLITier outputs a list of tiers in the ring or builder; see the output of
// CLIHelp for detailed information.
//
// Provide either the ring or the builder, but not both; set the other to nil.
// Normally the results from RingOrBuilder.
func CLITier(r Ring, b *Builder, args []string, output io.Writer) error {
	var tiers [][]string
	if r == nil {
		tiers = b.Tiers()
	} else {
		tiers = r.Tiers()
	}
	report := [][]string{
		[]string{"Tier", "Existing"},
		[]string{"Level", "Values"},
	}
	reportOpts := brimtext.NewDefaultAlignOptions()
	reportOpts.Alignments = []brimtext.Alignment{brimtext.Right, brimtext.Left}
	fmted := false
OUT:
	for _, values := range tiers {
		for _, value := range values {
			if strings.Contains(value, " ") {
				fmted = true
				break OUT
			}
		}
	}
	for level, values := range tiers {
		sort.Strings(values)
		var pvalue string
		if fmted {
			for _, value := range values {
				pvalue += fmt.Sprintf(" %#v", value)
			}
			pvalue = strings.Trim(pvalue, " ")
		} else {
			pvalue = strings.Join(values, " ")
		}
		report = append(report, []string{
			strconv.Itoa(level),
			strings.Trim(pvalue, " "),
		})
	}
	fmt.Fprint(output, brimtext.Align(report, reportOpts))
	return nil
}
Example #3
0
// CLINode outputs a list of nodes in the ring or builder, with optional
// filtering and also allows setting attributes on those nodes; see the output
// of CLIHelp for detailed information.
//
// Provide either the ring or the builder, but not both; set the other to nil.
// Normally the results from RingOrBuilder.
func CLINode(r Ring, b *Builder, args []string, full bool, output io.Writer) (changed bool, err error) {
	var nodes NodeSlice
	if r != nil {
		nodes = r.Nodes()
	} else {
		bnodes := b.Nodes()
		nodes = make(NodeSlice, len(bnodes))
		for i := len(nodes) - 1; i >= 0; i-- {
			nodes[i] = bnodes[i]
		}
	}
	filterArgs := make([]string, 0)
	setArgs := make([]string, 0)
	setFound := false
	for _, arg := range args {
		if arg == "set" {
			setFound = true
			continue
		}
		if setFound {
			setArgs = append(setArgs, arg)
		} else {
			filterArgs = append(filterArgs, arg)
		}
	}
	if len(setArgs) > 0 && b == nil {
		err = fmt.Errorf("set is only valid for builder files")
		return
	}
	if nodes, err = nodes.Filter(filterArgs); err != nil {
		return
	}
	if len(nodes) > 0 && len(setArgs) > 0 {
		if r != nil {
			return false, fmt.Errorf("cannot update nodes in a ring; use with a builder instead")
		}
		for _, n := range nodes {
			if err = CLIAddOrSet(b, setArgs, n.(BuilderNode), output); err != nil {
				return
			}
			changed = true
		}
	}
	if full || len(nodes) == 1 {
		first := true
		for _, n := range nodes {
			if first {
				first = false
			} else {
				fmt.Fprintln(output)
			}
			output.Write([]byte(CLINodeReport(n)))
		}
		return
	}
	header := []string{
		"ID",
		"Active",
		"Capacity",
		"Address",
		"Meta",
	}
	report := [][]string{header}
	reportAlign := brimtext.NewDefaultAlignOptions()
	reportAlign.Alignments = []brimtext.Alignment{
		brimtext.Left,
		brimtext.Right,
		brimtext.Right,
		brimtext.Right,
		brimtext.Left,
	}
	reportLine := func(n Node) []string {
		return []string{
			fmt.Sprintf("%d", n.ID()),
			fmt.Sprintf("%v", n.Active()),
			fmt.Sprintf("%d", n.Capacity()),
			n.Address(0),
			n.Meta(),
		}
	}
	for _, n := range nodes {
		if n.Active() {
			report = append(report, reportLine(n))
		}
	}
	for _, n := range nodes {
		if !n.Active() {
			report = append(report, reportLine(n))
		}
	}
	fmt.Fprint(output, brimtext.Align(report, reportAlign))
	return
}
Example #4
0
func main() {
	if len(os.Args) != 2 {
		fmt.Printf("%s <dir>\n", os.Args[0])
		os.Exit(1)
	}
	dirCount, otherCount, fileSizes := dirWalk(os.Args[1], nil)
	var fileSizesTotal int64
	for _, s := range fileSizes {
		fileSizesTotal += s
	}
	fileSizesMean := fileSizesTotal / int64(len(fileSizes))
	var fileSizesMedian int64
	if len(fileSizes) > 0 {
		sort.Sort(fileSizes)
		fileSizesMedian = fileSizes[len(fileSizes)/2]
	}
	report := [][]string{
		{fmt.Sprintf("%d", dirCount), "directories"},
		{fmt.Sprintf("%d", len(fileSizes)), "files"},
		{fmt.Sprintf("%d", otherCount), "items not files nor directories"},
		{fmt.Sprintf("%d", fileSizesTotal), "total file bytes"},
		{fmt.Sprintf("%d", fileSizesMean), "mean file size"},
		{fmt.Sprintf("%d", fileSizesMedian), "median file size"},
	}
	alignOptions := brimtext.NewDefaultAlignOptions()
	alignOptions.Alignments = []brimtext.Alignment{brimtext.Right, brimtext.Left}
	fmt.Println(brimtext.Align(report, alignOptions))

	var blockSizesMean int64
	var blockSizesMedian int64
	var blockSizesCount int64
	var blockSizesFull int64
	var blockSizesTarget int64
	blockSizesTarget = fileSizesMean
	var blockSizes int64Slice
	overshoot := true
	for {
		blockSizes = blockSizes[:0]
		blockSizesFull = 0
		for _, s := range fileSizes {
			if s < 1 {
				blockSizes = append(blockSizes, 0)
				continue
			}
			for s >= blockSizesTarget {
				blockSizes = append(blockSizes, blockSizesTarget)
				s -= blockSizesTarget
				blockSizesFull++
			}
			if s > 0 {
				blockSizes = append(blockSizes, s)
			}
		}
		blockSizesCount = int64(len(blockSizes))
		blockSizesMean = fileSizesTotal / blockSizesCount
		if blockSizesCount > 0 {
			sort.Sort(blockSizes)
			blockSizesMedian = blockSizes[blockSizesCount/2]
		}
		if blockSizesMedian > blockSizesMean && float64(blockSizesMedian-blockSizesMean)/float64(blockSizesMedian) < 0.01 {
			if !overshoot {
				break
			}
			if blockSizesTarget != math.MaxInt64 {
				if blockSizesTarget > math.MaxInt64/2 {
					blockSizesTarget = math.MaxInt64
				} else {
					blockSizesTarget *= 2
				}
				continue
			}
		}
		overshoot = false
		blockSizesTargetNew := blockSizesTarget - (blockSizesTarget-blockSizesMean+1)/2
		if blockSizesTargetNew == blockSizesTarget {
			break
		}
		blockSizesTarget = blockSizesTargetNew
	}
	report = [][]string{
		{fmt.Sprintf("%d", blockSizesMean), "mean block size"},
		{fmt.Sprintf("%d", blockSizesMedian), "median block size"},
		{fmt.Sprintf("%d", blockSizesCount), "block count"},
		{fmt.Sprintf("%d", blockSizesFull), fmt.Sprintf("full block count, %.0f%%", 100*float64(blockSizesFull)/float64(blockSizesCount))},
		{fmt.Sprintf("%d", blockSizesTarget), "\"best\" block size, median~=mean"},
	}
	fmt.Println(brimtext.Align(report, alignOptions))

	blockSizesTarget = math.MaxInt64
	var blockSizesTargetHighCap int64
	var blockSizesTargetLast int64
	overshoot = true
	done := false
	for {
		blockSizes = blockSizes[:0]
		blockSizesFull = 0
		for _, s := range fileSizes {
			if s < 1 {
				blockSizes = append(blockSizes, 0)
				continue
			}
			for s >= blockSizesTarget {
				blockSizes = append(blockSizes, blockSizesTarget)
				s -= blockSizesTarget
				blockSizesFull++
			}
			if s > 0 {
				blockSizes = append(blockSizes, s)
			}
		}
		blockSizesCount = int64(len(blockSizes))
		blockSizesMean = fileSizesTotal / blockSizesCount
		if blockSizesCount > 0 {
			sort.Sort(blockSizes)
			blockSizesMedian = blockSizes[blockSizesCount/2]
		}
		if done {
			break
		}
		if float64(blockSizesFull)/float64(blockSizesCount) < 0.5 {
			if !overshoot {
				blockSizesTarget = blockSizesTargetLast
				done = true
				continue
			}
			blockSizesTargetHighCap = blockSizesTarget
			blockSizesTarget /= 2
			if blockSizesTarget < 1 {
				blockSizesTarget = 1
				done = true
			}
			continue
		}
		overshoot = false
		blockSizesTargetNew := blockSizesTarget + (blockSizesTargetHighCap-blockSizesTarget+1)/2
		if blockSizesTargetNew == blockSizesTarget {
			break
		}
		blockSizesTargetLast = blockSizesTarget
		blockSizesTarget = blockSizesTargetNew
	}
	report = [][]string{
		{fmt.Sprintf("%d", blockSizesMean), "mean block size"},
		{fmt.Sprintf("%d", blockSizesMedian), "median block size"},
		{fmt.Sprintf("%d", blockSizesCount), "block count"},
		{fmt.Sprintf("%d", blockSizesFull), fmt.Sprintf("full block count, %.0f%%", 100*float64(blockSizesFull)/float64(blockSizesCount))},
		{fmt.Sprintf("%d", blockSizesTarget), "\"best\" block size, full~=50%"},
	}
	fmt.Println(brimtext.Align(report, alignOptions))
}