-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Removed duplicated key in stream dumps
- Loading branch information
Showing
5 changed files
with
115 additions
and
105 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
plugins/riot/src/main/java/com/redis/riot/AbstractCompareCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.redis.riot; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
|
||
import com.redis.riot.CompareStatusItemWriter.StatusCount; | ||
import com.redis.riot.core.Step; | ||
import com.redis.spring.batch.item.redis.RedisItemReader; | ||
import com.redis.spring.batch.item.redis.reader.DefaultKeyComparator; | ||
import com.redis.spring.batch.item.redis.reader.KeyComparator; | ||
import com.redis.spring.batch.item.redis.reader.KeyComparison; | ||
import com.redis.spring.batch.item.redis.reader.KeyComparisonItemReader; | ||
|
||
import io.lettuce.core.codec.ByteArrayCodec; | ||
import picocli.CommandLine.Option; | ||
|
||
public abstract class AbstractCompareCommand extends AbstractTargetCommand { | ||
|
||
public static final Duration DEFAULT_TTL_TOLERANCE = DefaultKeyComparator.DEFAULT_TTL_TOLERANCE; | ||
public static final String COMPARE_STEP_NAME = "compare"; | ||
public static final boolean DEFAULT_COMPARE_STREAM_MESSAGE_ID = true; | ||
|
||
private static final String COMPARE_TASK_NAME = "Comparing"; | ||
private static final String STATUS_DELIMITER = " | "; | ||
|
||
@Option(names = "--show-diffs", description = "Print details of key mismatches during dataset verification. Disables progress reporting.") | ||
private boolean showDiffs; | ||
|
||
@Option(names = "--ttl-tolerance", description = "Max TTL offset in millis to consider keys equal (default: ${DEFAULT-VALUE}).", paramLabel = "<ms>") | ||
private long ttlToleranceMillis = DEFAULT_TTL_TOLERANCE.toMillis(); | ||
|
||
private String compareMessage(Collection<StatusCount> counts) { | ||
StringBuilder builder = new StringBuilder(); | ||
counts.stream().map(CompareStepListener::toString).forEach(s -> builder.append(STATUS_DELIMITER).append(s)); | ||
return builder.toString(); | ||
} | ||
|
||
protected Step<KeyComparison<byte[]>, KeyComparison<byte[]>> compareStep() { | ||
KeyComparisonItemReader<byte[], byte[]> reader = compareReader(); | ||
CompareStatusItemWriter<byte[]> writer = new CompareStatusItemWriter<>(); | ||
Step<KeyComparison<byte[]>, KeyComparison<byte[]>> step = new Step<>(COMPARE_STEP_NAME, reader, writer); | ||
step.taskName(COMPARE_TASK_NAME); | ||
step.statusMessageSupplier(() -> compareMessage(writer.getMismatches())); | ||
step.maxItemCountSupplier(RedisScanSizeEstimator.from(reader.getSourceReader())); | ||
if (showDiffs) { | ||
log.info("Adding key diff logger"); | ||
step.writeListener(new CompareLoggingWriteListener<>(ByteArrayCodec.INSTANCE)); | ||
} | ||
step.executionListener(new CompareStepListener(writer)); | ||
return step; | ||
} | ||
|
||
private RedisItemReader<byte[], byte[], Object> compareRedisReader() { | ||
if (isQuickCompare()) { | ||
return RedisItemReader.type(ByteArrayCodec.INSTANCE); | ||
} | ||
return RedisItemReader.struct(ByteArrayCodec.INSTANCE); | ||
} | ||
|
||
protected abstract boolean isQuickCompare(); | ||
|
||
protected KeyComparisonItemReader<byte[], byte[]> compareReader() { | ||
RedisItemReader<byte[], byte[], Object> source = compareSourceReader(); | ||
RedisItemReader<byte[], byte[], Object> target = compareTargetReader(); | ||
KeyComparisonItemReader<byte[], byte[]> reader = new KeyComparisonItemReader<>(source, target); | ||
reader.setComparator(keyComparator()); | ||
return reader; | ||
} | ||
|
||
private KeyComparator<byte[]> keyComparator() { | ||
boolean ignoreStreamId = isIgnoreStreamMessageId(); | ||
Duration ttlTolerance = Duration.ofMillis(ttlToleranceMillis); | ||
log.info("Creating KeyComparator with ttlTolerance={} ignoreStreamMessageId={}", ttlTolerance, ignoreStreamId); | ||
DefaultKeyComparator<byte[], byte[]> comparator = new DefaultKeyComparator<>(ByteArrayCodec.INSTANCE); | ||
comparator.setIgnoreStreamMessageId(ignoreStreamId); | ||
comparator.setTtlTolerance(ttlTolerance); | ||
return comparator; | ||
} | ||
|
||
protected abstract boolean isIgnoreStreamMessageId(); | ||
|
||
private RedisItemReader<byte[], byte[], Object> compareSourceReader() { | ||
RedisItemReader<byte[], byte[], Object> reader = compareRedisReader(); | ||
configure(reader); | ||
return reader; | ||
} | ||
|
||
private RedisItemReader<byte[], byte[], Object> compareTargetReader() { | ||
RedisItemReader<byte[], byte[], Object> reader = compareRedisReader(); | ||
reader.setClient(targetRedisURIClient.getClient()); | ||
reader.setDatabase(targetRedisURIClient.getUri().getDatabase()); | ||
return reader; | ||
} | ||
|
||
public boolean isShowDiffs() { | ||
return showDiffs; | ||
} | ||
|
||
public void setShowDiffs(boolean showDiffs) { | ||
this.showDiffs = showDiffs; | ||
} | ||
|
||
public long getTtlToleranceMillis() { | ||
return ttlToleranceMillis; | ||
} | ||
|
||
public void setTtlToleranceMillis(long tolerance) { | ||
this.ttlToleranceMillis = tolerance; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters