Feat #277: Improve getCurretPin() call

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
This commit is contained in:
Hector Sanjuan 2018-01-16 12:00:21 +01:00
parent ae1afe3af8
commit a1ab106fcc
3 changed files with 13 additions and 7 deletions

View File

@ -44,7 +44,8 @@ import (
// it will return the current ones.
func (c *Cluster) allocate(hash *cid.Cid, rplMin, rplMax int, blacklist []peer.ID) ([]peer.ID, error) {
// Figure out who is holding the CID
currentAllocs := c.getCurrentPin(hash).Allocations
currentPin, _ := c.getCurrentPin(hash)
currentAllocs := currentPin.Allocations
metrics, err := c.getInformerMetrics()
if err != nil {
return nil, err
@ -83,12 +84,13 @@ func (c *Cluster) allocate(hash *cid.Cid, rplMin, rplMax int, blacklist []peer.I
// getCurrentPin returns the Pin object for h, if we can find one
// or builds an empty one.
func (c *Cluster) getCurrentPin(h *cid.Cid) api.Pin {
func (c *Cluster) getCurrentPin(h *cid.Cid) (api.Pin, bool) {
st, err := c.consensus.State()
if err != nil {
return api.PinCid(h)
return api.PinCid(h), false
}
return st.Get(h)
ok := st.Has(h)
return st.Get(h), ok
}
// getInformerMetrics returns the MonitorLastMetrics() for the

View File

@ -945,8 +945,8 @@ func (c *Cluster) Pins() []api.Pin {
// the item is successfully pinned. For that, use Status(). PinGet
// returns an error if the given Cid is not part of the global state.
func (c *Cluster) PinGet(h *cid.Cid) (api.Pin, error) {
pin := c.getCurrentPin(h)
if pin.ReplicationFactorMin == 0 && pin.ReplicationFactorMax == 0 {
pin, ok := c.getCurrentPin(h)
if !ok {
return pin, errors.New("cid is not part of the global state")
}
return pin, nil
@ -999,7 +999,7 @@ func (c *Cluster) pin(pin api.Pin, blacklist []peer.ID) (bool, error) {
pin.Allocations = allocs
}
if c.getCurrentPin(pin.Cid).Equals(pin) {
if curr, _ := c.getCurrentPin(pin.Cid); curr.Equals(pin) {
// skip pinning
logger.Debugf("pinning %s skipped: already correctly allocated", pin.Cid)
return false, nil

View File

@ -54,6 +54,10 @@ func (st *MapState) Rm(c *cid.Cid) error {
}
// Get returns Pin information for a CID.
// The returned object has its Cid and Allocations
// fields initialized, regardless of the
// presence of the provided Cid in the state.
// To check the presence, use MapState.Has(*cid.Cid).
func (st *MapState) Get(c *cid.Cid) api.Pin {
st.pinMux.RLock()
defer st.pinMux.RUnlock()