/
RocksDbKey.java
222 lines (194 loc) · 7.29 KB
/
RocksDbKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package org.apache.storm.metricstore.rocksdb;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;
import org.apache.storm.metricstore.AggLevel;
import org.apache.storm.shade.com.google.common.primitives.UnsignedBytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class representing the data used as a Key in RocksDB. Keys can be used either for metadata or metrics.
*
* <P>Keys are 38 bytes in size. The fields for a key are:
* <pre><
* Field Size Offset
*
* Type 1 0 The type maps to the KeyType enum, specifying a metric or various types of metadata
* Aggregation Level 1 1 The aggregation level for a metric (see AggLevel enum). 0 for metadata.
* TopologyId 4 2 The metadata string Id representing a topologyId for a metric, or the unique
* string Id for a metadata string
* Timestamp 8 6 The timestamp for a metric, unused for metadata
* MetricId 4 14 The metadata string Id for the metric name
* ComponentId 4 18 The metadata string Id for the component Id
* ExecutorId 4 22 The metadata string Id for the executor Id
* HostId 4 26 The metadata string Id for the host Id
* Port 4 30 The port number
* StreamId 4 34 The metadata string Id for the stream Id
* </pre>
*/
public class RocksDbKey implements Comparable<RocksDbKey> {
static final int KEY_SIZE = 38;
private static final Logger LOG = LoggerFactory.getLogger(RocksDbKey.class);
private static Map<Byte, RocksDbKey> PREFIX_MAP = new HashMap<>();
static {
// pregenerate commonly used keys for scans
for (KeyType type : EnumSet.allOf(KeyType.class)) {
RocksDbKey key = new RocksDbKey(type, 0);
PREFIX_MAP.put(type.getValue(), key);
}
PREFIX_MAP = Collections.unmodifiableMap(PREFIX_MAP);
}
private byte[] key;
/**
* Constructor for a RocksDB key for a metadata string.
*
* @param type type of metadata string
* @param metadataStringId the string Id for the string (stored in the topologyId portion of the key)
*/
RocksDbKey(KeyType type, int metadataStringId) {
byte[] key = new byte[KEY_SIZE];
ByteBuffer bb = ByteBuffer.wrap(key);
bb.put(type.getValue());
bb.put(AggLevel.AGG_LEVEL_NONE.getValue());
bb.putInt(metadataStringId);
this.key = key;
}
/**
* Constructor for a RocksDB key from raw data.
*
* @param raw the key data
*/
RocksDbKey(byte[] raw) {
this.key = raw;
}
/**
* Get a zeroed key of the specified type.
*
* @param type the desired type
* @return a key of the desired type
*/
static RocksDbKey getPrefix(KeyType type) {
return PREFIX_MAP.get(type.getValue());
}
/**
* gets the first possible key value for the desired key type.
*
* @return the initial key
*/
static RocksDbKey getInitialKey(KeyType type) {
return PREFIX_MAP.get(type.getValue());
}
/**
* gets the key just larger than the last possible key value for the desired key type.
*
* @return the last key
*/
static RocksDbKey getLastKey(KeyType type) {
byte value = (byte) (type.getValue() + 1);
return PREFIX_MAP.get(value);
}
/**
* Creates a metric key with the desired properties.
*
* @return the generated key
*/
static RocksDbKey createMetricKey(AggLevel aggLevel, int topologyId, long metricTimestamp, int metricId,
int componentId, int executorId, int hostId, int port,
int streamId) {
byte[] raw = new byte[KEY_SIZE];
ByteBuffer bb = ByteBuffer.wrap(raw);
bb.put(KeyType.METRIC_DATA.getValue());
bb.put(aggLevel.getValue());
bb.putInt(topologyId); // offset 2
bb.putLong(metricTimestamp); // offset 6
bb.putInt(metricId); // offset 14
bb.putInt(componentId); // offset 18
bb.putInt(executorId); // offset 22
bb.putInt(hostId); // offset 26
bb.putInt(port); // offset 30
bb.putInt(streamId); // offset 34
RocksDbKey key = new RocksDbKey(raw);
return key;
}
/**
* get the metadata string Id portion of the key for metadata keys.
*
* @return the metadata string Id
* @throws RuntimeException if the key is not a metadata type
*/
int getMetadataStringId() {
if (this.getType().getValue() < KeyType.METADATA_STRING_END.getValue()) {
return ByteBuffer.wrap(key, 2, 4).getInt();
} else {
throw new RuntimeException("Cannot fetch metadata string for key of type " + this.getType());
}
}
/**
* get the raw key bytes
*/
byte[] getRaw() {
return this.key;
}
/**
* get the type of key.
*
* @return the type of key
*/
KeyType getType() {
return KeyType.getKeyType(key[0]);
}
/**
* compares to keys on a byte by byte basis.
*
* @return comparison of key byte values
*/
@Override
public int compareTo(RocksDbKey o) {
return UnsignedBytes.lexicographicalComparator().compare(this.getRaw(), o.getRaw());
}
/**
* Get the unique string Id for a metric's topologyId.
*/
int getTopologyId() {
int val = ByteBuffer.wrap(key, 2, 4).getInt();
return val;
}
long getTimestamp() {
return ByteBuffer.wrap(key, 6, 8).getLong();
}
int getMetricId() {
return ByteBuffer.wrap(key, 14, 4).getInt();
}
int getComponentId() {
return ByteBuffer.wrap(key, 18, 4).getInt();
}
int getExecutorId() {
return ByteBuffer.wrap(key, 22, 4).getInt();
}
int getHostnameId() {
return ByteBuffer.wrap(key, 26, 4).getInt();
}
int getPort() {
return ByteBuffer.wrap(key, 30, 4).getInt();
}
int getStreamId() {
return ByteBuffer.wrap(key, 34, 4).getInt();
}
@Override
public String toString() {
return "[0x" + DatatypeConverter.printHexBinary(key) + "]";
}
}