Example Input
file1.txt
shard1 host1:6379 host2:6379 host3:6379
shard2 host4:6379 host5:6379 host6:6379
file2.txt
shard1 host1:6379 host2:6379 host3:6379
shard2 host4:6379 host5:9999 host6:6379
Python script to compare
def parse_file(filename):
"""Read shard -> hosts mapping from file."""
shards = {}
with open(filename, "r") as f:
for line in f:
parts = line.strip().split()
if not parts:
continue
shard = parts[0]
hosts = parts[1:]
shards[shard] = set(hosts) # use set for easy comparison
return shards
def compare_shards(file1, file2):
shards1 = parse_file(file1)
shards2 = parse_file(file2)
all_shards = set(shards1.keys()) | set(shards2.keys())
for shard in sorted(all_shards):
hosts1 = shards1.get(shard, set())
hosts2 = shards2.get(shard, set())
if hosts1 == hosts2:
print(f"✅ {shard} is consistent in both files")
else:
print(f"❌ {shard} mismatch")
if hosts1 - hosts2:
print(f" Present only in {file1}: {hosts1 - hosts2}")
if hosts2 - hosts1:
print(f" Present only in {file2}: {hosts2 - hosts1}")
# Example usage
compare_shards("file1.txt", "file2.txt")
Output for example
✅ shard1 is consistent in both files
❌ shard2 mismatch
Present only in file1.txt: {'host5:6379'}
Present only in file2.txt: {'host5:9999'}
Enhanced program
def parse_file(filename):
"""Read shard -> hosts mapping from file."""
shards = {}
with open(filename, "r") as f:
for line in f:
parts = line.strip().split()
if not parts:
continue
shard = parts[0] # first word = shard name
hosts = parts[1:] # rest = host list
shards[shard] = set(hosts) # store as set
return shards
def compare_shards(file1, file2):
shards1 = parse_file(file1)
shards2 = parse_file(file2)
all_shards = set(shards1.keys()) | set(shards2.keys()) # union of all shard names
for shard in sorted(all_shards):
hosts1 = shards1.get(shard, set())
hosts2 = shards2.get(shard, set())
if hosts1 == hosts2:
print(f"[OK] {shard} is consistent in both files")
else:
print(f"[MISMATCH] {shard}")
if shard not in shards1:
print(f" {shard} is missing in {file1}")
if shard not in shards2:
print(f" {shard} is missing in {file2}")
# Compare hosts in detail
extra_in_file1 = hosts1 - hosts2
extra_in_file2 = hosts2 - hosts1
if extra_in_file1:
print(f" Present only in {file1}:")
for h in sorted(extra_in_file1):
print(f" {h}")
if extra_in_file2:
print(f" Present only in {file2}:")
for h in sorted(extra_in_file2):
print(f" {h}")
# Example usage
if __name__ == "__main__":
compare_shards("file1.txt", "file2.txt")
Output
python3 comp.py file1 file2
[OK] shard1 is consistent in both files
[MISMATCH] shard2
Present only in file1.txt:
host5:6379
Present only in file2.txt:
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment