Python脚本如何校验全局数据同步完整

wen python案例 32

本文目录导读:

Python脚本如何校验全局数据同步完整

  1. 哈希校验法
  2. 增量校验(分块哈希)
  3. Merkle树校验(适用于分布式系统)
  4. 数据库同步校验
  5. 文件系统同步校验
  6. 实时监控校验
  7. 最佳实践建议

我来介绍几种Python校验全局数据同步完整性的方法:

哈希校验法

基础哈希对比

import hashlib
import json
class DataSyncVerifier:
    @staticmethod
    def calculate_hash(data):
        """计算数据哈希值"""
        if isinstance(data, (dict, list)):
            # 确保一致性:排序键值
            data_str = json.dumps(data, sort_keys=True)
        else:
            data_str = str(data)
        return hashlib.sha256(data_str.encode()).hexdigest()
    @staticmethod
    def verify_sync(source_data, target_data):
        """校验数据同步完整性"""
        source_hash = DataSyncVerifier.calculate_hash(source_data)
        target_hash = DataSyncVerifier.calculate_hash(target_data)
        return source_hash == target_hash
# 使用示例
source = {"users": [{"id": 1, "name": "Alice"}]}
target = {"users": [{"id": 1, "name": "Alice"}]}
if DataSyncVerifier.verify_sync(source, target):
    print("数据同步完整")
else:
    print("数据同步不完整")

增量校验(分块哈希)

class IncrementalSyncVerifier:
    def __init__(self, chunk_size=1024):
        self.chunk_size = chunk_size
    def calculate_chunk_hashes(self, data_iterable):
        """计算数据块哈希列表"""
        chunk_hashes = []
        for chunk in data_iterable:
            chunk_hash = hashlib.md5(chunk).hexdigest()
            chunk_hashes.append(chunk_hash)
        return chunk_hashes
    def verify_incremental_sync(self, source_iterable, target_iterable):
        """增量校验数据同步"""
        source_hashes = self.calculate_chunk_hashes(source_iterable)
        target_hashes = self.calculate_chunk_hashes(target_iterable)
        if len(source_hashes) != len(target_hashes):
            return False, "数据块数量不匹配"
        mismatched_chunks = []
        for i, (s_hash, t_hash) in enumerate(zip(source_hashes, target_hashes)):
            if s_hash != t_hash:
                mismatched_chunks.append(i)
        return len(mismatched_chunks) == 0, mismatched_chunks
# 使用示例
source_data = [b"chunk1", b"chunk2", b"chunk3"]
target_data = [b"chunk1", b"chunk2", b"chunk3"]
verifier = IncrementalSyncVerifier()
is_complete, details = verifier.verify_incremental_sync(source_data, target_data)
print(f"同步完整: {is_complete}, 异常块: {details}")

Merkle树校验(适用于分布式系统)

import hashlib
from typing import List, Optional
class MerkleNode:
    def __init__(self, hash_value: str, left: Optional['MerkleNode'] = None, 
                 right: Optional['MerkleNode'] = None):
        self.hash = hash_value
        self.left = left
        self.right = right
class MerkleTree:
    def __init__(self, data: List[str]):
        self.data = data
        self.root = self.build_tree(data)
    def build_tree(self, data: List[str]) -> MerkleNode:
        """构建Merkle树"""
        if not data:
            return None
        # 创建叶子节点
        nodes = [MerkleNode(hashlib.sha256(d.encode()).hexdigest()) for d in data]
        # 逐层构建父节点
        while len(nodes) > 1:
            temp_nodes = []
            for i in range(0, len(nodes), 2):
                left = nodes[i]
                right = nodes[i + 1] if i + 1 < len(nodes) else left
                combined_hash = hashlib.sha256(
                    (left.hash + right.hash).encode()
                ).hexdigest()
                temp_nodes.append(MerkleNode(combined_hash, left, right))
            nodes = temp_nodes
        return nodes[0] if nodes else None
    def get_root_hash(self) -> str:
        """获取根哈希"""
        return self.root.hash if self.root else ""
class DistributedSyncVerifier:
    @staticmethod
    def verify_merkle_sync(source_data: List[str], target_data: List[str]) -> bool:
        """使用Merkle树校验分布式数据同步"""
        source_tree = MerkleTree(source_data)
        target_tree = MerkleTree(target_data)
        return source_tree.get_root_hash() == target_tree.get_root_hash()
# 使用示例
source_records = ["user1", "user2", "user3", "user4"]
target_records = ["user1", "user2", "user3", "user4"]
verifier = DistributedSyncVerifier()
if verifier.verify_merkle_sync(source_records, target_records):
    print("分布式数据同步完整")

数据库同步校验

import psycopg2
from typing import Dict, Any
class DatabaseSyncVerifier:
    def __init__(self, source_conn_params: Dict, target_conn_params: Dict):
        self.source_conn = psycopg2.connect(**source_conn_params)
        self.target_conn = psycopg2.connect(**target_conn_params)
    def verify_table_sync(self, table_name: str, primary_key: str) -> bool:
        """校验特定表的数据同步"""
        source_cursor = self.source_conn.cursor()
        target_cursor = self.target_conn.cursor()
        # 获取数据行数
        source_cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
        target_cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
        source_count = source_cursor.fetchone()[0]
        target_count = target_cursor.fetchone()[0]
        if source_count != target_count:
            return False
        # 获取数据哈希
        source_cursor.execute(
            f"SELECT MD5(string_agg({primary_key}::text || '|' || "
            f"row_to_json({table_name})::text, ',' ORDER BY {primary_key})) "
            f"FROM {table_name}"
        )
        target_cursor.execute(
            f"SELECT MD5(string_agg({primary_key}::text || '|' || "
            f"row_to_json({table_name})::text, ',' ORDER BY {primary_key})) "
            f"FROM {table_name}"
        )
        source_hash = source_cursor.fetchone()[0]
        target_hash = target_cursor.fetchone()[0]
        source_cursor.close()
        target_cursor.close()
        return source_hash == target_hash
    def close(self):
        self.source_conn.close()
        self.target_conn.close()
# 使用示例
source_params = {
    "host": "source_host",
    "database": "source_db",
    "user": "user",
    "password": "password"
}
target_params = {
    "host": "target_host", 
    "database": "target_db",
    "user": "user",
    "password": "password"
}
verifier = DatabaseSyncVerifier(source_params, target_params)
is_synced = verifier.verify_table_sync("users", "id")
print(f"用户表同步状态: {'完整' if is_synced else '不完整'}")
verifier.close()

文件系统同步校验

import os
import hashlib
from pathlib import Path
class FileSyncVerifier:
    def __init__(self, source_dir: str, target_dir: str):
        self.source_dir = Path(source_dir)
        self.target_dir = Path(target_dir)
    def get_file_checksums(self, directory: Path) -> Dict[str, str]:
        """获取目录中所有文件的校验和"""
        checksums = {}
        for file_path in directory.rglob('*'):
            if file_path.is_file():
                relative_path = str(file_path.relative_to(directory))
                with open(file_path, 'rb') as f:
                    file_hash = hashlib.sha256(f.read()).hexdigest()
                checksums[relative_path] = file_hash
        return checksums
    def verify_sync(self) -> Dict[str, bool]:
        """校验文件同步完整性"""
        source_checksums = self.get_file_checksums(self.source_dir)
        target_checksums = self.get_file_checksums(self.target_dir)
        result = {
            "files_match": True,
            "missing_in_target": [],
            "extra_in_target": [],
            "corrupted_files": []
        }
        # 检查源文件是否都在目标端
        for file_path, checksum in source_checksums.items():
            if file_path not in target_checksums:
                result["files_match"] = False
                result["missing_in_target"].append(file_path)
            elif target_checksums[file_path] != checksum:
                result["files_match"] = False
                result["corrupted_files"].append(file_path)
        # 检查目标端多余文件
        for file_path in target_checksums:
            if file_path not in source_checksums:
                result["files_match"] = False
                result["extra_in_target"].append(file_path)
        return result
# 使用示例
verifier = FileSyncVerifier("/path/to/source", "/path/to/target")
sync_result = verifier.verify_sync()
print(f"文件同步状态: {'完整' if sync_result['files_match'] else '不完整'}")

实时监控校验

import time
import threading
from datetime import datetime
from typing import Callable
class RealTimeSyncMonitor:
    def __init__(self, check_interval: int = 60):
        self.check_interval = check_interval
        self.verifiers = []
        self.stop_monitoring = False
    def add_verifier(self, verifier: Callable, name: str):
        """添加校验器"""
        self.verifiers.append((verifier, name))
    def check_sync_health(self):
        """检查同步健康状态"""
        results = {}
        for verifier, name in self.verifiers:
            try:
                is_synced = verifier()
                results[name] = {
                    "status": "OK" if is_synced else "FAILED",
                    "timestamp": datetime.now().isoformat()
                }
            except Exception as e:
                results[name] = {
                    "status": "ERROR",
                    "error": str(e),
                    "timestamp": datetime.now().isoformat()
                }
        return results
    def start_monitoring(self):
        """启动监控"""
        def monitor_loop():
            while not self.stop_monitoring:
                results = self.check_sync_health()
                # 记录和报警
                for name, result in results.items():
                    if result["status"] != "OK":
                        print(f"[ALERT] 同步异常 - {name}: {result}")
                    else:
                        print(f"[INFO] 同步正常 - {name}: {result['timestamp']}")
                time.sleep(self.check_interval)
        monitor_thread = threading.Thread(target=monitor_loop, daemon=True)
        monitor_thread.start()
    def stop(self):
        """停止监控"""
        self.stop_monitoring = True
# 使用示例
def simple_sync_check():
    """简单的同步检查函数"""
    # 这里实现实际的同步校验逻辑
    return True
monitor = RealTimeSyncMonitor(check_interval=30)
monitor.add_verifier(simple_sync_check, "主数据库同步")
monitor.start_monitoring()
# 让监控运行一段时间
time.sleep(120)
monitor.stop()

最佳实践建议

  1. 选择合适的校验方法

    • 小数据量:使用完整哈希校验
    • 大数据量:使用增量或Merkle树校验
    • 实时系统:结合实时监控
  2. 性能优化

    # 使用缓存避免重复计算
    from functools import lru_cache
    @lru_cache(maxsize=100)
    def get_cached_hash(data):
        return hashlib.sha256(str(data).encode()).hexdigest()
  3. 异常处理

    class SyncVerificationError(Exception):
        pass
    try:
        verifier.verify_sync()
    except SyncVerificationError as e:
        # 记录错误并触发告警
        logger.error(f"同步校验失败: {e}")
        alert_system.send_alert("sync_failure", str(e))
  4. 日志记录

    import logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logger.info(f"数据同步校验开始: {datetime.now()}")
    logger.info(f"校验结果: {result}")

选择哪种方法取决于你的具体需求:数据量大小、实时性要求、系统架构等,通常建议组合使用多种校验方法以提高可靠性。

抱歉,评论功能暂时关闭!