forked from voldemort/voldemort
/
VoldemortNativeRequestHandler.java
380 lines (335 loc) · 14.7 KB
/
VoldemortNativeRequestHandler.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package voldemort.server.protocol.vold;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import voldemort.VoldemortException;
import voldemort.common.VoldemortOpCode;
import voldemort.server.RequestRoutingType;
import voldemort.server.StoreRepository;
import voldemort.server.protocol.AbstractRequestHandler;
import voldemort.server.protocol.RequestHandler;
import voldemort.server.protocol.StreamRequestHandler;
import voldemort.store.ErrorCodeMapper;
import voldemort.store.Store;
import voldemort.utils.ByteArray;
import voldemort.utils.ByteBufferBackedInputStream;
import voldemort.utils.ByteUtils;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Version;
import voldemort.versioning.Versioned;
/**
* Server-side request handler for voldemort native client protocol
*
*
*/
public class VoldemortNativeRequestHandler extends AbstractRequestHandler implements RequestHandler {
private final Logger logger = Logger.getLogger(VoldemortNativeRequestHandler.class);
private final int protocolVersion;
public VoldemortNativeRequestHandler(ErrorCodeMapper errorMapper,
StoreRepository repository,
int protocolVersion) {
super(errorMapper, repository);
if(protocolVersion < 0 || protocolVersion > 3)
throw new IllegalArgumentException("Unknown protocol version: " + protocolVersion);
this.protocolVersion = protocolVersion;
}
public StreamRequestHandler handleRequest(DataInputStream inputStream,
DataOutputStream outputStream) throws IOException {
byte opCode = inputStream.readByte();
String storeName = inputStream.readUTF();
RequestRoutingType routingType = getRoutingType(inputStream);
Store<ByteArray, byte[], byte[]> store = getStore(storeName, routingType);
if(store == null) {
writeException(outputStream, new VoldemortException("No store named '" + storeName
+ "'."));
} else {
switch(opCode) {
case VoldemortOpCode.GET_OP_CODE:
handleGet(inputStream, outputStream, store);
break;
case VoldemortOpCode.GET_ALL_OP_CODE:
handleGetAll(inputStream, outputStream, store);
break;
case VoldemortOpCode.PUT_OP_CODE:
handlePut(inputStream, outputStream, store);
break;
case VoldemortOpCode.DELETE_OP_CODE:
handleDelete(inputStream, outputStream, store);
break;
case VoldemortOpCode.GET_VERSION_OP_CODE:
handleGetVersion(inputStream, outputStream, store);
break;
default:
throw new IOException("Unknown op code: " + opCode);
}
}
outputStream.flush();
return null;
}
private RequestRoutingType getRoutingType(DataInputStream inputStream) throws IOException {
RequestRoutingType routingType = RequestRoutingType.NORMAL;
if(protocolVersion > 0) {
boolean isRouted = inputStream.readBoolean();
routingType = RequestRoutingType.getRequestRoutingType(isRouted, false);
}
if(protocolVersion > 1) {
int routingTypeCode = inputStream.readByte();
routingType = RequestRoutingType.getRequestRoutingType(routingTypeCode);
}
return routingType;
}
private void handleGetVersion(DataInputStream inputStream,
DataOutputStream outputStream,
Store<ByteArray, byte[], byte[]> store) throws IOException {
ByteArray key = readKey(inputStream);
List<Version> results = null;
try {
results = store.getVersions(key);
outputStream.writeShort(0);
} catch(VoldemortException e) {
logger.error(e.getMessage());
writeException(outputStream, e);
return;
}
outputStream.writeInt(results.size());
for(Version v: results) {
byte[] clock = ((VectorClock) v).toBytes();
outputStream.writeInt(clock.length);
outputStream.write(clock);
}
}
/**
* This is pretty ugly. We end up mimicking the request logic here, so this
* needs to stay in sync with handleRequest.
*/
public boolean isCompleteRequest(final ByteBuffer buffer) {
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
try {
byte opCode = inputStream.readByte();
// Read the store name in, but just to skip the bytes.
inputStream.readUTF();
// Read the 'is routed' flag in, but just to skip the byte.
if(protocolVersion > 0) {
boolean routed = inputStream.readBoolean();
if(logger.isDebugEnabled()) {
logger.debug("isRouted=" + routed);
}
}
// Read routing type
if(protocolVersion > 1) {
int routingTypeCode = inputStream.readByte();
if(logger.isDebugEnabled()) {
logger.debug("routingTypeCode=" + routingTypeCode);
}
}
switch(opCode) {
case VoldemortOpCode.GET_VERSION_OP_CODE:
// Read the key just to skip the bytes.
readKey(inputStream);
break;
case VoldemortOpCode.GET_OP_CODE:
// Read the key just to skip the bytes.
readKey(inputStream);
if(protocolVersion > 2) {
boolean hasTransform = inputStream.readBoolean();
if(hasTransform) {
readTransforms(inputStream);
}
}
break;
case VoldemortOpCode.GET_ALL_OP_CODE:
int numKeys = inputStream.readInt();
// Read the keys to skip the bytes.
for(int i = 0; i < numKeys; i++)
readKey(inputStream);
if(protocolVersion > 2) {
boolean hasTransform = inputStream.readBoolean();
if(hasTransform) {
int numTrans = inputStream.readInt();
for(int i = 0; i < numTrans; i++) {
readTransforms(inputStream);
}
}
}
break;
case VoldemortOpCode.PUT_OP_CODE: {
readKey(inputStream);
int dataSize = inputStream.readInt();
int newPosition = buffer.position() + dataSize;
if(newPosition > buffer.limit() || newPosition < 0)
throw new Exception("Data inconsistency on put - dataSize: " + dataSize
+ ", position: " + buffer.position() + ", limit: "
+ buffer.limit());
// Here we skip over the data (without reading it in) and
// move our position to just past it.
buffer.position(newPosition);
if(protocolVersion > 2) {
boolean hasTransform = inputStream.readBoolean();
if(hasTransform) {
readTransforms(inputStream);
}
}
break;
}
case VoldemortOpCode.DELETE_OP_CODE: {
readKey(inputStream);
int versionSize = inputStream.readShort();
int newPosition = buffer.position() + versionSize;
if(newPosition > buffer.limit() || newPosition < 0)
throw new Exception("Data inconsistency on delete - versionSize: "
+ versionSize + ", position: " + buffer.position()
+ ", limit: " + buffer.limit());
// Here we skip over the data (without reading it in) and
// move our position to just past it.
buffer.position(newPosition);
break;
}
default:
// Do nothing, let the request handler address this...
}
// If there aren't any remaining, we've "consumed" all the bytes and
// thus have a complete request...
return !buffer.hasRemaining();
} catch(Exception e) {
// This could also occur if the various methods we call into
// re-throw a corrupted value error as some other type of exception.
// For example, updating the position on a buffer past its limit
// throws an InvalidArgumentException.
if(logger.isDebugEnabled())
logger.debug("Probable partial read occurred causing exception", e);
return false;
}
}
private ByteArray readKey(DataInputStream inputStream) throws IOException {
int keySize = inputStream.readInt();
byte[] key = new byte[keySize];
inputStream.readFully(key);
return new ByteArray(key);
}
private byte[] readTransforms(DataInputStream inputStream) throws IOException {
int size = inputStream.readInt();
if(size == 0)
return null;
byte[] transforms = new byte[size];
inputStream.readFully(transforms);
return transforms;
}
private void writeResults(DataOutputStream outputStream, List<Versioned<byte[]>> values)
throws IOException {
outputStream.writeInt(values.size());
for(Versioned<byte[]> v: values) {
byte[] clock = ((VectorClock) v.getVersion()).toBytes();
byte[] value = v.getValue();
outputStream.writeInt(clock.length + value.length);
outputStream.write(clock);
outputStream.write(value);
}
}
private void handleGet(DataInputStream inputStream,
DataOutputStream outputStream,
Store<ByteArray, byte[], byte[]> store) throws IOException {
ByteArray key = readKey(inputStream);
byte[] transforms = null;
if(protocolVersion > 2) {
if(inputStream.readBoolean())
transforms = readTransforms(inputStream);
}
List<Versioned<byte[]>> results = null;
try {
results = store.get(key, transforms);
outputStream.writeShort(0);
} catch(VoldemortException e) {
logger.error(e.getMessage());
writeException(outputStream, e);
return;
}
writeResults(outputStream, results);
}
private void handleGetAll(DataInputStream inputStream,
DataOutputStream outputStream,
Store<ByteArray, byte[], byte[]> store) throws IOException {
// read keys
int numKeys = inputStream.readInt();
List<ByteArray> keys = new ArrayList<ByteArray>(numKeys);
for(int i = 0; i < numKeys; i++)
keys.add(readKey(inputStream));
Map<ByteArray, byte[]> transforms = null;
if(protocolVersion > 2) {
if(inputStream.readBoolean()) {
int size = inputStream.readInt();
transforms = new HashMap<ByteArray, byte[]>(size);
for(int i = 0; i < size; i++) {
transforms.put(readKey(inputStream), readTransforms(inputStream));
}
}
}
// execute the operation
Map<ByteArray, List<Versioned<byte[]>>> results = null;
try {
results = store.getAll(keys, transforms);
outputStream.writeShort(0);
} catch(VoldemortException e) {
logger.error(e.getMessage());
writeException(outputStream, e);
return;
}
// write back the results
outputStream.writeInt(results.size());
for(Map.Entry<ByteArray, List<Versioned<byte[]>>> entry: results.entrySet()) {
// write the key
outputStream.writeInt(entry.getKey().length());
outputStream.write(entry.getKey().get());
// write the values
writeResults(outputStream, entry.getValue());
}
}
private void handlePut(DataInputStream inputStream,
DataOutputStream outputStream,
Store<ByteArray, byte[], byte[]> store) throws IOException {
ByteArray key = readKey(inputStream);
int valueSize = inputStream.readInt();
byte[] bytes = new byte[valueSize];
ByteUtils.read(inputStream, bytes);
VectorClock clock = new VectorClock(bytes);
byte[] value = ByteUtils.copy(bytes, clock.sizeInBytes(), bytes.length);
byte[] transforms = null;
if(protocolVersion > 2) {
if(inputStream.readBoolean()) {
transforms = readTransforms(inputStream);
}
}
try {
store.put(key, new Versioned<byte[]>(value, clock), transforms);
outputStream.writeShort(0);
} catch(VoldemortException e) {
writeException(outputStream, e);
}
}
private void handleDelete(DataInputStream inputStream,
DataOutputStream outputStream,
Store<ByteArray, byte[], byte[]> store) throws IOException {
ByteArray key = readKey(inputStream);
int versionSize = inputStream.readShort();
byte[] versionBytes = new byte[versionSize];
ByteUtils.read(inputStream, versionBytes);
VectorClock version = new VectorClock(versionBytes);
try {
boolean succeeded = store.delete(key, version);
outputStream.writeShort(0);
outputStream.writeBoolean(succeeded);
} catch(VoldemortException e) {
writeException(outputStream, e);
}
}
private void writeException(DataOutputStream stream, VoldemortException e) throws IOException {
short code = getErrorMapper().getCode(e);
stream.writeShort(code);
stream.writeUTF(e.getMessage());
}
}