acbd7fda60
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.
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
|
|
fslock "github.com/ipfs/go-fs-lock"
|
|
)
|
|
|
|
// lock logic heavily inspired by go-ipfs/repo/fsrepo/lock/lock.go
|
|
|
|
// The name of the file used for locking
|
|
const lockFileName = "cluster.lock"
|
|
|
|
var locker *lock
|
|
|
|
// lock helps to coordinate procees via a lock file
|
|
type lock struct {
|
|
lockCloser io.Closer
|
|
path string
|
|
}
|
|
|
|
func (l *lock) lock() {
|
|
if l.lockCloser != nil {
|
|
checkErr("", errors.New("cannot acquire lock twice"))
|
|
}
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
errMsg := "%s config hasn't been initialized. Please run '%s init'"
|
|
errMsg = fmt.Sprintf(errMsg, programName, programName)
|
|
checkErr("", errors.New(errMsg))
|
|
}
|
|
|
|
// set the lock file within this function
|
|
logger.Debug("checking lock")
|
|
lk, err := fslock.Lock(l.path, lockFileName)
|
|
if err != nil {
|
|
logger.Debug(err)
|
|
l.lockCloser = nil
|
|
errStr := "%s. If no other "
|
|
errStr += "%s process is running, remove %s, or make sure "
|
|
errStr += "that the config folder is writable for the user "
|
|
errStr += "running %s."
|
|
errStr = fmt.Sprintf(
|
|
errStr,
|
|
err,
|
|
programName,
|
|
path.Join(l.path, lockFileName),
|
|
programName,
|
|
)
|
|
checkErr("obtaining execution lock", errors.New(errStr))
|
|
}
|
|
logger.Debugf("%s execution lock acquired", programName)
|
|
l.lockCloser = lk
|
|
}
|
|
|
|
func (l *lock) tryUnlock() error {
|
|
// Noop in the uninitialized case
|
|
if l.lockCloser == nil {
|
|
logger.Debug("locking not initialized, unlock is noop")
|
|
return nil
|
|
}
|
|
err := l.lockCloser.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
logger.Debug("successfully released execution lock")
|
|
l.lockCloser = nil
|
|
return nil
|
|
}
|