Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add schemaOnly parameter when applying raft log entries and ensure we reloadDB on catching up #4897

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions cluster/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (db *localDB) SetIndexer(idx Indexer) {
db.Schema.shardReader = idx
}

func (db *localDB) AddClass(cmd *command.ApplyRequest, nodeID string) error {
func (db *localDB) AddClass(cmd *command.ApplyRequest, nodeID string, schemaOnly bool) error {
req := command.AddClassRequest{}
if err := json.Unmarshal(cmd.SubCommand, &req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -57,12 +57,13 @@ func (db *localDB) AddClass(cmd *command.ApplyRequest, nodeID string) error {
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.addClass(req.Class, req.State, cmd.Version) },
updateStore: func() error { return db.store.AddClass(req) },
schemaOnly: schemaOnly,
triggerSchemaCallback: true,
},
)
}

func (db *localDB) RestoreClass(cmd *command.ApplyRequest, nodeID string) error {
func (db *localDB) RestoreClass(cmd *command.ApplyRequest, nodeID string, schemaOnly bool) error {
req := command.AddClassRequest{}
if err := json.Unmarshal(cmd.SubCommand, &req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -86,14 +87,15 @@ func (db *localDB) RestoreClass(cmd *command.ApplyRequest, nodeID string) error
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.addClass(req.Class, req.State, cmd.Version) },
updateStore: func() error { return db.store.AddClass(req) },
schemaOnly: schemaOnly,
triggerSchemaCallback: true,
},
)
}

// UpdateClass modifies the vectors and inverted indexes associated with a class
// Other class properties are handled by separate functions
func (db *localDB) UpdateClass(cmd *command.ApplyRequest, nodeID string) error {
func (db *localDB) UpdateClass(cmd *command.ApplyRequest, nodeID string, schemaOnly bool) error {
req := command.UpdateClassRequest{}
if err := json.Unmarshal(cmd.SubCommand, &req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand Down Expand Up @@ -128,23 +130,25 @@ func (db *localDB) UpdateClass(cmd *command.ApplyRequest, nodeID string) error {
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.updateClass(req.Class.Class, update) },
updateStore: func() error { return db.store.UpdateClass(req) },
schemaOnly: schemaOnly,
triggerSchemaCallback: true,
},
)
}

func (db *localDB) DeleteClass(cmd *command.ApplyRequest) error {
func (db *localDB) DeleteClass(cmd *command.ApplyRequest, schemaOnly bool) error {
return db.apply(
applyOp{
op: cmd.GetType().String(),
updateSchema: func() error { db.Schema.deleteClass(cmd.Class); return nil },
updateStore: func() error { return db.store.DeleteClass(cmd.Class) },
schemaOnly: schemaOnly,
triggerSchemaCallback: true,
},
)
}

func (db *localDB) AddProperty(cmd *command.ApplyRequest) error {
func (db *localDB) AddProperty(cmd *command.ApplyRequest, schemaOnly bool) error {
req := command.AddPropertyRequest{}
if err := json.Unmarshal(cmd.SubCommand, &req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -158,12 +162,13 @@ func (db *localDB) AddProperty(cmd *command.ApplyRequest) error {
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.addProperty(cmd.Class, cmd.Version, req.Properties...) },
updateStore: func() error { return db.store.AddProperty(cmd.Class, req) },
schemaOnly: schemaOnly,
triggerSchemaCallback: true,
},
)
}

func (db *localDB) UpdateShardStatus(cmd *command.ApplyRequest) error {
func (db *localDB) UpdateShardStatus(cmd *command.ApplyRequest, schemaOnly bool) error {
req := command.UpdateShardStatusRequest{}
if err := json.Unmarshal(cmd.SubCommand, &req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -174,11 +179,12 @@ func (db *localDB) UpdateShardStatus(cmd *command.ApplyRequest) error {
op: cmd.GetType().String(),
updateSchema: func() error { return nil },
updateStore: func() error { return db.store.UpdateShardStatus(&req) },
schemaOnly: schemaOnly,
},
)
}

func (db *localDB) AddTenants(cmd *command.ApplyRequest) error {
func (db *localDB) AddTenants(cmd *command.ApplyRequest, schemaOnly bool) error {
req := &command.AddTenantsRequest{}
if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -189,11 +195,12 @@ func (db *localDB) AddTenants(cmd *command.ApplyRequest) error {
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.addTenants(cmd.Class, cmd.Version, req) },
updateStore: func() error { return db.store.AddTenants(cmd.Class, req) },
schemaOnly: schemaOnly,
},
)
}

func (db *localDB) UpdateTenants(cmd *command.ApplyRequest) (n int, err error) {
func (db *localDB) UpdateTenants(cmd *command.ApplyRequest, schemaOnly bool) (n int, err error) {
req := &command.UpdateTenantsRequest{}
if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
return 0, fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -204,11 +211,12 @@ func (db *localDB) UpdateTenants(cmd *command.ApplyRequest) (n int, err error) {
op: cmd.GetType().String(),
updateSchema: func() error { n, err = db.Schema.updateTenants(cmd.Class, cmd.Version, req); return err },
updateStore: func() error { return db.store.UpdateTenants(cmd.Class, req) },
schemaOnly: schemaOnly,
},
)
}

func (db *localDB) DeleteTenants(cmd *command.ApplyRequest) error {
func (db *localDB) DeleteTenants(cmd *command.ApplyRequest, schemaOnly bool) error {
req := &command.DeleteTenantsRequest{}
if err := gproto.Unmarshal(cmd.SubCommand, req); err != nil {
return fmt.Errorf("%w: %w", errBadRequest, err)
Expand All @@ -219,6 +227,7 @@ func (db *localDB) DeleteTenants(cmd *command.ApplyRequest) error {
op: cmd.GetType().String(),
updateSchema: func() error { return db.Schema.deleteTenants(cmd.Class, cmd.Version, req) },
updateStore: func() error { return db.store.DeleteTenants(cmd.Class, req) },
schemaOnly: schemaOnly,
},
)
}
Expand All @@ -238,6 +247,7 @@ type applyOp struct {
op string
updateSchema func() error
updateStore func() error
schemaOnly bool
triggerSchemaCallback bool
}

Expand Down Expand Up @@ -265,8 +275,10 @@ func (db *localDB) apply(op applyOp) error {
return fmt.Errorf("%w: %s: %w", errSchema, op.op, err)
}

if err := op.updateStore(); err != nil {
return fmt.Errorf("%w: %s: %w", errDB, op.op, err)
if !op.schemaOnly {
if err := op.updateStore(); err != nil {
return fmt.Errorf("%w: %s: %w", errDB, op.op, err)
}
}

// Always trigger the schema callback last
Expand Down
53 changes: 38 additions & 15 deletions cluster/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,29 @@ func (st *Store) Apply(l *raft.Log) interface{} {
panic("error proto un-marshalling log data")
}

// schemaOnly is necessary so that on restart when we are re-applying RAFT log entries to our in-memory schema we
// don't update the database. This can lead to dataloss for example if we drop then re-add a class.
// If we don't have any last applied index on start, schema only is always false.
schemaOnly := st.lastAppliedIndexOnStart != 0 && l.Index <= st.lastAppliedIndexOnStart
defer func() {
// If we have an applied index from the previous store (i.e from disk). Then reload the DB once we catch up as
// that means we're done doing schema only.
if st.lastAppliedIndexOnStart != 0 && l.Index == st.lastAppliedIndexOnStart {
st.log.WithFields(logrus.Fields{
"log_type": l.Type,
"log_name": l.Type.String(),
"log_index": l.Index,
"last_store_log_applied_index": st.lastAppliedIndexOnStart,
}).Debug("reloading local DB as RAFT and local DB are now caught up")
cs := make([]command.UpdateClassRequest, len(st.db.Schema.Classes))
i := 0
for _, v := range st.db.Schema.Classes {
cs[i] = command.UpdateClassRequest{Class: &v.Class, State: &v.Sharding}
i++
}
reyreaud-l marked this conversation as resolved.
Show resolved Hide resolved
st.db.store.ReloadLocalDB(context.Background(), cs)
}

st.lastAppliedIndex.Store(l.Index)
if ret.Error != nil {
st.log.WithFields(logrus.Fields{
Expand All @@ -654,41 +676,42 @@ func (st *Store) Apply(l *raft.Log) interface{} {

cmd.Version = l.Index
st.log.WithFields(logrus.Fields{
"log_type": l.Type,
"log_name": l.Type.String(),
"log_index": l.Index,
"cmd_type": cmd.Type,
"cmd_type_name": cmd.Type.String(),
"cmd_class": cmd.Class,
"log_type": l.Type,
"log_name": l.Type.String(),
"log_index": l.Index,
"cmd_type": cmd.Type,
"cmd_type_name": cmd.Type.String(),
"cmd_class": cmd.Class,
"cmd_schema_only": schemaOnly,
}).Debug("server.apply")
switch cmd.Type {

case api.ApplyRequest_TYPE_ADD_CLASS:
ret.Error = st.db.AddClass(&cmd, st.nodeID)
ret.Error = st.db.AddClass(&cmd, st.nodeID, schemaOnly)

case api.ApplyRequest_TYPE_RESTORE_CLASS:
ret.Error = st.db.RestoreClass(&cmd, st.nodeID)
ret.Error = st.db.RestoreClass(&cmd, st.nodeID, schemaOnly)

case api.ApplyRequest_TYPE_UPDATE_CLASS:
ret.Error = st.db.UpdateClass(&cmd, st.nodeID)
ret.Error = st.db.UpdateClass(&cmd, st.nodeID, schemaOnly)

case api.ApplyRequest_TYPE_DELETE_CLASS:
ret.Error = st.db.DeleteClass(&cmd)
ret.Error = st.db.DeleteClass(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_ADD_PROPERTY:
ret.Error = st.db.AddProperty(&cmd)
ret.Error = st.db.AddProperty(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_UPDATE_SHARD_STATUS:
ret.Error = st.db.UpdateShardStatus(&cmd)
ret.Error = st.db.UpdateShardStatus(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_ADD_TENANT:
ret.Error = st.db.AddTenants(&cmd)
ret.Error = st.db.AddTenants(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_UPDATE_TENANT:
ret.Data, ret.Error = st.db.UpdateTenants(&cmd)
ret.Data, ret.Error = st.db.UpdateTenants(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_DELETE_TENANT:
ret.Error = st.db.DeleteTenants(&cmd)
ret.Error = st.db.DeleteTenants(&cmd, schemaOnly)

case api.ApplyRequest_TYPE_STORE_SCHEMA_V1:
ret.Error = st.StoreSchemaV1()
Expand Down
Loading