-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StandardTitanGraph.java
808 lines (691 loc) · 37.1 KB
/
StandardTitanGraph.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
package com.thinkaurelius.titan.graphdb.database;
import com.carrotsearch.hppc.LongArrayList;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.*;
import com.thinkaurelius.titan.core.*;
import com.thinkaurelius.titan.core.schema.ConsistencyModifier;
import com.thinkaurelius.titan.core.schema.SchemaStatus;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.thinkaurelius.titan.diskstorage.*;
import com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration;
import com.thinkaurelius.titan.diskstorage.configuration.Configuration;
import com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration;
import com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration;
import com.thinkaurelius.titan.diskstorage.indexing.IndexEntry;
import com.thinkaurelius.titan.diskstorage.indexing.IndexTransaction;
import com.thinkaurelius.titan.diskstorage.keycolumnvalue.*;
import com.thinkaurelius.titan.diskstorage.keycolumnvalue.cache.KCVSCache;
import com.thinkaurelius.titan.diskstorage.log.Log;
import com.thinkaurelius.titan.diskstorage.log.Message;
import com.thinkaurelius.titan.diskstorage.log.ReadMarker;
import com.thinkaurelius.titan.diskstorage.log.kcvs.KCVSLog;
import com.thinkaurelius.titan.diskstorage.util.RecordIterator;
import com.thinkaurelius.titan.diskstorage.util.StaticArrayEntry;
import com.thinkaurelius.titan.diskstorage.util.time.TimestampProvider;
import com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration;
import com.thinkaurelius.titan.graphdb.database.cache.SchemaCache;
import com.thinkaurelius.titan.graphdb.database.idassigner.VertexIDAssigner;
import com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler;
import com.thinkaurelius.titan.graphdb.database.log.LogTxStatus;
import com.thinkaurelius.titan.graphdb.database.log.TransactionLogHeader;
import com.thinkaurelius.titan.graphdb.database.management.ManagementLogger;
import com.thinkaurelius.titan.graphdb.database.management.ManagementSystem;
import com.thinkaurelius.titan.graphdb.database.serialize.Serializer;
import com.thinkaurelius.titan.graphdb.idmanagement.IDManager;
import com.thinkaurelius.titan.graphdb.internal.InternalRelation;
import com.thinkaurelius.titan.graphdb.internal.InternalRelationType;
import com.thinkaurelius.titan.graphdb.internal.InternalVertex;
import com.thinkaurelius.titan.graphdb.internal.InternalVertexLabel;
import com.thinkaurelius.titan.graphdb.query.QueryUtil;
import com.thinkaurelius.titan.graphdb.relations.EdgeDirection;
import com.thinkaurelius.titan.graphdb.tinkerpop.TitanBlueprintsGraph;
import com.thinkaurelius.titan.graphdb.tinkerpop.TitanFeatures;
import com.thinkaurelius.titan.graphdb.tinkerpop.optimize.AdjacentVertexFilterOptimizerStrategy;
import com.thinkaurelius.titan.graphdb.tinkerpop.optimize.TitanGraphStepStrategy;
import com.thinkaurelius.titan.graphdb.tinkerpop.optimize.TitanLocalQueryOptimizerStrategy;
import com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx;
import com.thinkaurelius.titan.graphdb.transaction.StandardTransactionBuilder;
import com.thinkaurelius.titan.graphdb.transaction.TransactionConfiguration;
import com.thinkaurelius.titan.graphdb.types.CompositeIndexType;
import com.thinkaurelius.titan.graphdb.types.MixedIndexType;
import com.thinkaurelius.titan.graphdb.types.system.BaseKey;
import com.thinkaurelius.titan.graphdb.types.system.BaseRelationType;
import com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex;
import com.thinkaurelius.titan.graphdb.util.ExceptionFactory;
import com.thinkaurelius.titan.util.system.IOUtils;
import com.thinkaurelius.titan.util.system.TXUtils;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.REGISTRATION_TIME;
import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.ROOT_NS;
public class StandardTitanGraph extends TitanBlueprintsGraph {
private static final Logger log =
LoggerFactory.getLogger(StandardTitanGraph.class);
static {
TraversalStrategies graphStrategies = TraversalStrategies.GlobalCache.getStrategies(Graph.class).clone()
.addStrategies(AdjacentVertexFilterOptimizerStrategy.instance(), TitanLocalQueryOptimizerStrategy.instance(), TitanGraphStepStrategy.instance());
//Register with cache
TraversalStrategies.GlobalCache.registerStrategies(StandardTitanGraph.class, graphStrategies);
TraversalStrategies.GlobalCache.registerStrategies(StandardTitanTx.class, graphStrategies);
}
private GraphDatabaseConfiguration config;
private Backend backend;
private IDManager idManager;
private VertexIDAssigner idAssigner;
private TimestampProvider times;
//Serializers
protected IndexSerializer indexSerializer;
protected EdgeSerializer edgeSerializer;
protected Serializer serializer;
//Caches
public SliceQuery vertexExistenceQuery;
private RelationQueryCache queryCache;
private SchemaCache schemaCache;
//Log
private ManagementLogger mgmtLogger;
//Shutdown hook
private volatile ShutdownThread shutdownHook;
private volatile boolean isOpen = true;
private AtomicLong txCounter;
private Set<StandardTitanTx> openTransactions;
public StandardTitanGraph(GraphDatabaseConfiguration configuration) {
this.config = configuration;
this.backend = configuration.getBackend();
this.idAssigner = config.getIDAssigner(backend);
this.idManager = idAssigner.getIDManager();
this.serializer = config.getSerializer();
StoreFeatures storeFeatures = backend.getStoreFeatures();
this.indexSerializer = new IndexSerializer(configuration.getConfiguration(), this.serializer,
this.backend.getIndexInformation(), storeFeatures.isDistributed() && storeFeatures.isKeyOrdered());
this.edgeSerializer = new EdgeSerializer(this.serializer);
this.vertexExistenceQuery = edgeSerializer.getQuery(BaseKey.VertexExists, Direction.OUT, new EdgeSerializer.TypedInterval[0]).setLimit(1);
this.queryCache = new RelationQueryCache(this.edgeSerializer);
this.schemaCache = configuration.getTypeCache(typeCacheRetrieval);
this.times = configuration.getTimestampProvider();
isOpen = true;
txCounter = new AtomicLong(0);
openTransactions = Collections.newSetFromMap(new ConcurrentHashMap<StandardTitanTx, Boolean>(100, 0.75f, 1));
//Register instance and ensure uniqueness
String uniqueInstanceId = configuration.getUniqueGraphId();
ModifiableConfiguration globalConfig = GraphDatabaseConfiguration.getGlobalSystemConfig(backend);
if (globalConfig.has(REGISTRATION_TIME, uniqueInstanceId)) {
throw new TitanException(String.format("A Titan graph with the same instance id [%s] is already open. Might required forced shutdown.", uniqueInstanceId));
}
globalConfig.set(REGISTRATION_TIME, times.getTime(), uniqueInstanceId);
Log mgmtLog = backend.getSystemMgmtLog();
mgmtLogger = new ManagementLogger(this, mgmtLog, schemaCache, this.times);
mgmtLog.registerReader(ReadMarker.fromNow(), mgmtLogger);
shutdownHook = new ShutdownThread(this);
Runtime.getRuntime().addShutdownHook(shutdownHook);
log.debug("Installed shutdown hook {}", shutdownHook, new Throwable("Hook creation trace"));
}
@Override
public boolean isOpen() {
return isOpen;
}
@Override
public boolean isClosed() {
return !isOpen();
}
@Override
public synchronized void close() throws TitanException {
try {
closeInternal();
} finally {
removeHook();
}
}
private synchronized void closeInternal() {
if (!isOpen) return;
Map<TitanTransaction, RuntimeException> txCloseExceptions = new HashMap<>();
try {
//Unregister instance
String uniqueid = null;
try {
uniqueid = config.getUniqueGraphId();
ModifiableConfiguration globalConfig = GraphDatabaseConfiguration.getGlobalSystemConfig(backend);
globalConfig.remove(REGISTRATION_TIME, uniqueid);
} catch (Exception e) {
log.warn("Unable to remove graph instance uniqueid {}", uniqueid, e);
}
/* Assuming a couple of properties about openTransactions:
* 1. no concurrent modifications during graph shutdown
* 2. all contained txs are open
*/
for (StandardTitanTx otx : openTransactions) {
try {
otx.close();
} catch (RuntimeException e) {
// Catch and store these exceptions, but proceed wit the loop
// Any remaining txs on the iterator should get a chance to close before we throw up
log.warn("Unable to close transaction {}", otx, e);
txCloseExceptions.put(otx, e);
}
}
super.close();
IOUtils.closeQuietly(idAssigner);
IOUtils.closeQuietly(backend);
IOUtils.closeQuietly(queryCache);
IOUtils.closeQuietly(serializer);
} finally {
isOpen = false;
}
// Throw an exception if at least one transaction failed to close
if (1 == txCloseExceptions.size()) {
// TP3's test suite requires that this be of type ISE
throw new IllegalStateException("Unable to close transaction",
Iterables.getOnlyElement(txCloseExceptions.values()));
} else if (1 < txCloseExceptions.size()) {
throw new IllegalStateException(String.format(
"Unable to close %s transactions (see warnings in log output for details)",
txCloseExceptions.size()));
}
}
private synchronized void removeHook() {
if (null == shutdownHook)
return;
ShutdownThread tmp = shutdownHook;
shutdownHook = null;
// Remove shutdown hook to avoid reference retention
try {
Runtime.getRuntime().removeShutdownHook(tmp);
log.debug("Removed shutdown hook {}", tmp);
} catch (IllegalStateException e) {
log.warn("Failed to remove shutdown hook", e);
}
}
// ################### Simple Getters #########################
@Override
public Features features() {
return TitanFeatures.getFeatures(this, backend.getStoreFeatures());
}
public IndexSerializer getIndexSerializer() {
return indexSerializer;
}
public Backend getBackend() {
return backend;
}
public IDManager getIDManager() {
return idManager;
}
public EdgeSerializer getEdgeSerializer() {
return edgeSerializer;
}
public Serializer getDataSerializer() {
return serializer;
}
//TODO: premature optimization, re-evaluate later
// public RelationQueryCache getQueryCache() {
// return queryCache;
// }
public SchemaCache getSchemaCache() {
return schemaCache;
}
public GraphDatabaseConfiguration getConfiguration() {
return config;
}
@Override
public TitanManagement openManagement() {
return new ManagementSystem(this,backend.getGlobalSystemConfig(),backend.getSystemMgmtLog(), mgmtLogger, schemaCache);
}
public Set<? extends TitanTransaction> getOpenTransactions() {
return Sets.newHashSet(openTransactions);
}
// ################### TRANSACTIONS #########################
@Override
public TitanTransaction newTransaction() {
return buildTransaction().start();
}
@Override
public StandardTransactionBuilder buildTransaction() {
return new StandardTransactionBuilder(getConfiguration(), this);
}
@Override
public TitanTransaction newThreadBoundTransaction() {
return buildTransaction().threadBound().start();
}
public StandardTitanTx newTransaction(final TransactionConfiguration configuration) {
if (!isOpen) ExceptionFactory.graphShutdown();
try {
StandardTitanTx tx = new StandardTitanTx(this, configuration);
tx.setBackendTransaction(openBackendTransaction(tx));
openTransactions.add(tx);
return tx;
} catch (BackendException e) {
throw new TitanException("Could not start new transaction", e);
}
}
private BackendTransaction openBackendTransaction(StandardTitanTx tx) throws BackendException {
IndexSerializer.IndexInfoRetriever retriever = indexSerializer.getIndexInfoRetriever(tx);
return backend.beginTransaction(tx.getConfiguration(), retriever);
}
public void closeTransaction(StandardTitanTx tx) {
openTransactions.remove(tx);
}
// ################### READ #########################
private final SchemaCache.StoreRetrieval typeCacheRetrieval = new SchemaCache.StoreRetrieval() {
@Override
public Long retrieveSchemaByName(String typeName) {
// Get a consistent tx
Configuration customTxOptions = backend.getStoreFeatures().getKeyConsistentTxConfig();
StandardTitanTx consistentTx = null;
try {
consistentTx = StandardTitanGraph.this.newTransaction(new StandardTransactionBuilder(getConfiguration(),
StandardTitanGraph.this, customTxOptions).groupName(GraphDatabaseConfiguration.METRICS_SCHEMA_PREFIX_DEFAULT));
consistentTx.getTxHandle().disableCache();
TitanVertex v = Iterables.getOnlyElement(QueryUtil.getVertices(consistentTx, BaseKey.SchemaName, typeName), null);
return v!=null?v.longId():null;
} finally {
TXUtils.rollbackQuietly(consistentTx);
}
}
@Override
public EntryList retrieveSchemaRelations(final long schemaId, final BaseRelationType type, final Direction dir) {
SliceQuery query = queryCache.getQuery(type,dir);
Configuration customTxOptions = backend.getStoreFeatures().getKeyConsistentTxConfig();
StandardTitanTx consistentTx = null;
try {
consistentTx = StandardTitanGraph.this.newTransaction(new StandardTransactionBuilder(getConfiguration(),
StandardTitanGraph.this, customTxOptions).groupName(GraphDatabaseConfiguration.METRICS_SCHEMA_PREFIX_DEFAULT));
consistentTx.getTxHandle().disableCache();
EntryList result = edgeQuery(schemaId, query, consistentTx.getTxHandle());
return result;
} finally {
TXUtils.rollbackQuietly(consistentTx);
}
}
};
public RecordIterator<Long> getVertexIDs(final BackendTransaction tx) {
Preconditions.checkArgument(backend.getStoreFeatures().hasOrderedScan() ||
backend.getStoreFeatures().hasUnorderedScan(),
"The configured storage backend does not support global graph operations - use Faunus instead");
final KeyIterator keyiter;
if (backend.getStoreFeatures().hasUnorderedScan()) {
keyiter = tx.edgeStoreKeys(vertexExistenceQuery);
} else {
keyiter = tx.edgeStoreKeys(new KeyRangeQuery(IDHandler.MIN_KEY, IDHandler.MAX_KEY, vertexExistenceQuery));
}
return new RecordIterator<Long>() {
@Override
public boolean hasNext() {
return keyiter.hasNext();
}
@Override
public Long next() {
return idManager.getKeyID(keyiter.next());
}
@Override
public void close() throws IOException {
keyiter.close();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Removal not supported");
}
};
}
public EntryList edgeQuery(long vid, SliceQuery query, BackendTransaction tx) {
Preconditions.checkArgument(vid > 0);
return tx.edgeStoreQuery(new KeySliceQuery(idManager.getKey(vid), query));
}
public List<EntryList> edgeMultiQuery(LongArrayList vids, SliceQuery query, BackendTransaction tx) {
Preconditions.checkArgument(vids != null && !vids.isEmpty());
List<StaticBuffer> vertexIds = new ArrayList<StaticBuffer>(vids.size());
for (int i = 0; i < vids.size(); i++) {
Preconditions.checkArgument(vids.get(i) > 0);
vertexIds.add(idManager.getKey(vids.get(i)));
}
Map<StaticBuffer,EntryList> result = tx.edgeStoreMultiQuery(vertexIds, query);
List<EntryList> resultList = new ArrayList<EntryList>(result.size());
for (StaticBuffer v : vertexIds) resultList.add(result.get(v));
return resultList;
}
// ################### WRITE #########################
public void assignID(InternalRelation relation) {
idAssigner.assignID(relation);
}
public void assignID(InternalVertex vertex, VertexLabel label) {
idAssigner.assignID(vertex,label);
}
public static boolean acquireLock(InternalRelation relation, int pos, boolean acquireLocksConfig) {
InternalRelationType type = (InternalRelationType)relation.getType();
return acquireLocksConfig && type.getConsistencyModifier()== ConsistencyModifier.LOCK &&
( type.multiplicity().isUnique(EdgeDirection.fromPosition(pos))
|| pos==0 && type.multiplicity()== Multiplicity.SIMPLE);
}
public static boolean acquireLock(CompositeIndexType index, boolean acquireLocksConfig) {
return acquireLocksConfig && index.getConsistencyModifier()==ConsistencyModifier.LOCK
&& index.getCardinality()!= Cardinality.LIST;
}
/**
* The TTL of a relation (edge or property) is the minimum of:
* 1) The TTL configured of the relation type (if exists)
* 2) The TTL configured for the label any of the relation end point vertices (if exists)
*
* @param rel relation to determine the TTL for
* @return
*/
public static int getTTL(InternalRelation rel) {
assert rel.isNew();
InternalRelationType baseType = (InternalRelationType) rel.getType();
assert baseType.getBaseType()==null;
int ttl = 0;
Integer ettl = baseType.getTTL();
if (ettl>0) ttl = ettl;
for (int i=0;i<rel.getArity();i++) {
int vttl = getTTL(rel.getVertex(i));
if (vttl>0 && (vttl<ttl || ttl<=0)) ttl = vttl;
}
return ttl;
}
public static int getTTL(InternalVertex v) {
assert v.hasId();
if (IDManager.VertexIDType.UnmodifiableVertex.is(v.longId())) {
assert v.isNew() : "Should not be able to add relations to existing static vertices: " + v;
return ((InternalVertexLabel)v.vertexLabel()).getTTL();
} else return 0;
}
private static class ModificationSummary {
final boolean hasModifications;
final boolean has2iModifications;
private ModificationSummary(boolean hasModifications, boolean has2iModifications) {
this.hasModifications = hasModifications;
this.has2iModifications = has2iModifications;
}
}
public ModificationSummary prepareCommit(final Collection<InternalRelation> addedRelations,
final Collection<InternalRelation> deletedRelations,
final Predicate<InternalRelation> filter,
final BackendTransaction mutator, final StandardTitanTx tx,
final boolean acquireLocks) throws BackendException {
ListMultimap<Long, InternalRelation> mutations = ArrayListMultimap.create();
ListMultimap<InternalVertex, InternalRelation> mutatedProperties = ArrayListMultimap.create();
List<IndexSerializer.IndexUpdate> indexUpdates = Lists.newArrayList();
//1) Collect deleted edges and their index updates and acquire edge locks
for (InternalRelation del : Iterables.filter(deletedRelations,filter)) {
Preconditions.checkArgument(del.isRemoved());
for (int pos = 0; pos < del.getLen(); pos++) {
InternalVertex vertex = del.getVertex(pos);
if (pos == 0 || !del.isLoop()) {
if (del.isProperty()) mutatedProperties.put(vertex,del);
mutations.put(vertex.longId(), del);
}
if (acquireLock(del,pos,acquireLocks)) {
Entry entry = edgeSerializer.writeRelation(del, pos, tx);
mutator.acquireEdgeLock(idManager.getKey(vertex.longId()), entry);
}
}
indexUpdates.addAll(indexSerializer.getIndexUpdates(del));
}
//2) Collect added edges and their index updates and acquire edge locks
for (InternalRelation add : Iterables.filter(addedRelations,filter)) {
Preconditions.checkArgument(add.isNew());
for (int pos = 0; pos < add.getLen(); pos++) {
InternalVertex vertex = add.getVertex(pos);
if (pos == 0 || !add.isLoop()) {
if (add.isProperty()) mutatedProperties.put(vertex,add);
mutations.put(vertex.longId(), add);
}
if (!vertex.isNew() && acquireLock(add,pos,acquireLocks)) {
Entry entry = edgeSerializer.writeRelation(add, pos, tx);
mutator.acquireEdgeLock(idManager.getKey(vertex.longId()), entry.getColumn());
}
}
indexUpdates.addAll(indexSerializer.getIndexUpdates(add));
}
//3) Collect all index update for vertices
for (InternalVertex v : mutatedProperties.keySet()) {
indexUpdates.addAll(indexSerializer.getIndexUpdates(v,mutatedProperties.get(v)));
}
//4) Acquire index locks (deletions first)
for (IndexSerializer.IndexUpdate update : indexUpdates) {
if (!update.isCompositeIndex() || !update.isDeletion()) continue;
CompositeIndexType iIndex = (CompositeIndexType) update.getIndex();
if (acquireLock(iIndex,acquireLocks)) {
mutator.acquireIndexLock((StaticBuffer)update.getKey(), (Entry)update.getEntry());
}
}
for (IndexSerializer.IndexUpdate update : indexUpdates) {
if (!update.isCompositeIndex() || !update.isAddition()) continue;
CompositeIndexType iIndex = (CompositeIndexType) update.getIndex();
if (acquireLock(iIndex,acquireLocks)) {
mutator.acquireIndexLock((StaticBuffer)update.getKey(), ((Entry)update.getEntry()).getColumn());
}
}
//5) Add relation mutations
for (Long vertexid : mutations.keySet()) {
Preconditions.checkArgument(vertexid > 0, "Vertex has no id: %s", vertexid);
List<InternalRelation> edges = mutations.get(vertexid);
List<Entry> additions = new ArrayList<Entry>(edges.size());
List<Entry> deletions = new ArrayList<Entry>(Math.max(10, edges.size() / 10));
for (InternalRelation edge : edges) {
InternalRelationType baseType = (InternalRelationType) edge.getType();
assert baseType.getBaseType()==null;
for (InternalRelationType type : baseType.getRelationIndexes()) {
if (type.getStatus()== SchemaStatus.DISABLED) continue;
for (int pos = 0; pos < edge.getArity(); pos++) {
if (!type.isUnidirected(Direction.BOTH) && !type.isUnidirected(EdgeDirection.fromPosition(pos)))
continue; //Directionality is not covered
if (edge.getVertex(pos).longId()==vertexid) {
StaticArrayEntry entry = edgeSerializer.writeRelation(edge, type, pos, tx);
if (edge.isRemoved()) {
deletions.add(entry);
} else {
Preconditions.checkArgument(edge.isNew());
int ttl = getTTL(edge);
if (ttl > 0) {
entry.setMetaData(EntryMetaData.TTL, ttl);
}
additions.add(entry);
}
}
}
}
}
StaticBuffer vertexKey = idManager.getKey(vertexid);
mutator.mutateEdges(vertexKey, additions, deletions);
}
//6) Add index updates
boolean has2iMods = false;
for (IndexSerializer.IndexUpdate indexUpdate : indexUpdates) {
assert indexUpdate.isAddition() || indexUpdate.isDeletion();
if (indexUpdate.isCompositeIndex()) {
IndexSerializer.IndexUpdate<StaticBuffer,Entry> update = indexUpdate;
if (update.isAddition())
mutator.mutateIndex(update.getKey(), Lists.newArrayList(update.getEntry()), KCVSCache.NO_DELETIONS);
else
mutator.mutateIndex(update.getKey(), KeyColumnValueStore.NO_ADDITIONS, Lists.newArrayList(update.getEntry()));
} else {
IndexSerializer.IndexUpdate<String,IndexEntry> update = indexUpdate;
has2iMods = true;
IndexTransaction itx = mutator.getIndexTransaction(update.getIndex().getBackingIndexName());
String indexStore = ((MixedIndexType)update.getIndex()).getStoreName();
if (update.isAddition())
itx.add(indexStore, update.getKey(), update.getEntry(), update.getElement().isNew());
else
itx.delete(indexStore,update.getKey(),update.getEntry().field,update.getEntry().value,update.getElement().isRemoved());
}
}
return new ModificationSummary(!mutations.isEmpty(),has2iMods);
}
private static final Predicate<InternalRelation> SCHEMA_FILTER = new Predicate<InternalRelation>() {
@Override
public boolean apply(@Nullable InternalRelation internalRelation) {
return internalRelation.getType() instanceof BaseRelationType && internalRelation.getVertex(0) instanceof TitanSchemaVertex;
}
};
private static final Predicate<InternalRelation> NO_SCHEMA_FILTER = new Predicate<InternalRelation>() {
@Override
public boolean apply(@Nullable InternalRelation internalRelation) {
return !SCHEMA_FILTER.apply(internalRelation);
}
};
private static final Predicate<InternalRelation> NO_FILTER = Predicates.alwaysTrue();
public void commit(final Collection<InternalRelation> addedRelations,
final Collection<InternalRelation> deletedRelations, final StandardTitanTx tx) {
if (addedRelations.isEmpty() && deletedRelations.isEmpty()) return;
//1. Finalize transaction
log.debug("Saving transaction. Added {}, removed {}", addedRelations.size(), deletedRelations.size());
if (!tx.getConfiguration().hasCommitTime()) tx.getConfiguration().setCommitTime(times.getTime());
final Instant txTimestamp = tx.getConfiguration().getCommitTime();
final long transactionId = txCounter.incrementAndGet();
//2. Assign TitanVertex IDs
if (!tx.getConfiguration().hasAssignIDsImmediately())
idAssigner.assignIDs(addedRelations);
//3. Commit
BackendTransaction mutator = tx.getTxHandle();
final boolean acquireLocks = tx.getConfiguration().hasAcquireLocks();
final boolean hasTxIsolation = backend.getStoreFeatures().hasTxIsolation();
final boolean logTransaction = config.hasLogTransactions() && !tx.getConfiguration().hasEnabledBatchLoading();
final KCVSLog txLog = logTransaction?backend.getSystemTxLog():null;
final TransactionLogHeader txLogHeader = new TransactionLogHeader(transactionId,txTimestamp, times);
ModificationSummary commitSummary;
try {
//3.1 Log transaction (write-ahead log) if enabled
if (logTransaction) {
//[FAILURE] Inability to log transaction fails the transaction by escalation since it's likely due to unavailability of primary
//storage backend.
txLog.add(txLogHeader.serializeModifications(serializer, LogTxStatus.PRECOMMIT, tx, addedRelations, deletedRelations),txLogHeader.getLogKey());
}
//3.2 Commit schema elements and their associated relations in a separate transaction if backend does not support
// transactional isolation
boolean hasSchemaElements = !Iterables.isEmpty(Iterables.filter(deletedRelations,SCHEMA_FILTER))
|| !Iterables.isEmpty(Iterables.filter(addedRelations,SCHEMA_FILTER));
Preconditions.checkArgument(!hasSchemaElements || (!tx.getConfiguration().hasEnabledBatchLoading() && acquireLocks),
"Attempting to create schema elements in inconsistent state");
if (hasSchemaElements && !hasTxIsolation) {
/*
* On storage without transactional isolation, create separate
* backend transaction for schema aspects to make sure that
* those are persisted prior to and independently of other
* mutations in the tx. If the storage supports transactional
* isolation, then don't create a separate tx.
*/
final BackendTransaction schemaMutator = openBackendTransaction(tx);
try {
//[FAILURE] If the preparation throws an exception abort directly - nothing persisted since batch-loading cannot be enabled for schema elements
commitSummary = prepareCommit(addedRelations,deletedRelations, SCHEMA_FILTER, schemaMutator, tx, acquireLocks);
assert commitSummary.hasModifications && !commitSummary.has2iModifications;
} catch (Throwable e) {
//Roll back schema tx and escalate exception
schemaMutator.rollback();
throw e;
}
try {
schemaMutator.commit();
} catch (Throwable e) {
//[FAILURE] Primary persistence failed => abort and escalate exception, nothing should have been persisted
log.error("Could not commit transaction ["+transactionId+"] due to storage exception in system-commit",e);
throw e;
}
}
//[FAILURE] Exceptions during preparation here cause the entire transaction to fail on transactional systems
//or just the non-system part on others. Nothing has been persisted unless batch-loading
commitSummary = prepareCommit(addedRelations,deletedRelations, hasTxIsolation? NO_FILTER : NO_SCHEMA_FILTER, mutator, tx, acquireLocks);
if (commitSummary.hasModifications) {
String logTxIdentifier = tx.getConfiguration().getLogIdentifier();
boolean hasSecondaryPersistence = logTxIdentifier!=null || commitSummary.has2iModifications;
//1. Commit storage - failures lead to immediate abort
//1a. Add success message to tx log which will be committed atomically with all transactional changes so that we can recover secondary failures
// This should not throw an exception since the mutations are just cached. If it does, it will be escalated since its critical
if (logTransaction) {
txLog.add(txLogHeader.serializePrimary(serializer,
hasSecondaryPersistence?LogTxStatus.PRIMARY_SUCCESS:LogTxStatus.COMPLETE_SUCCESS),
txLogHeader.getLogKey(),mutator.getTxLogPersistor());
}
try {
mutator.commitStorage();
} catch (Throwable e) {
//[FAILURE] If primary storage persistence fails abort directly (only schema could have been persisted)
log.error("Could not commit transaction ["+transactionId+"] due to storage exception in commit",e);
throw e;
}
if (hasSecondaryPersistence) {
LogTxStatus status = LogTxStatus.SECONDARY_SUCCESS;
Map<String,Throwable> indexFailures = ImmutableMap.of();
boolean userlogSuccess = true;
try {
//2. Commit indexes - [FAILURE] all exceptions are collected and logged but nothing is aborted
indexFailures = mutator.commitIndexes();
if (!indexFailures.isEmpty()) {
status = LogTxStatus.SECONDARY_FAILURE;
for (Map.Entry<String,Throwable> entry : indexFailures.entrySet()) {
log.error("Error while commiting index mutations for transaction ["+transactionId+"] on index: " +entry.getKey(),entry.getValue());
}
}
//3. Log transaction if configured - [FAILURE] is recorded but does not cause exception
if (logTxIdentifier!=null) {
try {
userlogSuccess = false;
final Log userLog = backend.getUserLog(logTxIdentifier);
Future<Message> env = userLog.add(txLogHeader.serializeModifications(serializer, LogTxStatus.USER_LOG, tx, addedRelations, deletedRelations));
if (env.isDone()) {
try {
env.get();
} catch (ExecutionException ex) {
throw ex.getCause();
}
}
userlogSuccess=true;
} catch (Throwable e) {
status = LogTxStatus.SECONDARY_FAILURE;
log.error("Could not user-log committed transaction ["+transactionId+"] to " + logTxIdentifier, e);
}
}
} finally {
if (logTransaction) {
//[FAILURE] An exception here will be logged and not escalated; tx considered success and
// needs to be cleaned up later
try {
txLog.add(txLogHeader.serializeSecondary(serializer,status,indexFailures,userlogSuccess),txLogHeader.getLogKey());
} catch (Throwable e) {
log.error("Could not tx-log secondary persistence status on transaction ["+transactionId+"]",e);
}
}
}
} else {
//This just closes the transaction since there are no modifications
mutator.commitIndexes();
}
} else { //Just commit everything at once
//[FAILURE] This case only happens when there are no non-system mutations in which case all changes
//are already flushed. Hence, an exception here is unlikely and should abort
mutator.commit();
}
} catch (Throwable e) {
log.error("Could not commit transaction ["+transactionId+"] due to exception",e);
try {
//Clean up any left-over transaction handles
mutator.rollback();
} catch (Throwable e2) {
log.error("Could not roll-back transaction ["+transactionId+"] after failure due to exception",e2);
}
if (e instanceof RuntimeException) throw (RuntimeException)e;
else throw new TitanException("Unexpected exception",e);
}
}
private static class ShutdownThread extends Thread {
private final StandardTitanGraph graph;
public ShutdownThread(StandardTitanGraph graph) {
this.graph = graph;
}
@Override
public void start() {
log.debug("Shutting down graph {} using shutdown hook {}", graph, this);
graph.closeInternal();
}
}
}