Migration to state specifying recursive attribute

License: MIT
Signed-off-by: Wyatt Daviau <wdaviau@cs.stanford.edu>
This commit is contained in:
Wyatt Daviau 2018-03-09 15:17:48 -05:00
parent 1b7b9185e2
commit bdc373de61
2 changed files with 53 additions and 7 deletions

View File

@ -19,7 +19,7 @@ import (
// Version is the map state Version. States with old versions should // Version is the map state Version. States with old versions should
// perform an upgrade before. // perform an upgrade before.
const Version = 3 const Version = 4
var logger = logging.Logger("mapstate") var logger = logging.Logger("mapstate")

View File

@ -21,6 +21,8 @@ type migrateable interface {
unmarshal([]byte) error unmarshal([]byte) error
} }
/* V1 */
type mapStateV1 struct { type mapStateV1 struct {
Version int Version int
PinMap map[string]struct{} PinMap map[string]struct{}
@ -47,6 +49,8 @@ func (st *mapStateV1) next() migrateable {
return &mst2 return &mst2
} }
/* V2 */
type pinSerialV2 struct { type pinSerialV2 struct {
Cid string `json:"cid"` Cid string `json:"cid"`
Name string `json:"name"` Name string `json:"name"`
@ -67,9 +71,9 @@ func (st *mapStateV2) unmarshal(bs []byte) error {
func (st *mapStateV2) next() migrateable { func (st *mapStateV2) next() migrateable {
var mst3 mapStateV3 var mst3 mapStateV3
mst3.PinMap = make(map[string]api.PinSerial) mst3.PinMap = make(map[string]pinSerialV3)
for k, v := range st.PinMap { for k, v := range st.PinMap {
mst3.PinMap[k] = api.PinSerial{ mst3.PinMap[k] = pinSerialV3{
Cid: v.Cid, Cid: v.Cid,
Name: v.Name, Name: v.Name,
Allocations: v.Allocations, Allocations: v.Allocations,
@ -80,8 +84,18 @@ func (st *mapStateV2) next() migrateable {
return &mst3 return &mst3
} }
/* V3 */
type pinSerialV3 struct {
Cid string `json:"cid"`
Name string `json:"name"`
Allocations []string `json:"allocations"`
ReplicationFactorMin int `json:"replication_factor_min"`
ReplicationFactorMax int `json:"replication_factor_max"`
}
type mapStateV3 struct { type mapStateV3 struct {
PinMap map[string]api.PinSerial PinMap map[string]pinSerialV3
Version int Version int
} }
@ -92,10 +106,39 @@ func (st *mapStateV3) unmarshal(bs []byte) error {
} }
func (st *mapStateV3) next() migrateable { func (st *mapStateV3) next() migrateable {
var mst4 mapStateV4
mst4.PinMap = make(map[string]api.PinSerial)
for k, v := range st.PinMap {
mst4.PinMap[k] = api.PinSerial{
Cid: v.Cid,
Name: v.Name,
Allocations: v.Allocations,
ReplicationFactorMin: v.ReplicationFactorMin,
ReplicationFactorMax: v.ReplicationFactorMax,
Recursive: true,
}
}
return &mst4
}
/* V4 */
type mapStateV4 struct {
PinMap map[string]api.PinSerial
Version int
}
func (st *mapStateV4) unmarshal(bs []byte) error {
buf := bytes.NewBuffer(bs)
dec := msgpack.Multicodec(msgpack.DefaultMsgpackHandle()).Decoder(buf)
return dec.Decode(st)
}
func (st *mapStateV4) next() migrateable {
return nil return nil
} }
func finalCopy(st *MapState, internal *mapStateV3) { func finalCopy(st *MapState, internal *mapStateV4) {
for k, v := range internal.PinMap { for k, v := range internal.PinMap {
st.PinMap[k] = v st.PinMap[k] = v
} }
@ -110,6 +153,9 @@ func (st *MapState) migrateFrom(version int, snap []byte) error {
case 2: case 2:
var mst2 mapStateV2 var mst2 mapStateV2
m = &mst2 m = &mst2
case 3:
var mst3 mapStateV3
m = &mst3
default: default:
return errors.New("version migration not supported") return errors.New("version migration not supported")
} }
@ -122,11 +168,11 @@ func (st *MapState) migrateFrom(version int, snap []byte) error {
for { for {
next = m.next() next = m.next()
if next == nil { if next == nil {
mst3, ok := m.(*mapStateV3) mst4, ok := m.(*mapStateV4)
if !ok { if !ok {
return errors.New("migration ended prematurely") return errors.New("migration ended prematurely")
} }
finalCopy(st, mst3) finalCopy(st, mst4)
return nil return nil
} }
m = next m = next