// mergeTrigger is called on a successful commit of an AdminMerge // transaction. It recomputes stats for the receiving range. func (r *Range) mergeTrigger(batch engine.Engine, merge *proto.MergeTrigger) error { if !bytes.Equal(r.Desc().StartKey, merge.UpdatedDesc.StartKey) { return util.Errorf("range and updated range start keys do not match: %s != %s", r.Desc().StartKey, merge.UpdatedDesc.StartKey) } if !r.Desc().EndKey.Less(merge.UpdatedDesc.EndKey) { return util.Errorf("range end key is not less than the post merge end key: %s >= %s", r.Desc().EndKey, merge.UpdatedDesc.EndKey) } if merge.SubsumedRaftID <= 0 { return util.Errorf("subsumed raft ID must be provided: %d", merge.SubsumedRaftID) } // Copy the subsumed range's response cache to the subsuming one. if err := r.respCache.CopyFrom(batch, merge.SubsumedRaftID); err != nil { return util.Errorf("unable to copy response cache to new split range: %s", err) } // Compute stats for updated range. now := r.rm.Clock().Timestamp() iter := newRangeDataIterator(&merge.UpdatedDesc, batch) ms, err := engine.MVCCComputeStats(iter, now.WallTime) iter.Close() if err != nil { return util.Errorf("unable to compute stats for the range after merge: %s", err) } if err = r.stats.SetMVCCStats(batch, ms); err != nil { return util.Errorf("unable to write MVCC stats: %s", err) } // Clear the timestamp cache. In the case that this replica and the // subsumed replica each held their respective leader leases, we // could merge the timestamp caches for efficiency. But it's unlikely // and not worth the extra logic and potential for error. r.Lock() r.tsCache.Clear(r.rm.Clock()) r.Unlock() batch.Defer(func() { if err := r.rm.MergeRange(r, merge.UpdatedDesc.EndKey, merge.SubsumedRaftID); err != nil { // Our in-memory state has diverged from the on-disk state. log.Fatalf("failed to update store after merging range: %s", err) } }) return nil }
// splitTrigger is called on a successful commit of an AdminSplit // transaction. It copies the response cache for the new range and // recomputes stats for both the existing, updated range and the new // range. func (r *Range) splitTrigger(batch engine.Engine, split *proto.SplitTrigger) error { if !bytes.Equal(r.Desc().StartKey, split.UpdatedDesc.StartKey) || !bytes.Equal(r.Desc().EndKey, split.NewDesc.EndKey) { return util.Errorf("range does not match splits: (%s-%s) + (%s-%s) != %s", split.UpdatedDesc.StartKey, split.UpdatedDesc.EndKey, split.NewDesc.StartKey, split.NewDesc.EndKey, r) } // Copy the GC metadata. gcMeta, err := r.GetGCMetadata() if err != nil { return util.Errorf("unable to fetch GC metadata: %s", err) } if err := engine.MVCCPutProto(batch, nil, keys.RangeGCMetadataKey(split.NewDesc.RaftID), proto.ZeroTimestamp, nil, gcMeta); err != nil { return util.Errorf("unable to copy GC metadata: %s", err) } // Copy the last verification timestamp. verifyTS, err := r.GetLastVerificationTimestamp() if err != nil { return util.Errorf("unable to fetch last verification timestamp: %s", err) } if err := engine.MVCCPutProto(batch, nil, keys.RangeLastVerificationTimestampKey(split.NewDesc.RaftID), proto.ZeroTimestamp, nil, &verifyTS); err != nil { return util.Errorf("unable to copy last verification timestamp: %s", err) } // Compute stats for updated range. now := r.rm.Clock().Timestamp() iter := newRangeDataIterator(&split.UpdatedDesc, batch) ms, err := engine.MVCCComputeStats(iter, now.WallTime) iter.Close() if err != nil { return util.Errorf("unable to compute stats for updated range after split: %s", err) } if err := r.stats.SetMVCCStats(batch, ms); err != nil { return util.Errorf("unable to write MVCC stats: %s", err) } // Initialize the new range's response cache by copying the original's. if err = r.respCache.CopyInto(batch, split.NewDesc.RaftID); err != nil { return util.Errorf("unable to copy response cache to new split range: %s", err) } // Add the new split range to the store. This step atomically // updates the EndKey of the updated range and also adds the // new range to the store's range map. newRng, err := NewRange(&split.NewDesc, r.rm) if err != nil { return err } // Compute stats for new range. iter = newRangeDataIterator(&split.NewDesc, batch) ms, err = engine.MVCCComputeStats(iter, now.WallTime) iter.Close() if err != nil { return util.Errorf("unable to compute stats for new range after split: %s", err) } if err = newRng.stats.SetMVCCStats(batch, ms); err != nil { return util.Errorf("unable to write MVCC stats: %s", err) } // Copy the timestamp cache into the new range. r.Lock() r.tsCache.MergeInto(newRng.tsCache, true /* clear */) r.Unlock() batch.Defer(func() { if err := r.rm.SplitRange(r, newRng); err != nil { // Our in-memory state has diverged from the on-disk state. log.Fatalf("failed to update Store after split: %s", err) } }) return nil }