Skip to content

Commit

Permalink
PinInfo type: include Allocations, Origins, Metadata
Browse files Browse the repository at this point in the history
This will facilitate building outputs for the Pinning Services API, saving a
round trip to query the cluster State, since all the needed information
already comes from the PinTracker, which has already accessed the state.

Since the pintracker already included a state attribute (Name), we are simply
going down that path.
  • Loading branch information
hsanjuan committed Feb 1, 2022
1 parent ea624d5 commit 0787ffb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
33 changes: 27 additions & 6 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,12 @@ var ipfsPinStatus2TrackerStatusMap = map[IPFSPinStatus]TrackerStatus{
// GlobalPinInfo contains cluster-wide status information about a tracked Cid,
// indexed by cluster peer.
type GlobalPinInfo struct {
Cid cid.Cid `json:"cid" codec:"c"`
Name string `json:"name" codec:"n"`
Cid cid.Cid `json:"cid" codec:"c"`
Name string `json:"name" codec:"n"`
Allocations []peer.ID `json:"allocations" codec:"a,omitempty"`
Origins []Multiaddr `json:"origins" codec:"g,omitempty"`
Metadata map[string]string `json:"metadata" codec:"m,omitempty"`

// https://github.com/golang/go/issues/28827
// Peer IDs are of string Kind(). We can't use peer IDs here
// as Go ignores TextMarshaler.
Expand All @@ -270,9 +274,12 @@ func (gpi *GlobalPinInfo) String() string {

// Add adds a PinInfo object to a GlobalPinInfo
func (gpi *GlobalPinInfo) Add(pi *PinInfo) {
if !gpi.Cid.Defined() {
if !gpi.Cid.Defined() || !pi.Status.Match(TrackerStatusClusterError) {
gpi.Cid = pi.Cid
gpi.Name = pi.Name
gpi.Allocations = pi.Allocations
gpi.Origins = pi.Origins
gpi.Metadata = pi.Metadata
}

if gpi.PeerMap == nil {
Expand All @@ -282,6 +289,17 @@ func (gpi *GlobalPinInfo) Add(pi *PinInfo) {
gpi.PeerMap[peer.Encode(pi.Peer)] = &pi.PinInfoShort
}

// Matches returns true if one of the statuses in GlobalPinInfo matches
// the given filter.
func (gpi *GlobalPinInfo) Match(filter TrackerStatus) bool {
for _, pi := range gpi.PeerMap {
if pi.Status.Match(filter) {
return true
}
}
return false
}

// PinInfoShort is a subset of PinInfo which is embedded in GlobalPinInfo
// objects and does not carry redundant information as PinInfo would.
type PinInfoShort struct {
Expand All @@ -297,9 +315,12 @@ type PinInfoShort struct {
// PinInfo holds information about local pins. This is used by the Pin
// Trackers.
type PinInfo struct {
Cid cid.Cid `json:"cid" codec:"c"`
Name string `json:"name" codec:"m,omitempty"`
Peer peer.ID `json:"peer" codec:"p,omitempty"`
Cid cid.Cid `json:"cid" codec:"c"`
Name string `json:"name" codec:"m,omitempty"`
Peer peer.ID `json:"peer" codec:"p,omitempty"`
Allocations []peer.ID `json:"allocations" codec:"a,omitempty"`
Origins []Multiaddr `json:"origins" codec:"g,omitempty"`
Metadata map[string]string `json:"metadata" codec:"md,omitempty"`

PinInfoShort
}
Expand Down
33 changes: 21 additions & 12 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,17 +1749,20 @@ func (c *Cluster) getTrustedPeers(ctx context.Context, exclude peer.ID) ([]peer.
return trustedPeers, nil
}

func (c *Cluster) setTrackerStatus(gpin *api.GlobalPinInfo, h cid.Cid, peers []peer.ID, status api.TrackerStatus, name string, t time.Time) {
func (c *Cluster) setTrackerStatus(gpin *api.GlobalPinInfo, h cid.Cid, peers []peer.ID, status api.TrackerStatus, pin api.Pin, t time.Time) {
for _, p := range peers {
pv := pingValueFromMetric(c.monitor.LatestForPeer(c.ctx, pingMetricName, p))
peerName := pv.Peername
if peerName == "" {
peerName = p.String()
}
gpin.Add(&api.PinInfo{
Cid: h,
Name: name,
Peer: p,
Cid: h,
Name: pin.Name,
Allocations: pin.Allocations,
Origins: pin.Origins,
Metadata: pin.Metadata,
Peer: p,
PinInfoShort: api.PinInfoShort{
PeerName: peerName,
IPFS: pv.IPFSID,
Expand Down Expand Up @@ -1810,7 +1813,7 @@ func (c *Cluster) globalPinInfoCid(ctx context.Context, comp, method string, h c
h,
members,
api.TrackerStatusUnpinned,
"",
*api.PinCid(h),
timeNow,
)
return gpin, nil
Expand Down Expand Up @@ -1842,7 +1845,7 @@ func (c *Cluster) globalPinInfoCid(ctx context.Context, comp, method string, h c
}

// set status remote on un-allocated peers
c.setTrackerStatus(gpin, h, remote, api.TrackerStatusRemote, pin.Name, timeNow)
c.setTrackerStatus(gpin, h, remote, api.TrackerStatusRemote, *pin, timeNow)

lenDests := len(dests)
replies := make([]*api.PinInfo, lenDests)
Expand Down Expand Up @@ -1885,9 +1888,12 @@ func (c *Cluster) globalPinInfoCid(ctx context.Context, comp, method string, h c
peerName = dests[i].String()
}
gpin.Add(&api.PinInfo{
Cid: h,
Name: pin.Name,
Peer: dests[i],
Cid: h,
Name: pin.Name,
Peer: dests[i],
Allocations: pin.Allocations,
Origins: pin.Origins,
Metadata: pin.Metadata,
PinInfoShort: api.PinInfoShort{
PeerName: peerName,
IPFS: pv.IPFSID,
Expand Down Expand Up @@ -1981,9 +1987,12 @@ func (c *Cluster) globalPinInfoSlice(ctx context.Context, comp, method string, a

for c := range fullMap {
setPinInfo(&api.PinInfo{
Cid: c,
Name: "",
Peer: p,
Cid: c,
Name: "",
Peer: p,
Allocations: nil,
Origins: nil,
Metadata: nil,
PinInfoShort: api.PinInfoShort{
PeerName: peerName,
IPFS: pv.IPFSID,
Expand Down
25 changes: 19 additions & 6 deletions pintracker/stateless/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func (spt *Tracker) Status(ctx context.Context, c cid.Cid) *api.PinInfo {
pinInfo := &api.PinInfo{
Cid: c,
Peer: spt.peerID,
Name: "", // etc to be filled later
PinInfoShort: api.PinInfoShort{
PeerName: spt.peerName,
IPFS: spt.getIPFSID(ctx),
Expand Down Expand Up @@ -396,6 +397,9 @@ func (spt *Tracker) Status(ctx context.Context, c cid.Cid) *api.PinInfo {
// The pin IS in the state.
pinInfo.Name = gpin.Name
pinInfo.TS = gpin.Timestamp
pinInfo.Allocations = gpin.Allocations
pinInfo.Origins = gpin.Origins
pinInfo.Metadata = gpin.Metadata

// check if pin is a meta pin
if gpin.Type == api.MetaType {
Expand Down Expand Up @@ -536,9 +540,12 @@ func (spt *Tracker) ipfsStatusAll(ctx context.Context) (map[cid.Cid]*api.PinInfo
continue
}
p := &api.PinInfo{
Cid: c,
Name: "", // to be filled later
Peer: spt.peerID,
Cid: c,
Name: "", // to be filled later
Allocations: nil, // to be filled later
Origins: nil, // to be filled later
Metadata: nil, // to be filled later
Peer: spt.peerID,
PinInfoShort: api.PinInfoShort{
PeerName: spt.peerName,
IPFS: ipfsid,
Expand Down Expand Up @@ -601,9 +608,12 @@ func (spt *Tracker) localStatus(ctx context.Context, incExtra bool, filter api.T
ipfsInfo, pinnedInIpfs := localpis[p.Cid]
// base pinInfo object - status to be filled.
pinInfo := api.PinInfo{
Cid: p.Cid,
Name: p.Name,
Peer: spt.peerID,
Cid: p.Cid,
Name: p.Name,
Peer: spt.peerID,
Allocations: p.Allocations,
Origins: p.Origins,
Metadata: p.Metadata,
PinInfoShort: api.PinInfoShort{
PeerName: spt.peerName,
IPFS: ipfsid,
Expand All @@ -629,6 +639,9 @@ func (spt *Tracker) localStatus(ctx context.Context, incExtra bool, filter api.T
case pinnedInIpfs: // always false unless filter matches TrackerStatusPinnned
ipfsInfo.Name = p.Name
ipfsInfo.TS = p.Timestamp
ipfsInfo.Allocations = p.Allocations
ipfsInfo.Origins = p.Origins
ipfsInfo.Metadata = p.Metadata
pininfos[p.Cid] = ipfsInfo
default:
// report as UNEXPECTEDLY_UNPINNED for this peer.
Expand Down

0 comments on commit 0787ffb

Please sign in to comment.