ipfs-cluster/state/interface.go
Hector Sanjuan acbd7fda60 Consensus: add new "crdt" consensus component
This adds a new "crdt" consensus component using go-ds-crdt.

This implies several refactors to fully make cluster consensus-component
independent:

* Delete mapstate and fully adopt dsstate (after people have migrated).
* Return errors from state methods rather than ignoring them.
* Add a new "datastore" modules so that we can configure datastores in the
   main configuration like other components.
* Let the consensus components fully define the "state.State". Thus, they do
not receive the state, they receive the storage where we put the state (a
go-datastore).
* Allow to customize how the monitor component obtains Peers() (the current
  peerset), including avoiding using the current peerset. At the moment the
  crdt consensus uses the monitoring component to define the current peerset.
  Therefore the monitor component cannot rely on the consensus component to
  produce a peerset.
* Re-factor/re-implementation of "ipfs-cluster-service state"
  operations. Includes the dissapearance of the "migrate" one.

The CRDT consensus component defines creates a crdt-datastore (with ipfs-lite)
and uses it to intitialize a dssate. Thus the crdt-store is elegantly
wrapped. Any modifications to the state get automatically replicated to other
peers. We store all the CRDT DAG blocks in the local datastore.

The consensus components only expose a ReadOnly state, as any modifications to
the shared state should happen through them.

DHT and PubSub facilities must now be created outside of Cluster and passed in
so they can be re-used by different components.
2019-04-17 19:14:26 +02:00

60 lines
1.8 KiB
Go

// Package state holds the interface that any state implementation for
// IPFS Cluster must satisfy.
package state
// State represents the shared state of the cluster and it
import (
"context"
"errors"
"io"
"github.com/ipfs/ipfs-cluster/api"
cid "github.com/ipfs/go-cid"
)
// ErrNotFound should be returned when a pin is not part of the state.
var ErrNotFound = errors.New("pin is not part of the pinset")
// State is a wrapper to the Cluster shared state so that Pin objects can
// easily read, written and queried. The state can be marshaled and
// unmarshaled. Implementation should be thread-safe.
type State interface {
ReadOnly
WriteOnly
// Migrate restores the serialized format of an outdated state to the
// current version.
Migrate(ctx context.Context, r io.Reader) error
// Marshal serializes the state to a byte slice.
Marshal(io.Writer) error
// Unmarshal deserializes the state from marshaled bytes.
Unmarshal(io.Reader) error
// Commit writes any batched operations.
}
// ReadOnly represents a the read side of a State.
type ReadOnly interface {
// List lists all the pins in the state.
List(context.Context) ([]*api.Pin, error)
// Has returns true if the state is holding information for a Cid.
Has(context.Context, cid.Cid) (bool, error)
// Get returns the information attacthed to this pin, if any. If the
// pin is not part of the state, it should return ErrNotFound.
Get(context.Context, cid.Cid) (*api.Pin, error)
}
// WriteOnly represents the write side of a State.
type WriteOnly interface {
// Add adds a pin to the State
Add(context.Context, *api.Pin) error
// Rm removes a pin from the State.
Rm(context.Context, cid.Cid) error
}
// BatchingState represents a state which batches write operations.
type BatchingState interface {
State
// Commit writes any batched operations.
Commit(context.Context) error
}