/
IntraOp.java
490 lines (465 loc) · 18.2 KB
/
IntraOp.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package org.usergrid.vx.experimental;
import com.google.common.base.Preconditions;
import groovy.lang.GroovyClassLoader;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.KSMetaData;
import org.apache.cassandra.config.Schema;
import org.apache.cassandra.cql3.QueryProcessor;
import org.apache.cassandra.db.*;
import org.apache.cassandra.db.Column;
import org.apache.cassandra.db.ConsistencyLevel;
import org.apache.cassandra.db.filter.QueryPath;
import org.apache.cassandra.exceptions.*;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.exceptions.UnavailableException;
import org.apache.cassandra.service.ClientState;
import org.apache.cassandra.service.MigrationManager;
import org.apache.cassandra.service.QueryState;
import org.apache.cassandra.service.StorageProxy;
import org.apache.cassandra.thrift.*;
import org.apache.cassandra.transport.messages.ResultMessage;
import org.vertx.java.core.Vertx;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class IntraOp implements Serializable{
public static final String COLUMN= "COLUMN";
public static final String VALUE="VALUE";
// TODO add back access to ID
private final AtomicInteger opid= new AtomicInteger(0);
private static final long serialVersionUID = 4700563595579293663L;
private Type type;
private Map<String,Object> op;
protected IntraOp() {}
IntraOp(Type type){
this.type = type;
op = new TreeMap<String,Object>();
}
public IntraOp set(String key, Object value){
op.put(key,value);
return this;
}
public Map<String, Object> getOp() {
return op;
}
public Type getType() {
return type;
}
public enum Type {
LISTCOLUMNFAMILY {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String keyspace = (String) op.getOp().get("keyspace");
KSMetaData ks = Schema.instance.getKSMetaData(keyspace);
res.getOpsRes().put(i, ks.cfMetaData().keySet());
}
},
LISTKEYSPACES {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
res.getOpsRes().put(i, Schema.instance.getNonSystemTables());
}
},
CONSISTENCY {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
ConsistencyLevel level = ConsistencyLevel.valueOf((String) op.getOp().get("level"));
res.getOpsRes().put(i, "OK");
state.consistency = level;
}
},
CREATEKEYSPACE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
Collection<CFMetaData> cfDefs = new ArrayList<CFMetaData>(0);
IntraOp op = req.getE().get(i);
String ks = (String) op.getOp().get("name");
KsDef def = new KsDef();
def.setName(ks);
def.setStrategy_class("SimpleStrategy");
Map<String, String> strat = new HashMap<String, String>();
strat.put("replication_factor", "1");
def.setStrategy_options(strat);
KSMetaData ksm = null;
try {
ksm = KSMetaData.fromThrift(def,
cfDefs.toArray(new CFMetaData[cfDefs.size()]));
} catch (ConfigurationException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
try {
MigrationManager.announceNewKeyspace(ksm);
} catch (ConfigurationException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
res.getOpsRes().put(i, "OK");
}
},
CREATECOLUMNFAMILY {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String cf = (String) op.getOp().get("name");
CfDef def = new CfDef();
def.setName(cf);
def.setKeyspace(state.currentKeyspace);
def.unsetId();
CFMetaData cfm = null;
try {
cfm = CFMetaData.fromThrift(def);
cfm.addDefaultIndexNames();
} catch (org.apache.cassandra.exceptions.InvalidRequestException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
} catch (ConfigurationException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
try {
MigrationManager.announceNewColumnFamily(cfm);
} catch (ConfigurationException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
res.getOpsRes().put(i, "OK");
}
},
FOREACH {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
}
},
COLUMNPREDICATE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
}
},
SLICE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
List<Map> finalResults = new ArrayList<Map>();
ByteBuffer rowkey = IntraService.byteBufferForObject(IntraService.resolveObject(op.getOp().get("rowkey"), req, res, state, i));
ByteBuffer start = IntraService.byteBufferForObject(IntraService.resolveObject(op.getOp().get("start"), req, res, state, i));
ByteBuffer end = IntraService.byteBufferForObject(IntraService.resolveObject(op.getOp().get("end"), req, res, state, i));
List<ReadCommand> commands = new ArrayList<ReadCommand>(1);
ColumnPath cp = new ColumnPath();
cp.setColumn_family(state.currentColumnFamily);
QueryPath qp = new QueryPath(cp);
SliceFromReadCommand sr = new SliceFromReadCommand(state.currentKeyspace, rowkey, qp, start, end, false, 100);
commands.add(sr);
List<Row> results = null;
try {
results = StorageProxy.read(commands, state.consistency);
ColumnFamily cf = results.get(0).cf;
if (cf == null){ //cf= null is no data
} else {
IntraService.readCf(cf, finalResults, state);
}
res.getOpsRes().put(i,finalResults);
} catch (ReadTimeoutException | org.apache.cassandra.exceptions.UnavailableException
| IsBootstrappingException | IOException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
res.getOpsRes().put(i, finalResults);
}
},
GETREF {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
}
},
GET {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
List<Map> finalResults = new ArrayList<Map>();
ByteBuffer rowkey = IntraService.byteBufferForObject(IntraService.resolveObject(
op.getOp().get("rowkey"), req, res, state, i));
ByteBuffer column = IntraService.byteBufferForObject(IntraService.resolveObject(
op.getOp().get("name"), req, res, state, i));
QueryPath path = new QueryPath(state.currentColumnFamily, null);
List<ByteBuffer> nameAsList = Arrays.asList(column);
ReadCommand command = new SliceByNamesReadCommand(state.currentKeyspace,
rowkey, path, nameAsList);
List<Row> rows = null;
try {
rows = StorageProxy.read(Arrays.asList(command), state.consistency);
ColumnFamily cf = rows.get(0).cf;
if (cf == null) { // cf= null is no data
} else {
IntraService.readCf(cf, finalResults, state);
}
res.getOpsRes().put(i, finalResults);
} catch (ReadTimeoutException | org.apache.cassandra.exceptions.UnavailableException
| IsBootstrappingException | IOException e) {
res.getOpsRes().put(i, e.getMessage());
return;
}
}
},
SET {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
RowMutation rm = new RowMutation(state.currentKeyspace,
IntraService.byteBufferForObject(
IntraService.resolveObject(op.getOp().get("rowkey"), req, res, state, i)
));
QueryPath qp = new QueryPath(state.currentColumnFamily,null, IntraService.byteBufferForObject(
IntraService.resolveObject(op.getOp().get("name"), req, res, state, i)
) );
Object val = op.getOp().get("value");
rm.add(qp, IntraService.byteBufferForObject(
IntraService.resolveObject(val, req, res, state, i)
), (Long) (state.autoTimestamp ? state.nanotime : op.getOp().get("timestamp")));
try {
StorageProxy.mutate(Arrays.asList(rm), state.consistency);
res.getOpsRes().put(i, "OK");
} catch (WriteTimeoutException | org.apache.cassandra.exceptions.UnavailableException | OverloadedException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
}
},
AUTOTIMESTAMP {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
state.autoTimestamp = true;
res.getOpsRes().put(i, "OK");
}
},
SETKEYSPACE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
state.currentKeyspace = (String) op.getOp().get("keyspace");
res.getOpsRes().put(i, "OK");
}
},
SETCOLUMNFAMILY {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
state.currentColumnFamily = (String) op.getOp().get("columnfamily");
res.getOpsRes().put(i, "OK");
}
},
ASSUME {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
IntraMetaData imd = new IntraMetaData();
imd.keyspace = (String) op.getOp().get("keyspace");
imd.columnfamily = (String) op.getOp().get("columnfamily");
imd.type = (String) op.getOp().get("type");
state.meta.put( imd , (String) op.getOp().get("clazz") );
res.getOpsRes().put(i, "OK");
}
},
CREATEPROCESSOR {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String name = (String) op.getOp().get("name");
GroovyClassLoader gc = new GroovyClassLoader();
Class c = gc.parseClass((String) op.getOp().get("value") );
Processor p = null;
try {
p = (Processor) c.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
res.setExceptionAndId(e, i);
return;
}
IntraState.processors.put(name,p);
}
},
PROCESS {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String processorName = (String) op.getOp().get("processorname");
Map params = (Map) op.getOp().get("params");
Processor p = state.processors.get(processorName);
Integer inputId = (Integer) op.getOp().get("input");
List<Map> toProcess = (List<Map>)res.getOpsRes().get(inputId);
List<Map> results = p.process(toProcess);
res.getOpsRes().put(i, results);
}
},
DROPKEYSPACE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
}
},
CREATEFILTER {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String name = (String) op.getOp().get("name");
GroovyClassLoader gc = new GroovyClassLoader();
Class c = gc.parseClass((String) op.getOp().get("value") );
Filter f = null;
try {
f = (Filter) c.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
res.setExceptionAndId(e, i);
return;
}
IntraState.filters.put(name, f);
}
},
FILTERMODE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String name = (String) op.getOp().get("name");
Boolean on = (Boolean) op.getOp().get("on");
if (on){
Filter f = state.filters.get(name);
if (f == null){
res.setExceptionAndId("filter "+name +" not found", i);
return;
} else {
state.currentFilter=f;
}
} else {
state.currentFilter = null;
}
}
},
CQLQUERY {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
ClientState clientState = new ClientState();
try {
clientState.setCQLVersion((String) op.getOp().get("version"));
clientState.setKeyspace(state.currentKeyspace);
} catch (InvalidRequestException e) {
res.setExceptionAndId(e, i);
return;
}
QueryState queryState = new QueryState(clientState);
ResultMessage rm = null;
try {
rm = QueryProcessor.process((String) op.getOp().get("query"), state.consistency, queryState);
} catch (RequestExecutionException | RequestValidationException e) {
res.setExceptionAndId(e, i);
return;
}
//ToDo maybe processInternal
CqlResult result = rm.toThriftResult();
List<CqlRow> rows = result.getRows();
List<HashMap> returnRows = new ArrayList<HashMap>();
for (CqlRow row: rows){
List<org.apache.cassandra.thrift.Column> columns = row.getColumns();
for (org.apache.cassandra.thrift.Column c: columns){
HashMap m = new HashMap();
m.put("value", c.value);
m.put("name", c.name);
returnRows.add(m);
}
}
res.getOpsRes().put(i,returnRows);
}
},
CLEAR {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
int id = (Integer) op.getOp().get("id");
res.getOpsRes().put(id, new ArrayList<HashMap>());
}
},
CREATEMULTIPROCESS {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String name = (String) op.getOp().get("name");
GroovyClassLoader gc = new GroovyClassLoader();
Class c = gc.parseClass((String) op.getOp().get("value") );
MultiProcessor p = null;
try {
p = (MultiProcessor) c.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
res.setExceptionAndId(e, i);
return;
}
IntraState.multiProcessors.put(name, p);
}
},
MULTIPROCESS {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
String name = (String) op.getOp().get("name");
Map params = (Map) op.getOp().get("params");
//Processor p = state.processors.get(processorName);
MultiProcessor p = state.multiProcessors.get(name);
List<Map> mpResults = p.multiProcess(res.getOpsRes(), params);
res.getOpsRes().put(i, mpResults);
}
},
STATE {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i,
Vertx vertx) {
IntraOp op = req.getE().get(i);
if (((String)op.getOp().get("mode")).equalsIgnoreCase("save")){
res.getOpsRes().put(i, state.saveState(state));
} else if (((String)op.getOp().get("mode")).equalsIgnoreCase("get")){
Integer id = (Integer) op.getOp().get("id");
IntraState other = state.getState(id.intValue());
state.currentColumnFamily=other.currentColumnFamily;
state.currentKeyspace=other.currentKeyspace;
state.consistency = other.consistency;
state.autoTimestamp = other.autoTimestamp;
state.nanotime = other.nanotime;
state.meta = other.meta;
state.currentFilter = other.currentFilter;
}
}
},
BATCHSET {
@Override
public void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx) {
IntraOp op = req.getE().get(i);
List<Map> rows = (List<Map>) op.getOp().get("rows");
List<RowMutation> m = new ArrayList<RowMutation>();
for (Map row:rows){
//code is mostly cloned from Type.SET
RowMutation rm = new RowMutation(state.currentKeyspace,
IntraService.byteBufferForObject(IntraService.resolveObject(
row.get("rowkey"), req, res, state, i)));
QueryPath qp = new QueryPath(state.currentColumnFamily, null,
IntraService.byteBufferForObject(IntraService.resolveObject(
row.get("name"), req, res, state, i)));
rm.add( qp,IntraService.byteBufferForObject(IntraService.resolveObject(
row.get("value"), req, res, state, i)),
(Long) (state.autoTimestamp ? state.nanotime : op.getOp().get(
"timestamp")));
m.add(rm);
}
try {
StorageProxy.mutate(m, state.consistency);
res.getOpsRes().put(i, "OK");
} catch (WriteTimeoutException
| org.apache.cassandra.exceptions.UnavailableException
| OverloadedException e) {
res.setExceptionAndId(e.getMessage(), i);
return;
}
}
};
public abstract void execute(IntraReq req, IntraRes res, IntraState state, int i, Vertx vertx);
}
}