From 7efc4726a2a781a16c78b7e3fd6661a23e377e4e Mon Sep 17 00:00:00 2001 From: Andreas Falk Date: Thu, 7 Aug 2014 17:11:58 +0200 Subject: [PATCH] Calculate the base time in 1 method instead of 4 different places --- src/core/IncomingDataPoints.java | 19 +++++-------------- src/core/RowKey.java | 11 +++-------- src/core/TSDB.java | 10 +--------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/core/IncomingDataPoints.java b/src/core/IncomingDataPoints.java index 31b0e79617..f78a8c309c 100644 --- a/src/core/IncomingDataPoints.java +++ b/src/core/IncomingDataPoints.java @@ -121,22 +121,20 @@ public void setSeries(final String metric, final Map tags) { /** * Updates the base time in the row key. - * @param timestamp The timestamp from which to derive the new base time. + * @param base_time The new base time * @return The updated base time. */ - private long updateBaseTime(final long timestamp) { + private void updateBaseTime(final long base_time) { // We force the starting timestamp to be on a MAX_TIMESPAN boundary // so that all TSDs create rows with the same base time. Otherwise // we'd need to coordinate TSDs to avoid creating rows that cover // overlapping time periods. - final long base_time = timestamp - (timestamp % Const.MAX_TIMESPAN); // Clone the row key since we're going to change it. We must clone it // because the TsdbStore may still hold a reference to it in its // internal datastructures. row = Arrays.copyOf(row, row.length); Bytes.setInt(row, (int) base_time, tsdb.metrics.width()); tsdb.getTsdbStore().scheduleForCompaction(row); - return base_time; } /** @@ -171,18 +169,11 @@ private Deferred addPointInternal(final long timestamp, final byte[] val last_ts = (ms_timestamp ? timestamp : timestamp * 1000); long base_time = RowKey.baseTime(row); - long incoming_base_time; - if (ms_timestamp) { - // drop the ms timestamp to seconds to calculate the base timestamp - incoming_base_time = ((timestamp / 1000) - - ((timestamp / 1000) % Const.MAX_TIMESPAN)); - } else { - incoming_base_time = (timestamp - (timestamp % Const.MAX_TIMESPAN)); - } - + long incoming_base_time = HBaseStore.buildBaseTime(timestamp); + if (incoming_base_time - base_time >= Const.MAX_TIMESPAN) { // Need to start a new row as we've exceeded Const.MAX_TIMESPAN. - base_time = updateBaseTime((ms_timestamp ? timestamp / 1000: timestamp)); + updateBaseTime(incoming_base_time); } // Java is so stupid with its auto-promotion of int to float. diff --git a/src/core/RowKey.java b/src/core/RowKey.java index 180c312aac..b28ac23c0f 100644 --- a/src/core/RowKey.java +++ b/src/core/RowKey.java @@ -15,6 +15,8 @@ import java.util.Arrays; import java.util.Map; +import net.opentsdb.storage.hbase.HBaseStore; + import org.hbase.async.Bytes; import com.stumbleupon.async.Deferred; @@ -81,14 +83,7 @@ public static Map tags(final byte[] row) { */ public static byte[] rowKeyFromTSUID(final TSDB tsdb, final byte[] tsuid, final long timestamp) { - final long base_time; - if ((timestamp & Const.SECOND_MASK) != 0) { - // drop the ms timestamp to seconds to calculate the base timestamp - base_time = ((timestamp / 1000) - - ((timestamp / 1000) % Const.MAX_TIMESPAN)); - } else { - base_time = (timestamp - (timestamp % Const.MAX_TIMESPAN)); - } + final long base_time = HBaseStore.buildBaseTime(timestamp); final byte[] row = new byte[tsuid.length + Const.TIMESTAMP_BYTES]; System.arraycopy(tsuid, 0, row, 0, Const.METRICS_WIDTH); Bytes.setInt(row, (int) base_time, Const.METRICS_WIDTH); diff --git a/src/core/TSDB.java b/src/core/TSDB.java index 954f3daf05..f73b2c1651 100644 --- a/src/core/TSDB.java +++ b/src/core/TSDB.java @@ -720,15 +720,7 @@ class RowKeyCB implements Callback, byte[]> { public Deferred call(byte[] row) throws Exception { final byte[] qualifier = Internal.buildQualifier(timestamp, flags); - final long base_time; - if ((timestamp & Const.SECOND_MASK) != 0) { - // drop the ms timestamp to seconds to calculate the base timestamp - base_time = ((timestamp / 1000) - - ((timestamp / 1000) % Const.MAX_TIMESPAN)); - } else { - base_time = (timestamp - (timestamp % Const.MAX_TIMESPAN)); - } - + final long base_time = HBaseStore.buildBaseTime(timestamp); Bytes.setInt(row, (int) base_time, metrics.width()); // TODO(tsuna): Add a callback to time the latency of HBase and store the