MCPcopy
hub / github.com/OpenTSDB/opentsdb / prefixKeyWithSalt

Method prefixKeyWithSalt

src/core/RowKey.java:141–167  ·  view source on GitHub ↗

Calculates and writes an array of one or more salt bytes at the front of the given row key. The salt is calculated by taking the Java hash code of the metric and tag UIDs and returning a modulo based on the number of salt buckets. The result will always be a positive integer from 0 to salt buckets.

(final byte[] row_key)

Source from the content-addressed store, hash-verified

139 * @since 2.2
140 */
141 public static void prefixKeyWithSalt(final byte[] row_key) {
142 if (Const.SALT_WIDTH() > 0) {
143 if (row_key.length < (Const.SALT_WIDTH() + TSDB.metrics_width()) ||
144 (Bytes.memcmp(row_key, new byte[Const.SALT_WIDTH() + TSDB.metrics_width()],
145 Const.SALT_WIDTH(), TSDB.metrics_width()) == 0)) {
146 // ^ Don't salt the global annotation row, leave it at zero
147 return;
148 }
149 final int tags_start = Const.SALT_WIDTH() + TSDB.metrics_width() +
150 Const.TIMESTAMP_BYTES;
151
152 // we want the metric and tags, not the timestamp
153 final byte[] salt_base =
154 new byte[row_key.length - Const.SALT_WIDTH() - Const.TIMESTAMP_BYTES];
155 System.arraycopy(row_key, Const.SALT_WIDTH(), salt_base, 0, TSDB.metrics_width());
156 System.arraycopy(row_key, tags_start,salt_base, TSDB.metrics_width(),
157 row_key.length - tags_start);
158 int modulo = Arrays.hashCode(salt_base) % Const.SALT_BUCKETS();
159 if (modulo < 0) {
160 // make sure we return a positive salt.
161 modulo = modulo * -1;
162 }
163
164 final byte[] salt = getSaltBytes(modulo);
165 System.arraycopy(salt, 0, row_key, 0, Const.SALT_WIDTH());
166 } // else salting is disabled so it's a no-op
167 }
168
169 /**
170 * Checks a row key to determine if it contains the metric UID. If salting is

Calls 5

SALT_WIDTHMethod · 0.95
metrics_widthMethod · 0.95
SALT_BUCKETSMethod · 0.95
getSaltBytesMethod · 0.95
hashCodeMethod · 0.45