|
|
@ -247,16 +247,16 @@ func (s *Store) Open(enableBootstrap bool) error {
|
|
|
|
// there are no commands in the log, then this is the only opportunity to
|
|
|
|
// there are no commands in the log, then this is the only opportunity to
|
|
|
|
// create that on-disk database file before Raft initializes.
|
|
|
|
// create that on-disk database file before Raft initializes.
|
|
|
|
if !s.dbConf.Memory && !s.snapsExistOnOpen && s.lastCommandIdxOnOpen == 0 {
|
|
|
|
if !s.dbConf.Memory && !s.snapsExistOnOpen && s.lastCommandIdxOnOpen == 0 {
|
|
|
|
s.db, err = s.openOnDisk(nil)
|
|
|
|
s.db, err = s.createOnDisk(nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open on-disk database")
|
|
|
|
return fmt.Errorf("failed to create on-disk database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.onDiskCreated = true
|
|
|
|
s.onDiskCreated = true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// We need an in-memory database, at least for bootstrapping purposes.
|
|
|
|
// We need an in-memory database, at least for bootstrapping purposes.
|
|
|
|
s.db, err = s.openInMemory(nil)
|
|
|
|
s.db, err = s.createInMemory(nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open in-memory database")
|
|
|
|
return fmt.Errorf("failed to create in-memory database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -780,9 +780,9 @@ func (s *Store) Noop(id string) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// openInMemory returns an in-memory database. If b is non-nil, then the
|
|
|
|
// createInMemory returns an in-memory database. If b is non-nil, then the
|
|
|
|
// database will be initialized with the contents of b.
|
|
|
|
// database will be initialized with the contents of b.
|
|
|
|
func (s *Store) openInMemory(b []byte) (db *sql.DB, err error) {
|
|
|
|
func (s *Store) createInMemory(b []byte) (db *sql.DB, err error) {
|
|
|
|
if b == nil {
|
|
|
|
if b == nil {
|
|
|
|
db, err = sql.OpenInMemoryWithDSN(s.dbConf.DSN)
|
|
|
|
db, err = sql.OpenInMemoryWithDSN(s.dbConf.DSN)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -791,10 +791,10 @@ func (s *Store) openInMemory(b []byte) (db *sql.DB, err error) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// openOnDisk opens an on-disk database file at the Store's configured path. If
|
|
|
|
// createOnDisk opens an on-disk database file at the Store's configured path. If
|
|
|
|
// b is non-nil, any preexisting file will first be overwritten with those contents.
|
|
|
|
// b is non-nil, any preexisting file will first be overwritten with those contents.
|
|
|
|
// Otherwise any pre-existing file will be removed before the database is opened.
|
|
|
|
// Otherwise any pre-existing file will be removed before the database is opened.
|
|
|
|
func (s *Store) openOnDisk(b []byte) (*sql.DB, error) {
|
|
|
|
func (s *Store) createOnDisk(b []byte) (*sql.DB, error) {
|
|
|
|
if err := os.Remove(s.dbPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
if err := os.Remove(s.dbPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -906,7 +906,7 @@ func (s *Store) Apply(l *raft.Log) (e interface{}) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Open a new on-disk database.
|
|
|
|
// Open a new on-disk database.
|
|
|
|
s.db, err = s.openOnDisk(b)
|
|
|
|
s.db, err = s.createOnDisk(b)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
e = &fsmGenericResponse{error: fmt.Errorf("open on-disk failed: %s", err)}
|
|
|
|
e = &fsmGenericResponse{error: fmt.Errorf("open on-disk failed: %s", err)}
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -1074,7 +1074,7 @@ func (s *Store) Restore(rc io.ReadCloser) error {
|
|
|
|
// are no command entries in the log -- so Apply will not be called.
|
|
|
|
// are no command entries in the log -- so Apply will not be called.
|
|
|
|
// Therefore this is the last opportunity to create the on-disk database
|
|
|
|
// Therefore this is the last opportunity to create the on-disk database
|
|
|
|
// before Raft starts.
|
|
|
|
// before Raft starts.
|
|
|
|
db, err = s.openOnDisk(database)
|
|
|
|
db, err = s.createOnDisk(database)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open on-disk file during restore: %s", err)
|
|
|
|
return fmt.Errorf("open on-disk file during restore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1086,9 +1086,9 @@ func (s *Store) Restore(rc io.ReadCloser) error {
|
|
|
|
// command entries in the log. So by sticking with an in-memory database
|
|
|
|
// command entries in the log. So by sticking with an in-memory database
|
|
|
|
// those entries will be applied in the fastest possible manner. We will
|
|
|
|
// those entries will be applied in the fastest possible manner. We will
|
|
|
|
// defer creation of any database on disk until the Apply function.
|
|
|
|
// defer creation of any database on disk until the Apply function.
|
|
|
|
db, err = s.openInMemory(database)
|
|
|
|
db, err = s.createInMemory(database)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("openInMemory: %s", err)
|
|
|
|
return fmt.Errorf("createInMemory: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.db = db
|
|
|
|
s.db = db
|
|
|
|