dgraph-io/dgraph · error

while parsing KV: %+v, got error: %v

Error message

while parsing KV: %+v, got error: %v

What it means

populateKeyValues is invoked by applyCommitted when a moved predicate's KVs are applied on the destination group. After writing the batch, it parses the first KV's key with x.Parse to load the predicate's schema. If that key cannot be parsed as a valid Dgraph key, this error is returned, indicating corrupt or out-of-protocol data in the moved KV stream.

Source

Thrown at worker/predicate_move.go:55

	emptyPayload      = api.Payload{}
)

// size of kvs won't be too big, we would take care before proposing.
func populateKeyValues(ctx context.Context, kvs []*bpb.KV) error {
	glog.Infof("Writing %d keys\n", len(kvs))
	if len(kvs) == 0 {
		return nil
	}
	writer := posting.NewTxnWriter(pstore)
	if err := writer.Write(&bpb.KVList{Kv: kvs}); err != nil {
		return err
	}
	if err := writer.Flush(); err != nil {
		return err
	}
	pk, err := x.Parse(kvs[0].Key)
	if err != nil {
		return errors.Errorf("while parsing KV: %+v, got error: %v", kvs[0], err)
	}
	return schema.Load(pk.Attr)
}

func batchAndProposeKeyValues(ctx context.Context, kvs chan *pb.KVS) error {
	glog.Infoln("Receiving predicate. Batching and proposing key values")
	n := groups().Node
	proposal := &pb.Proposal{}
	size := 0
	var pk x.ParsedKey

	for kvPayload := range kvs {
		buf := z.NewBufferSlice(kvPayload.GetData())
		err := buf.SliceIterate(func(s []byte) error {
			kv := &bpb.KV{}
			x.Check(proto.Unmarshal(s, kv))
			if len(pk.Attr) == 0 {
				// This only happens once.

View on GitHub (pinned to 759e242be6)

Solutions

  1. Retry the predicate move after verifying all Alphas run the same Dgraph version.
  2. Inspect the failing KV printed in the error to identify the malformed key prefix.
  3. If corruption is suspected, run `dgraph debug` on the source posting directory and restore the affected predicate from backup.
  4. Re-move the tablet from a healthy source replica (another group member with correct data).
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := x.Parse(kvs[0].Key); err != nil {
	return errors.Errorf("refusing to apply corrupt KV: %v", err)
}

Type guard

func validDgraphKey(key []byte) bool {
	_, err := x.Parse(key)
	return err == nil
}

Try / catch

if err := applyCommitted(...); err != nil && strings.Contains(err.Error(), "while parsing KV") {
	// abort move, verify Alpha versions, restore predicate from backup
}

Prevention

When it happens

Trigger: applyCommitted -> populateKeyValues receives a KV list whose first key fails x.Parse: key format is invalid (wrong byte prefix/length), data written by an incompatible Dgraph version, or corrupted bytes in the posting store during a predicate move.

Common situations: Rolling upgrades where source and destination Alphas run incompatible versions during a tablet move; bit-rot or manual tampering with the postings directory; custom tooling injecting malformed KVs into pstore.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/ea805d14748f2817. Report an issue: GitHub.