Skip to content

Commit

Permalink
Calculate the base time in 1 method instead of 4 different places
Browse files Browse the repository at this point in the history
  • Loading branch information
vonbirdie committed Aug 7, 2014
1 parent 365b7e6 commit 7efc472
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
19 changes: 5 additions & 14 deletions src/core/IncomingDataPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,20 @@ public void setSeries(final String metric, final Map<String, String> 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;
}

/**
Expand Down Expand Up @@ -171,18 +169,11 @@ private Deferred<Object> 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.
Expand Down
11 changes: 3 additions & 8 deletions src/core/RowKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,14 +83,7 @@ public static Map<byte[], byte[]> 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);
Expand Down
10 changes: 1 addition & 9 deletions src/core/TSDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,15 +720,7 @@ class RowKeyCB implements Callback<Deferred<Object>, byte[]> {
public Deferred<Object> 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
Expand Down

0 comments on commit 7efc472

Please sign in to comment.