Use DataStax/Cassandra timestamp generator.

This commit is contained in:
Greg Burd 2018-01-25 14:21:12 -05:00
parent 8b9d582fa5
commit 287e1a5b8b

View file

@ -18,10 +18,11 @@ package net.helenus.core.operation;
import com.codahale.metrics.Timer; import com.codahale.metrics.Timer;
import com.datastax.driver.core.BatchStatement; import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.TimestampGenerator;
import com.datastax.driver.core.AtomicMonotonicTimestampGenerator;
import com.google.common.base.Stopwatch; import com.google.common.base.Stopwatch;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.helenus.core.AbstractSessionOperations; import net.helenus.core.AbstractSessionOperations;
@ -29,6 +30,9 @@ import net.helenus.core.UnitOfWork;
import net.helenus.support.HelenusException; import net.helenus.support.HelenusException;
public class BatchOperation extends Operation<Long> { public class BatchOperation extends Operation<Long> {
//TODO(gburd): find the way to get the driver's timestamp generator
private static final TimestampGenerator timestampGenerator = new AtomicMonotonicTimestampGenerator();
private final BatchStatement batch; private final BatchStatement batch;
private List<AbstractOperation<?, ?>> operations = new ArrayList<AbstractOperation<?, ?>>(); private List<AbstractOperation<?, ?>> operations = new ArrayList<AbstractOperation<?, ?>>();
private boolean logged = true; private boolean logged = true;
@ -50,11 +54,6 @@ public class BatchOperation extends Operation<Long> {
return batch; return batch;
} }
private long captureTimestampMsec() {
// Java 9: Instant.now().truncatedTo( ChronoUnit.MICROSECONDS );
return TimeUnit.NANOSECONDS.convert(System.nanoTime(), TimeUnit.MICROSECONDS);
}
public BatchOperation logged() { public BatchOperation logged() {
logged = true; logged = true;
return this; return this;
@ -69,7 +68,7 @@ public class BatchOperation extends Operation<Long> {
if (operations.size() == 0) return 0L; if (operations.size() == 0) return 0L;
final Timer.Context context = requestLatency.time(); final Timer.Context context = requestLatency.time();
try { try {
batch.setDefaultTimestamp(captureTimestampMsec()); batch.setDefaultTimestamp(timestampGenerator.next());
ResultSet resultSet = ResultSet resultSet =
this.execute( this.execute(
sessionOps, sessionOps,
@ -96,7 +95,7 @@ public class BatchOperation extends Operation<Long> {
final Stopwatch timer = Stopwatch.createStarted(); final Stopwatch timer = Stopwatch.createStarted();
try { try {
uow.recordCacheAndDatabaseOperationCount(0, 1); uow.recordCacheAndDatabaseOperationCount(0, 1);
batch.setDefaultTimestamp(captureTimestampMsec()); batch.setDefaultTimestamp(timestampGenerator.next());
ResultSet resultSet = ResultSet resultSet =
this.execute( this.execute(
sessionOps, sessionOps,