ipfs-cluster/ipfs-cluster-ctl/formatters.go
Hector Sanjuan 2512ecb701 Issue #41: Add Replication factor
New PeerManager, Allocator, Informer components have been added along
with a new "replication_factor" configuration option.

First, cluster peers collect and push metrics (Informer) to the Cluster
leader regularly. The Informer is an interface that can be implemented
in custom wayts to support custom metrics.

Second, on a pin operation, using the information from the collected metrics,
an Allocator can provide a list of preferences as to where the new pin
should be assigned. The Allocator is an interface allowing to provide
different allocation strategies.

Both Allocator and Informer are Cluster Componenets, and have access
to the RPC API.

The allocations are kept in the shared state. Cluster peer failure
detection is still missing and re-allocation is still missing, although
re-pinning something when a node is down/metrics missing does re-allocate
the pin somewhere else.

License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2017-02-14 19:13:08 +01:00

116 lines
2.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/ipfs/ipfs-cluster/api"
)
const (
formatNone = iota
formatID
formatGPInfo
formatString
formatVersion
formatCidArg
)
type format int
func textFormat(body []byte, format int) {
if len(body) < 2 {
fmt.Println("")
}
slice := body[0] == '['
if slice {
textFormatSlice(body, format)
} else {
textFormatObject(body, format)
}
}
func textFormatObject(body []byte, format int) {
switch format {
case formatID:
var obj api.IDSerial
textFormatDecodeOn(body, &obj)
textFormatPrintIDSerial(&obj)
case formatGPInfo:
var obj api.GlobalPinInfoSerial
textFormatDecodeOn(body, &obj)
textFormatPrintGPinfo(&obj)
case formatVersion:
var obj api.Version
textFormatDecodeOn(body, &obj)
textFormatPrintVersion(&obj)
case formatCidArg:
var obj api.CidArgSerial
textFormatDecodeOn(body, &obj)
textFormatPrintCidArg(&obj)
default:
var obj interface{}
textFormatDecodeOn(body, &obj)
fmt.Printf("%s\n", obj)
}
}
func textFormatSlice(body []byte, format int) {
var rawMsg []json.RawMessage
textFormatDecodeOn(body, &rawMsg)
for _, raw := range rawMsg {
textFormatObject(raw, format)
}
}
func textFormatDecodeOn(body []byte, obj interface{}) {
checkErr("decoding JSON", json.Unmarshal(body, obj))
}
func textFormatPrintIDSerial(obj *api.IDSerial) {
if obj.Error != "" {
fmt.Printf("%s | ERROR: %s\n", obj.ID, obj.Error)
return
}
fmt.Printf("%s | %d peers\n", obj.ID, len(obj.ClusterPeers))
fmt.Println(" > Addresses:")
for _, a := range obj.Addresses {
fmt.Printf(" - %s\n", a)
}
if obj.IPFS.Error != "" {
fmt.Printf(" > IPFS ERROR: %s\n", obj.IPFS.Error)
return
}
fmt.Printf(" > IPFS: %s\n", obj.IPFS.ID)
for _, a := range obj.IPFS.Addresses {
fmt.Printf(" - %s\n", a)
}
}
func textFormatPrintGPinfo(obj *api.GlobalPinInfoSerial) {
fmt.Printf("%s:\n", obj.Cid)
for k, v := range obj.PeerMap {
if v.Error != "" {
fmt.Printf(" - %s ERROR: %s\n", k, v.Error)
continue
}
fmt.Printf(" > Peer %s: %s | %s\n", k, strings.ToUpper(v.Status), v.TS)
}
}
func textFormatPrintVersion(obj *api.Version) {
fmt.Println(obj.Version)
}
func textFormatPrintCidArg(obj *api.CidArgSerial) {
fmt.Printf("%s | Allocations: ", obj.Cid)
if obj.Everywhere {
fmt.Printf("[everywhere]\n")
} else {
fmt.Printf("%s", obj.Allocations)
}
}