Repository: tikv/tikv
File: components/compact-log-backup/src/compaction/exec.rs (Rust)

Code before:
        // partial ordering of two conflicting records.
        let partial_cmp = |wa: &WriteRef<'_>, wb: &WriteRef<'_>| {
            use WriteType::*;
            match (wa.write_type, wb.write_type) {
                // Rollback -> Collapsed with Put happens.
                // Should keep the Put.
                (Put, Rollback | Put) if wa.has_overlapped_rollback => Some(Ordering::Greater),
                (Rollback | Put, Put) if wb.has_overlapped_rollback => Some(Ordering::Less),

                // Rollback -> Protected Rollback.
                // Keep the protected one.
                // This was observered in some versions and shouldn't happen in normally.
                (Rollback, Rollback) if wa.is_protected() => Some(Ordering::Greater),
                (Rollback, Rollback) if wb.is_protected() => Some(Ordering::Less),

                // No comparing rule here.
                _ => None,
            }
        };

        sanity_check(&wa, &wb);
        let maybe_ord = partial_cmp(&wa, &wb);
        info!("resolve conflict result."; "conflict_id" => cid, "order" => ?maybe_ord);
        match maybe_ord {
            Some(Ordering::Greater) => std::mem::swap(a, b),
            None => panic!("cannot resolve the conflict {}", cid),

            Some(_) => {}
        }
    }

    fn update_checksum_diff(
        c: &Subcompaction,
        a: &mut Record,
        b: &mut Record,
        diff: &mut ChecksumDiff,
    ) {
        Self::resolve(c, a, b);

        diff.removed_key += 1;
        diff.decreaed_size += (a.key.len() + a.value.len()) as u64;
        let mut d = crc64fast::Digest::new();
        d.write(&a.key);
        d.write(&a.value);
        // When we remove even number of the same key-value pair, we don't need to
        // update the crc64xor checksum. Trick here: If a key-value pair appears
        // even times, that key will be eliminated in this checksum, so we just
        // need to `XOR` all duplicated keys up, we will get the right crc64xor diff.
        diff.crc64xor_diff ^= d.sum64();
    }


Review comment (bug):
The error message could be more descriptive. Instead of just including the conflict ID, consider including contextual information such as the key or other identifiers that can help with debugging when this panic occurs in production.
```suggestion
            None => panic!(
                "cannot resolve the conflict {} for key {}",
                cid,
                redact(&a.key),
            ),
```

Code after:
        // partial ordering of two conflicting records.
        let partial_cmp = |wa: &WriteRef<'_>, wb: &WriteRef<'_>| {
            use WriteType::*;
            match (wa.write_type, wb.write_type) {
                // Rollback -> Collapsed with Put happens.
                // Should keep the Put.
                (Put, Rollback | Put) if wa.has_overlapped_rollback => Some(Ordering::Greater),
                (Rollback | Put, Put) if wb.has_overlapped_rollback => Some(Ordering::Less),

                // Rollback -> Protected Rollback.
                // Keep the protected one.
                // This was observed in some versions and shouldn't happen normally.
                (Rollback, Rollback) if wa.is_protected() => Some(Ordering::Greater),
                (Rollback, Rollback) if wb.is_protected() => Some(Ordering::Less),

                // No comparing rule here.
                _ => None,
            }
        };

        sanity_check(&wa, &wb);
        let maybe_ord = partial_cmp(&wa, &wb);
        info!("resolve conflict result."; "conflict_id" => cid, "order" => ?maybe_ord);
        match maybe_ord {
            Some(Ordering::Greater) => std::mem::swap(a, b),
            None => panic!(
                "cannot resolve the conflict {} for key {}",
                cid,
                redact(&a.key)
            ),

            Some(_) => {}
        }
    }

    fn update_checksum_diff(
        c: &Subcompaction,
        a: &mut Record,
        b: &mut Record,
        diff: &mut ChecksumDiff,
    ) {
        Self::resolve(c, a, b);

        diff.removed_key += 1;
        diff.decreaed_size += (a.key.len() + a.value.len()) as u64;
        let mut d = crc64fast::Digest::new();
        d.write(&a.key);
        d.write(&a.value);
        // When we remove even number of the same key-value pair, we don't need to
        // update the crc64xor checksum. Trick here: If a key-value pair appears
        // even times, that key will be eliminated in this checksum, so we just