2017-02-08 17:04:08 +00:00
|
|
|
// Package api holds declarations for types used in ipfs-cluster APIs to make
|
|
|
|
// them re-usable across differen tools. This include RPC API "Serial[izable]"
|
|
|
|
// versions for types. The Go API uses natives types, while RPC API,
|
2017-12-06 14:00:01 +00:00
|
|
|
// REST APIs etc use serializable types (i.e. json format). Conversion methods
|
2017-02-08 17:04:08 +00:00
|
|
|
// exists between types.
|
|
|
|
//
|
|
|
|
// Note that all conversion methods ignore any parsing errors. All values must
|
|
|
|
// be validated first before initializing any of the types defined here.
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-12-08 14:39:30 +00:00
|
|
|
"fmt"
|
2018-05-26 01:36:39 +00:00
|
|
|
"regexp"
|
2018-01-16 10:21:34 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-02-08 17:04:08 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
2017-12-07 22:59:24 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2017-02-08 17:04:08 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
protocol "github.com/libp2p/go-libp2p-protocol"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2017-12-07 22:59:24 +00:00
|
|
|
|
|
|
|
// needed to parse /ws multiaddresses
|
|
|
|
_ "github.com/libp2p/go-ws-transport"
|
2018-03-16 12:37:32 +00:00
|
|
|
// needed to parse /dns* multiaddresses
|
2017-12-07 22:59:24 +00:00
|
|
|
_ "github.com/multiformats/go-multiaddr-dns"
|
2017-02-08 17:04:08 +00:00
|
|
|
)
|
|
|
|
|
2017-12-07 22:59:24 +00:00
|
|
|
var logger = logging.Logger("apitypes")
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
// TrackerStatus values
|
|
|
|
const (
|
|
|
|
// IPFSStatus should never take this value
|
2018-06-01 15:27:21 +00:00
|
|
|
TrackerStatusBug TrackerStatus = iota
|
2017-02-08 17:04:08 +00:00
|
|
|
// The cluster node is offline or not responding
|
|
|
|
TrackerStatusClusterError
|
|
|
|
// An error occurred pinning
|
|
|
|
TrackerStatusPinError
|
|
|
|
// An error occurred unpinning
|
|
|
|
TrackerStatusUnpinError
|
|
|
|
// The IPFS daemon has pinned the item
|
|
|
|
TrackerStatusPinned
|
|
|
|
// The IPFS daemon is currently pinning the item
|
|
|
|
TrackerStatusPinning
|
|
|
|
// The IPFS daemon is currently unpinning the item
|
|
|
|
TrackerStatusUnpinning
|
|
|
|
// The IPFS daemon is not pinning the item
|
|
|
|
TrackerStatusUnpinned
|
2018-04-18 04:53:41 +00:00
|
|
|
// The IPFS daemon is not pinning the item but it is being tracked
|
2017-02-13 15:46:53 +00:00
|
|
|
TrackerStatusRemote
|
2018-04-18 04:53:41 +00:00
|
|
|
// The item has been queued for pinning on the IPFS daemon
|
|
|
|
TrackerStatusPinQueued
|
|
|
|
// The item has been queued for unpinning on the IPFS daemon
|
|
|
|
TrackerStatusUnpinQueued
|
2017-02-08 17:04:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TrackerStatus represents the status of a tracked Cid in the PinTracker
|
|
|
|
type TrackerStatus int
|
|
|
|
|
|
|
|
var trackerStatusString = map[TrackerStatus]string{
|
|
|
|
TrackerStatusBug: "bug",
|
|
|
|
TrackerStatusClusterError: "cluster_error",
|
|
|
|
TrackerStatusPinError: "pin_error",
|
|
|
|
TrackerStatusUnpinError: "unpin_error",
|
|
|
|
TrackerStatusPinned: "pinned",
|
|
|
|
TrackerStatusPinning: "pinning",
|
|
|
|
TrackerStatusUnpinning: "unpinning",
|
|
|
|
TrackerStatusUnpinned: "unpinned",
|
2017-02-13 15:46:53 +00:00
|
|
|
TrackerStatusRemote: "remote",
|
2018-04-18 04:53:41 +00:00
|
|
|
TrackerStatusPinQueued: "pin_queued",
|
|
|
|
TrackerStatusUnpinQueued: "unpin_queued",
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String converts a TrackerStatus into a readable string.
|
|
|
|
func (st TrackerStatus) String() string {
|
|
|
|
return trackerStatusString[st]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrackerStatusFromString parses a string and returns the matching
|
|
|
|
// TrackerStatus value.
|
|
|
|
func TrackerStatusFromString(str string) TrackerStatus {
|
|
|
|
for k, v := range trackerStatusString {
|
|
|
|
if v == str {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TrackerStatusBug
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPFSPinStatus values
|
|
|
|
const (
|
2018-06-01 15:27:21 +00:00
|
|
|
IPFSPinStatusBug IPFSPinStatus = iota
|
2017-02-08 17:04:08 +00:00
|
|
|
IPFSPinStatusError
|
|
|
|
IPFSPinStatusDirect
|
|
|
|
IPFSPinStatusRecursive
|
|
|
|
IPFSPinStatusIndirect
|
|
|
|
IPFSPinStatusUnpinned
|
|
|
|
)
|
|
|
|
|
|
|
|
// IPFSPinStatus represents the status of a pin in IPFS (direct, recursive etc.)
|
|
|
|
type IPFSPinStatus int
|
|
|
|
|
|
|
|
// IPFSPinStatusFromString parses a string and returns the matching
|
|
|
|
// IPFSPinStatus.
|
|
|
|
func IPFSPinStatusFromString(t string) IPFSPinStatus {
|
2018-05-26 01:08:45 +00:00
|
|
|
// Since indirect statuses are of the form "indirect through <cid>", use a regexp to match
|
|
|
|
var ind, _ = regexp.MatchString("^indirect", t)
|
2017-02-08 17:04:08 +00:00
|
|
|
// TODO: This is only used in the http_connector to parse
|
|
|
|
// ipfs-daemon-returned values. Maybe it should be extended.
|
|
|
|
switch {
|
2018-05-26 01:08:45 +00:00
|
|
|
case ind:
|
2017-02-08 17:04:08 +00:00
|
|
|
return IPFSPinStatusIndirect
|
|
|
|
case t == "direct":
|
|
|
|
return IPFSPinStatusDirect
|
|
|
|
case t == "recursive":
|
|
|
|
return IPFSPinStatusRecursive
|
|
|
|
default:
|
|
|
|
return IPFSPinStatusBug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPinned returns true if the status is Direct or Recursive
|
|
|
|
func (ips IPFSPinStatus) IsPinned() bool {
|
|
|
|
return ips == IPFSPinStatusDirect || ips == IPFSPinStatusRecursive
|
|
|
|
}
|
|
|
|
|
2018-05-25 09:05:35 +00:00
|
|
|
// ToTrackerStatus converts the IPFSPinStatus value to the
|
|
|
|
// appropriate TrackerStatus value.
|
|
|
|
func (ips IPFSPinStatus) ToTrackerStatus() TrackerStatus {
|
|
|
|
return ipfsPinStatus2TrackerStatusMap[ips]
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipfsPinStatus2TrackerStatusMap = map[IPFSPinStatus]TrackerStatus{
|
|
|
|
IPFSPinStatusDirect: TrackerStatusPinned,
|
|
|
|
IPFSPinStatusRecursive: TrackerStatusPinned,
|
|
|
|
IPFSPinStatusIndirect: TrackerStatusUnpinned,
|
|
|
|
IPFSPinStatusUnpinned: TrackerStatusUnpinned,
|
|
|
|
IPFSPinStatusBug: TrackerStatusBug,
|
|
|
|
IPFSPinStatusError: TrackerStatusClusterError, //TODO(ajl): check suitability
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
// GlobalPinInfo contains cluster-wide status information about a tracked Cid,
|
|
|
|
// indexed by cluster peer.
|
|
|
|
type GlobalPinInfo struct {
|
|
|
|
Cid *cid.Cid
|
|
|
|
PeerMap map[peer.ID]PinInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalPinInfoSerial is the serializable version of GlobalPinInfo.
|
|
|
|
type GlobalPinInfoSerial struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
PeerMap map[string]PinInfoSerial `json:"peer_map"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSerial converts a GlobalPinInfo to its serializable version.
|
|
|
|
func (gpi GlobalPinInfo) ToSerial() GlobalPinInfoSerial {
|
|
|
|
s := GlobalPinInfoSerial{}
|
2017-12-07 22:59:24 +00:00
|
|
|
if gpi.Cid != nil {
|
|
|
|
s.Cid = gpi.Cid.String()
|
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
s.PeerMap = make(map[string]PinInfoSerial)
|
|
|
|
for k, v := range gpi.PeerMap {
|
|
|
|
s.PeerMap[peer.IDB58Encode(k)] = v.ToSerial()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToGlobalPinInfo converts a GlobalPinInfoSerial to its native version.
|
|
|
|
func (gpis GlobalPinInfoSerial) ToGlobalPinInfo() GlobalPinInfo {
|
2017-12-07 22:59:24 +00:00
|
|
|
c, err := cid.Decode(gpis.Cid)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(gpis.Cid, err)
|
2017-12-07 22:59:24 +00:00
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
gpi := GlobalPinInfo{
|
|
|
|
Cid: c,
|
|
|
|
PeerMap: make(map[peer.ID]PinInfo),
|
|
|
|
}
|
|
|
|
for k, v := range gpis.PeerMap {
|
2017-12-07 22:59:24 +00:00
|
|
|
p, err := peer.IDB58Decode(k)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(k, err)
|
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
gpi.PeerMap[p] = v.ToPinInfo()
|
|
|
|
}
|
|
|
|
return gpi
|
|
|
|
}
|
|
|
|
|
2017-11-30 00:53:31 +00:00
|
|
|
// PinInfo holds information about local pins.
|
2017-02-08 17:04:08 +00:00
|
|
|
type PinInfo struct {
|
|
|
|
Cid *cid.Cid
|
|
|
|
Peer peer.ID
|
|
|
|
Status TrackerStatus
|
|
|
|
TS time.Time
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PinInfoSerial is a serializable version of PinInfo.
|
|
|
|
// information is marked as
|
|
|
|
type PinInfoSerial struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
Peer string `json:"peer"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
TS string `json:"timestamp"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSerial converts a PinInfo to its serializable version.
|
|
|
|
func (pi PinInfo) ToSerial() PinInfoSerial {
|
2017-12-07 22:59:24 +00:00
|
|
|
c := ""
|
|
|
|
if pi.Cid != nil {
|
|
|
|
c = pi.Cid.String()
|
|
|
|
}
|
|
|
|
p := ""
|
|
|
|
if pi.Peer != "" {
|
|
|
|
p = peer.IDB58Encode(pi.Peer)
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
return PinInfoSerial{
|
2017-12-07 22:59:24 +00:00
|
|
|
Cid: c,
|
|
|
|
Peer: p,
|
2017-02-08 17:04:08 +00:00
|
|
|
Status: pi.Status.String(),
|
2017-03-14 12:45:17 +00:00
|
|
|
TS: pi.TS.UTC().Format(time.RFC3339),
|
2017-02-08 17:04:08 +00:00
|
|
|
Error: pi.Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPinInfo converts a PinInfoSerial to its native version.
|
|
|
|
func (pis PinInfoSerial) ToPinInfo() PinInfo {
|
2017-12-07 22:59:24 +00:00
|
|
|
c, err := cid.Decode(pis.Cid)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(pis.Cid, err)
|
2017-12-07 22:59:24 +00:00
|
|
|
}
|
|
|
|
p, err := peer.IDB58Decode(pis.Peer)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(pis.Peer, err)
|
2017-12-07 22:59:24 +00:00
|
|
|
}
|
|
|
|
ts, err := time.Parse(time.RFC3339, pis.TS)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(pis.TS, err)
|
2017-12-07 22:59:24 +00:00
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
return PinInfo{
|
|
|
|
Cid: c,
|
|
|
|
Peer: p,
|
|
|
|
Status: TrackerStatusFromString(pis.Status),
|
|
|
|
TS: ts,
|
|
|
|
Error: pis.Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version holds version information
|
|
|
|
type Version struct {
|
|
|
|
Version string `json:"Version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPFSID is used to store information about the underlying IPFS daemon
|
|
|
|
type IPFSID struct {
|
|
|
|
ID peer.ID
|
|
|
|
Addresses []ma.Multiaddr
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPFSIDSerial is the serializable IPFSID for RPC requests
|
|
|
|
type IPFSIDSerial struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Addresses MultiaddrsSerial `json:"addresses"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSerial converts IPFSID to a go serializable object
|
|
|
|
func (id *IPFSID) ToSerial() IPFSIDSerial {
|
2017-12-07 22:59:24 +00:00
|
|
|
p := ""
|
|
|
|
if id.ID != "" {
|
|
|
|
p = peer.IDB58Encode(id.ID)
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
return IPFSIDSerial{
|
2017-12-07 22:59:24 +00:00
|
|
|
ID: p,
|
2017-02-08 17:04:08 +00:00
|
|
|
Addresses: MultiaddrsToSerial(id.Addresses),
|
|
|
|
Error: id.Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToIPFSID converts an IPFSIDSerial to IPFSID
|
|
|
|
func (ids *IPFSIDSerial) ToIPFSID() IPFSID {
|
|
|
|
id := IPFSID{}
|
|
|
|
if pID, err := peer.IDB58Decode(ids.ID); err == nil {
|
|
|
|
id.ID = pID
|
|
|
|
}
|
|
|
|
id.Addresses = ids.Addresses.ToMultiaddrs()
|
|
|
|
id.Error = ids.Error
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:12:46 +00:00
|
|
|
// ConnectGraph holds information about the connectivity of the cluster
|
|
|
|
// To read, traverse the keys of ClusterLinks. Each such id is one of
|
|
|
|
// the peers of the "ClusterID" peer running the query. ClusterLinks[id]
|
|
|
|
// in turn lists the ids that peer "id" sees itself connected to. It is
|
|
|
|
// possible that id is a peer of ClusterID, but ClusterID can not reach id
|
|
|
|
// over rpc, in which case ClusterLinks[id] == [], as id's view of its
|
|
|
|
// connectivity can not be retrieved.
|
|
|
|
//
|
|
|
|
// Iff there was an error reading the IPFSID of the peer then id will not be a
|
|
|
|
// key of ClustertoIPFS or IPFSLinks. Finally iff id is a key of ClustertoIPFS
|
|
|
|
// then id will be a key of IPFSLinks. In the event of a SwarmPeers error
|
|
|
|
// IPFSLinks[id] == [].
|
|
|
|
type ConnectGraph struct {
|
|
|
|
ClusterID peer.ID
|
|
|
|
IPFSLinks map[peer.ID][]peer.ID // ipfs to ipfs links
|
|
|
|
ClusterLinks map[peer.ID][]peer.ID // cluster to cluster links
|
|
|
|
ClustertoIPFS map[peer.ID]peer.ID // cluster to ipfs links
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectGraphSerial is the serializable ConnectGraph counterpart for RPC requests
|
|
|
|
type ConnectGraphSerial struct {
|
|
|
|
ClusterID string
|
|
|
|
IPFSLinks map[string][]string `json:"ipfs_links"`
|
|
|
|
ClusterLinks map[string][]string `json:"cluster_links"`
|
|
|
|
ClustertoIPFS map[string]string `json:"cluster_to_ipfs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSerial converts a ConnectGraph to its Go-serializable version
|
|
|
|
func (cg ConnectGraph) ToSerial() ConnectGraphSerial {
|
2018-01-18 02:49:35 +00:00
|
|
|
IPFSLinksSerial := serializeLinkMap(cg.IPFSLinks)
|
|
|
|
ClusterLinksSerial := serializeLinkMap(cg.ClusterLinks)
|
2017-10-13 21:12:46 +00:00
|
|
|
ClustertoIPFSSerial := make(map[string]string)
|
|
|
|
for k, v := range cg.ClustertoIPFS {
|
|
|
|
ClustertoIPFSSerial[peer.IDB58Encode(k)] = peer.IDB58Encode(v)
|
|
|
|
}
|
|
|
|
return ConnectGraphSerial{
|
|
|
|
ClusterID: peer.IDB58Encode(cg.ClusterID),
|
|
|
|
IPFSLinks: IPFSLinksSerial,
|
|
|
|
ClusterLinks: ClusterLinksSerial,
|
|
|
|
ClustertoIPFS: ClustertoIPFSSerial,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:49:35 +00:00
|
|
|
// ToConnectGraph converts a ConnectGraphSerial to a ConnectGraph
|
|
|
|
func (cgs ConnectGraphSerial) ToConnectGraph() ConnectGraph {
|
|
|
|
ClustertoIPFS := make(map[peer.ID]peer.ID)
|
|
|
|
for k, v := range cgs.ClustertoIPFS {
|
|
|
|
pid1, _ := peer.IDB58Decode(k)
|
|
|
|
pid2, _ := peer.IDB58Decode(v)
|
|
|
|
ClustertoIPFS[pid1] = pid2
|
|
|
|
}
|
|
|
|
pid, _ := peer.IDB58Decode(cgs.ClusterID)
|
|
|
|
return ConnectGraph{
|
|
|
|
ClusterID: pid,
|
|
|
|
IPFSLinks: deserializeLinkMap(cgs.IPFSLinks),
|
|
|
|
ClusterLinks: deserializeLinkMap(cgs.ClusterLinks),
|
|
|
|
ClustertoIPFS: ClustertoIPFS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeLinkMap(Links map[peer.ID][]peer.ID) map[string][]string {
|
2017-10-13 21:12:46 +00:00
|
|
|
LinksSerial := make(map[string][]string)
|
|
|
|
for k, v := range Links {
|
|
|
|
kS := peer.IDB58Encode(k)
|
|
|
|
LinksSerial[kS] = PeersToStrings(v)
|
|
|
|
}
|
|
|
|
return LinksSerial
|
|
|
|
}
|
|
|
|
|
2018-01-18 02:49:35 +00:00
|
|
|
func deserializeLinkMap(LinksSerial map[string][]string) map[peer.ID][]peer.ID {
|
|
|
|
Links := make(map[peer.ID][]peer.ID)
|
|
|
|
for k, v := range LinksSerial {
|
|
|
|
pid, _ := peer.IDB58Decode(k)
|
|
|
|
Links[pid] = StringsToPeers(v)
|
|
|
|
}
|
|
|
|
return Links
|
2017-10-13 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 02:49:35 +00:00
|
|
|
// SwarmPeers lists an ipfs daemon's peers
|
|
|
|
type SwarmPeers []peer.ID
|
|
|
|
|
2017-10-13 21:12:46 +00:00
|
|
|
// SwarmPeersSerial is the serialized form of SwarmPeers for RPC use
|
2018-01-18 02:49:35 +00:00
|
|
|
type SwarmPeersSerial []string
|
2017-10-13 21:12:46 +00:00
|
|
|
|
|
|
|
// ToSerial converts SwarmPeers to its Go-serializeable version
|
|
|
|
func (swarm SwarmPeers) ToSerial() SwarmPeersSerial {
|
2018-01-18 02:49:35 +00:00
|
|
|
return PeersToStrings(swarm)
|
2017-10-13 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToSwarmPeers converts a SwarmPeersSerial object to SwarmPeers.
|
|
|
|
func (swarmS SwarmPeersSerial) ToSwarmPeers() SwarmPeers {
|
2018-01-18 02:49:35 +00:00
|
|
|
return StringsToPeers(swarmS)
|
2017-10-13 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
// ID holds information about the Cluster peer
|
|
|
|
type ID struct {
|
2017-11-10 15:09:45 +00:00
|
|
|
ID peer.ID
|
|
|
|
Addresses []ma.Multiaddr
|
|
|
|
ClusterPeers []peer.ID
|
|
|
|
ClusterPeersAddresses []ma.Multiaddr
|
|
|
|
Version string
|
|
|
|
Commit string
|
|
|
|
RPCProtocolVersion protocol.ID
|
|
|
|
Error string
|
|
|
|
IPFS IPFSID
|
2017-12-01 18:50:13 +00:00
|
|
|
Peername string
|
2017-02-08 17:04:08 +00:00
|
|
|
//PublicKey crypto.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDSerial is the serializable ID counterpart for RPC requests
|
|
|
|
type IDSerial struct {
|
2017-11-10 15:09:45 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Addresses MultiaddrsSerial `json:"addresses"`
|
|
|
|
ClusterPeers []string `json:"cluster_peers"`
|
|
|
|
ClusterPeersAddresses MultiaddrsSerial `json:"cluster_peers_addresses"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Commit string `json:"commit"`
|
|
|
|
RPCProtocolVersion string `json:"rpc_protocol_version"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
IPFS IPFSIDSerial `json:"ipfs"`
|
2017-12-01 18:50:13 +00:00
|
|
|
Peername string `json:"peername"`
|
2017-02-08 17:04:08 +00:00
|
|
|
//PublicKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSerial converts an ID to its Go-serializable version
|
|
|
|
func (id ID) ToSerial() IDSerial {
|
|
|
|
//var pkey []byte
|
|
|
|
//if id.PublicKey != nil {
|
|
|
|
// pkey, _ = id.PublicKey.Bytes()
|
|
|
|
//}
|
|
|
|
|
2017-12-07 22:59:24 +00:00
|
|
|
p := ""
|
|
|
|
if id.ID != "" {
|
|
|
|
p = peer.IDB58Encode(id.ID)
|
2017-11-10 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 17:04:08 +00:00
|
|
|
return IDSerial{
|
2017-12-07 22:59:24 +00:00
|
|
|
ID: p,
|
2017-11-10 15:09:45 +00:00
|
|
|
Addresses: MultiaddrsToSerial(id.Addresses),
|
2017-12-07 22:59:24 +00:00
|
|
|
ClusterPeers: PeersToStrings(id.ClusterPeers),
|
2017-11-10 15:09:45 +00:00
|
|
|
ClusterPeersAddresses: MultiaddrsToSerial(id.ClusterPeersAddresses),
|
|
|
|
Version: id.Version,
|
|
|
|
Commit: id.Commit,
|
|
|
|
RPCProtocolVersion: string(id.RPCProtocolVersion),
|
|
|
|
Error: id.Error,
|
|
|
|
IPFS: id.IPFS.ToSerial(),
|
2017-12-01 18:50:13 +00:00
|
|
|
Peername: id.Peername,
|
2017-12-07 22:59:24 +00:00
|
|
|
//PublicKey: pkey,
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToID converts an IDSerial object to ID.
|
|
|
|
// It will ignore any errors when parsing the fields.
|
|
|
|
func (ids IDSerial) ToID() ID {
|
|
|
|
id := ID{}
|
2017-12-07 22:59:24 +00:00
|
|
|
p, err := peer.IDB58Decode(ids.ID)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(ids.ID, err)
|
2017-12-07 22:59:24 +00:00
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
id.ID = p
|
|
|
|
|
|
|
|
//if pkey, err := crypto.UnmarshalPublicKey(ids.PublicKey); err == nil {
|
|
|
|
// id.PublicKey = pkey
|
|
|
|
//}
|
|
|
|
|
|
|
|
id.Addresses = ids.Addresses.ToMultiaddrs()
|
2017-12-07 22:59:24 +00:00
|
|
|
id.ClusterPeers = StringsToPeers(ids.ClusterPeers)
|
2017-11-10 15:09:45 +00:00
|
|
|
id.ClusterPeersAddresses = ids.ClusterPeersAddresses.ToMultiaddrs()
|
2017-02-08 17:04:08 +00:00
|
|
|
id.Version = ids.Version
|
|
|
|
id.Commit = ids.Commit
|
|
|
|
id.RPCProtocolVersion = protocol.ID(ids.RPCProtocolVersion)
|
|
|
|
id.Error = ids.Error
|
|
|
|
id.IPFS = ids.IPFS.ToIPFSID()
|
2017-12-01 18:50:13 +00:00
|
|
|
id.Peername = ids.Peername
|
2017-02-08 17:04:08 +00:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiaddrSerial is a Multiaddress in a serializable form
|
|
|
|
type MultiaddrSerial string
|
|
|
|
|
|
|
|
// MultiaddrsSerial is an array of Multiaddresses in serializable form
|
|
|
|
type MultiaddrsSerial []MultiaddrSerial
|
|
|
|
|
|
|
|
// MultiaddrToSerial converts a Multiaddress to its serializable form
|
|
|
|
func MultiaddrToSerial(addr ma.Multiaddr) MultiaddrSerial {
|
2017-12-07 22:59:24 +00:00
|
|
|
if addr != nil {
|
|
|
|
return MultiaddrSerial(addr.String())
|
|
|
|
}
|
|
|
|
return ""
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToMultiaddr converts a serializable Multiaddress to its original type.
|
|
|
|
// All errors are ignored.
|
|
|
|
func (addrS MultiaddrSerial) ToMultiaddr() ma.Multiaddr {
|
2017-12-07 22:59:24 +00:00
|
|
|
str := string(addrS)
|
|
|
|
a, err := ma.NewMultiaddr(str)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(str, err)
|
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiaddrsToSerial converts a slice of Multiaddresses to its
|
|
|
|
// serializable form.
|
|
|
|
func MultiaddrsToSerial(addrs []ma.Multiaddr) MultiaddrsSerial {
|
|
|
|
addrsS := make([]MultiaddrSerial, len(addrs), len(addrs))
|
|
|
|
for i, a := range addrs {
|
2017-12-07 22:59:24 +00:00
|
|
|
if a != nil {
|
|
|
|
addrsS[i] = MultiaddrToSerial(a)
|
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
return addrsS
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMultiaddrs converts MultiaddrsSerial back to a slice of Multiaddresses
|
|
|
|
func (addrsS MultiaddrsSerial) ToMultiaddrs() []ma.Multiaddr {
|
|
|
|
addrs := make([]ma.Multiaddr, len(addrsS), len(addrsS))
|
|
|
|
for i, addrS := range addrsS {
|
|
|
|
addrs[i] = addrS.ToMultiaddr()
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
// Pin is an argument that carries a Cid. It may carry more things in the
|
2017-02-08 17:04:08 +00:00
|
|
|
// future.
|
2017-03-08 15:57:27 +00:00
|
|
|
type Pin struct {
|
2018-01-12 17:04:46 +00:00
|
|
|
Cid *cid.Cid
|
|
|
|
Name string
|
|
|
|
Allocations []peer.ID
|
|
|
|
ReplicationFactorMin int
|
|
|
|
ReplicationFactorMax int
|
2018-03-01 19:52:10 +00:00
|
|
|
Recursive bool
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:52:10 +00:00
|
|
|
// PinCid is a shorcut to create a Pin only with a Cid. Default is for pin to
|
|
|
|
// be recursive
|
2017-03-08 15:57:27 +00:00
|
|
|
func PinCid(c *cid.Cid) Pin {
|
|
|
|
return Pin{
|
2018-01-16 10:21:34 +00:00
|
|
|
Cid: c,
|
|
|
|
Allocations: []peer.ID{},
|
2018-03-01 19:52:10 +00:00
|
|
|
Recursive: true,
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
// PinSerial is a serializable version of Pin
|
|
|
|
type PinSerial struct {
|
2018-01-12 17:04:46 +00:00
|
|
|
Cid string `json:"cid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Allocations []string `json:"allocations"`
|
|
|
|
ReplicationFactorMin int `json:"replication_factor_min"`
|
|
|
|
ReplicationFactorMax int `json:"replication_factor_max"`
|
2018-03-01 19:52:10 +00:00
|
|
|
Recursive bool `json:"recursive"`
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
// ToSerial converts a Pin to PinSerial.
|
|
|
|
func (pin Pin) ToSerial() PinSerial {
|
2017-12-07 22:59:24 +00:00
|
|
|
c := ""
|
|
|
|
if pin.Cid != nil {
|
|
|
|
c = pin.Cid.String()
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 00:09:52 +00:00
|
|
|
n := pin.Name
|
|
|
|
allocs := PeersToStrings(pin.Allocations)
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
return PinSerial{
|
2018-01-12 17:04:46 +00:00
|
|
|
Cid: c,
|
|
|
|
Name: n,
|
|
|
|
Allocations: allocs,
|
|
|
|
ReplicationFactorMin: pin.ReplicationFactorMin,
|
|
|
|
ReplicationFactorMax: pin.ReplicationFactorMax,
|
2018-03-01 19:52:10 +00:00
|
|
|
Recursive: pin.Recursive,
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 10:21:34 +00:00
|
|
|
// Equals checks if two pins are the same (with the same allocations).
|
|
|
|
// If allocations are the same but in different order, they are still
|
|
|
|
// considered equivalent.
|
|
|
|
func (pin Pin) Equals(pin2 Pin) bool {
|
|
|
|
pin1s := pin.ToSerial()
|
|
|
|
pin2s := pin2.ToSerial()
|
|
|
|
|
|
|
|
if pin1s.Cid != pin2s.Cid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pin1s.Name != pin2s.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-01 19:52:10 +00:00
|
|
|
if pin1s.Recursive != pin2s.Recursive {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-16 10:21:34 +00:00
|
|
|
sort.Strings(pin1s.Allocations)
|
|
|
|
sort.Strings(pin2s.Allocations)
|
|
|
|
|
|
|
|
if strings.Join(pin1s.Allocations, ",") != strings.Join(pin2s.Allocations, ",") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pin1s.ReplicationFactorMax != pin2s.ReplicationFactorMax {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pin1s.ReplicationFactorMin != pin2s.ReplicationFactorMin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
// ToPin converts a PinSerial to its native form.
|
|
|
|
func (pins PinSerial) ToPin() Pin {
|
2017-12-07 22:59:24 +00:00
|
|
|
c, err := cid.Decode(pins.Cid)
|
|
|
|
if err != nil {
|
2018-03-28 17:16:49 +00:00
|
|
|
logger.Debug(pins.Cid, err)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
2017-03-08 17:28:43 +00:00
|
|
|
|
2017-03-08 15:57:27 +00:00
|
|
|
return Pin{
|
2018-01-12 17:04:46 +00:00
|
|
|
Cid: c,
|
|
|
|
Name: pins.Name,
|
|
|
|
Allocations: StringsToPeers(pins.Allocations),
|
|
|
|
ReplicationFactorMin: pins.ReplicationFactorMin,
|
|
|
|
ReplicationFactorMax: pins.ReplicationFactorMax,
|
2018-03-01 19:52:10 +00:00
|
|
|
Recursive: pins.Recursive,
|
2017-02-08 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
// Metric transports information about a peer.ID. It is used to decide
|
|
|
|
// pin allocations by a PinAllocator. IPFS cluster is agnostic to
|
|
|
|
// the Value, which should be interpreted by the PinAllocator.
|
|
|
|
type Metric struct {
|
|
|
|
Name string
|
|
|
|
Peer peer.ID // filled-in by Cluster.
|
|
|
|
Value string
|
2018-04-05 14:09:41 +00:00
|
|
|
Expire int64 // UnixNano
|
|
|
|
Valid bool // if the metric is not valid it will be discarded
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:50:03 +00:00
|
|
|
// SetTTL sets Metric to expire after the given time.Duration
|
|
|
|
func (m *Metric) SetTTL(d time.Duration) {
|
Issue #162: Rework configuration format
The following commit reimplements ipfs-cluster configuration under
the following premises:
* Each component is initialized with a configuration object
defined by its module
* Each component decides how the JSON representation of its
configuration looks like
* Each component parses and validates its own configuration
* Each component exposes its own defaults
* Component configurations are make the sections of a
central JSON configuration file (which replaces the current
JSON format)
* Component configurations implement a common interface
(config.ComponentConfig) with a set of common operations
* The central configuration file is managed by a
config.ConfigManager which:
* Registers ComponentConfigs
* Assigns the correspondent sections from the JSON file to each
component and delegates the parsing
* Delegates the JSON generation for each section
* Can be notified when the configuration is updated and must be
saved to disk
The new service.json would then look as follows:
```json
{
"cluster": {
"id": "QmTVW8NoRxC5wBhV7WtAYtRn7itipEESfozWN5KmXUQnk2",
"private_key": "<...>",
"secret": "00224102ae6aaf94f2606abf69a0e278251ecc1d64815b617ff19d6d2841f786",
"peers": [],
"bootstrap": [],
"leave_on_shutdown": false,
"listen_multiaddress": "/ip4/0.0.0.0/tcp/9096",
"state_sync_interval": "1m0s",
"ipfs_sync_interval": "2m10s",
"replication_factor": -1,
"monitor_ping_interval": "15s"
},
"consensus": {
"raft": {
"heartbeat_timeout": "1s",
"election_timeout": "1s",
"commit_timeout": "50ms",
"max_append_entries": 64,
"trailing_logs": 10240,
"snapshot_interval": "2m0s",
"snapshot_threshold": 8192,
"leader_lease_timeout": "500ms"
}
},
"api": {
"restapi": {
"listen_multiaddress": "/ip4/127.0.0.1/tcp/9094",
"read_timeout": "30s",
"read_header_timeout": "5s",
"write_timeout": "1m0s",
"idle_timeout": "2m0s"
}
},
"ipfs_connector": {
"ipfshttp": {
"proxy_listen_multiaddress": "/ip4/127.0.0.1/tcp/9095",
"node_multiaddress": "/ip4/127.0.0.1/tcp/5001",
"connect_swarms_delay": "7s",
"proxy_read_timeout": "10m0s",
"proxy_read_header_timeout": "5s",
"proxy_write_timeout": "10m0s",
"proxy_idle_timeout": "1m0s"
}
},
"monitor": {
"monbasic": {
"check_interval": "15s"
}
},
"informer": {
"disk": {
"metric_ttl": "30s",
"metric_type": "freespace"
},
"numpin": {
"metric_ttl": "10s"
}
}
}
```
This new format aims to be easily extensible per component. As such,
it already surfaces quite a few new options which were hardcoded
before.
Additionally, since Go API have changed, some redundant methods have been
removed and small refactoring has happened to take advantage of the new
way.
License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2017-10-11 18:23:03 +00:00
|
|
|
exp := time.Now().Add(d)
|
2018-04-05 14:09:41 +00:00
|
|
|
m.Expire = exp.UnixNano()
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTTL returns the time left before the Metric expires
|
|
|
|
func (m *Metric) GetTTL() time.Duration {
|
2018-04-05 14:09:41 +00:00
|
|
|
expDate := time.Unix(0, m.Expire)
|
|
|
|
return expDate.Sub(time.Now())
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expired returns if the Metric has expired
|
|
|
|
func (m *Metric) Expired() bool {
|
2018-04-05 14:09:41 +00:00
|
|
|
expDate := time.Unix(0, m.Expire)
|
|
|
|
return time.Now().After(expDate)
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discard returns if the metric not valid or has expired
|
|
|
|
func (m *Metric) Discard() bool {
|
|
|
|
return !m.Valid || m.Expired()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alert carries alerting information about a peer. WIP.
|
|
|
|
type Alert struct {
|
|
|
|
Peer peer.ID
|
|
|
|
MetricName string
|
|
|
|
}
|
2017-03-28 15:48:26 +00:00
|
|
|
|
|
|
|
// Error can be used by APIs to return errors.
|
|
|
|
type Error struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the error interface and returns the error's message.
|
2017-12-08 14:39:30 +00:00
|
|
|
func (e *Error) Error() string {
|
|
|
|
return fmt.Sprintf("%s (%d)", e.Message, e.Code)
|
2017-03-28 15:48:26 +00:00
|
|
|
}
|