/
ManagementSystem.java
1484 lines (1285 loc) · 66.4 KB
/
ManagementSystem.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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 JanusGraph Authors
//
// Licensed 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.janusgraph.graphdb.database.management;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.REGISTRATION_NS;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.REGISTRATION_TIME;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ROOT_NS;
import static org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper.RELATION_INDEX_SEPARATOR;
import org.janusgraph.core.*;
import org.janusgraph.core.schema.*;
import org.janusgraph.diskstorage.BackendException;
import org.janusgraph.diskstorage.configuration.*;
import org.janusgraph.diskstorage.configuration.backend.KCVSConfiguration;
import org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics;
import org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner;
import org.janusgraph.diskstorage.log.Log;
import org.janusgraph.graphdb.database.IndexSerializer;
import org.janusgraph.graphdb.database.StandardJanusGraph;
import org.janusgraph.graphdb.database.cache.SchemaCache;
import org.janusgraph.graphdb.database.serialize.DataOutput;
import org.janusgraph.graphdb.internal.*;
import org.janusgraph.graphdb.olap.VertexJobConverter;
import org.janusgraph.graphdb.olap.job.IndexRemoveJob;
import org.janusgraph.graphdb.olap.job.IndexRepairJob;
import org.janusgraph.graphdb.query.QueryUtil;
import org.janusgraph.graphdb.transaction.StandardJanusGraphTx;
import org.janusgraph.graphdb.types.*;
import org.janusgraph.graphdb.types.indextype.IndexTypeWrapper;
import org.janusgraph.graphdb.types.system.BaseKey;
import org.janusgraph.graphdb.types.system.SystemTypeManager;
import org.janusgraph.graphdb.types.vertices.EdgeLabelVertex;
import org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex;
import org.janusgraph.graphdb.types.vertices.PropertyKeyVertex;
import org.janusgraph.graphdb.types.vertices.RelationTypeVertex;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* @author Matthias Broecheler (me@matthiasb.com)
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class ManagementSystem implements JanusGraphManagement {
private static final Logger LOGGER =
LoggerFactory.getLogger(ManagementSystem.class);
private static final String CURRENT_INSTANCE_SUFFIX = "(current)";
private final StandardJanusGraph graph;
private final Log sysLog;
private final ManagementLogger managementLogger;
private final TransactionalConfiguration transactionalConfig;
private final ModifiableConfiguration modifyConfig;
private final UserModifiableConfiguration userConfig;
private final SchemaCache schemaCache;
private final StandardJanusGraphTx transaction;
private final Set<JanusGraphSchemaVertex> updatedTypes;
private boolean evictGraphFromCache;
private final List<Callable<Boolean>> updatedTypeTriggers;
private final Instant txStartTime;
private boolean graphShutdownRequired;
private boolean isOpen;
private final static String FIRSTDASH= "------------------------------------------------------------------------------------------------\n";
private final static String DASHBREAK = "---------------------------------------------------------------------------------------------------\n";
public ManagementSystem(StandardJanusGraph graph, KCVSConfiguration config, Log sysLog,
ManagementLogger managementLogger, SchemaCache schemaCache) {
Preconditions.checkArgument(config != null && graph != null && sysLog != null && managementLogger != null);
this.graph = graph;
this.sysLog = sysLog;
this.managementLogger = managementLogger;
this.schemaCache = schemaCache;
this.transactionalConfig = new TransactionalConfiguration(config);
this.modifyConfig = new ModifiableConfiguration(ROOT_NS,
transactionalConfig, BasicConfiguration.Restriction.GLOBAL);
this.userConfig = new UserModifiableConfiguration(modifyConfig, configVerifier);
this.updatedTypes = new HashSet<>();
this.evictGraphFromCache = false;
this.updatedTypeTriggers = new ArrayList<>();
this.graphShutdownRequired = false;
this.transaction = (StandardJanusGraphTx) graph.buildTransaction().disableBatchLoading().start();
this.txStartTime = graph.getConfiguration().getTimestampProvider().getTime();
this.isOpen = true;
}
private final UserModifiableConfiguration.ConfigVerifier configVerifier = new UserModifiableConfiguration.ConfigVerifier() {
@Override
public void verifyModification(ConfigOption option) {
Preconditions.checkArgument (graph.getConfiguration().isUpgradeAllowed(option.getName()) ||
option.getType() != ConfigOption.Type.FIXED, "Cannot change the fixed configuration option: %s", option);
Preconditions.checkArgument(option.getType() != ConfigOption.Type.LOCAL, "Cannot change the local configuration option: %s", option);
if (option.getType() == ConfigOption.Type.GLOBAL_OFFLINE) {
//Verify that there no other open JanusGraph graph instance and no open transactions
Set<String> openInstances = getOpenInstancesInternal();
assert openInstances.size() > 0;
Preconditions.checkArgument(openInstances.size() < 2, "Cannot change offline config option [%s] since multiple instances are currently open: %s", option, openInstances);
Preconditions.checkArgument(openInstances.contains(graph.getConfiguration().getUniqueGraphId()),
"Only one open instance ("
+ openInstances.iterator().next() + "), but it's not the current one ("
+ graph.getConfiguration().getUniqueGraphId() + ")");
//Indicate that this graph must be closed
graphShutdownRequired = true;
}
}
};
public Set<String> getOpenInstancesInternal() {
Set<String> openInstances = Sets.newHashSet(modifyConfig.getContainedNamespaces(REGISTRATION_NS));
LOGGER.debug("Open instances: {}", openInstances);
return openInstances;
}
@Override
public Set<String> getOpenInstances() {
Set<String> openInstances = getOpenInstancesInternal();
String uid = graph.getConfiguration().getUniqueGraphId();
Preconditions.checkArgument(openInstances.contains(uid), "Current instance [%s] not listed as an open instance: %s", uid, openInstances);
openInstances.remove(uid);
openInstances.add(uid + CURRENT_INSTANCE_SUFFIX);
return openInstances;
}
@Override
public void forceCloseInstance(String instanceId) {
Preconditions.checkArgument(!graph.getConfiguration().getUniqueGraphId().equals(instanceId),
"Cannot force close this current instance [%s]. Properly shut down the graph instead.", instanceId);
Preconditions.checkArgument(modifyConfig.has(REGISTRATION_TIME, instanceId), "Instance [%s] is not currently open", instanceId);
Instant registrationTime = modifyConfig.get(REGISTRATION_TIME, instanceId);
Preconditions.checkArgument(registrationTime.compareTo(txStartTime) < 0, "The to-be-closed instance [%s] was started after this transaction" +
"which indicates a successful restart and can hence not be closed: %s vs %s", instanceId, registrationTime, txStartTime);
modifyConfig.remove(REGISTRATION_TIME, instanceId);
}
private void ensureOpen() {
Preconditions.checkState(isOpen, "This management system instance has been closed");
}
@Override
public synchronized void commit() {
ensureOpen();
//Commit config changes
if (transactionalConfig.hasMutations()) {
DataOutput out = graph.getDataSerializer().getDataOutput(128);
out.writeObjectNotNull(MgmtLogType.CONFIG_MUTATION);
transactionalConfig.logMutations(out);
sysLog.add(out.getStaticBuffer());
}
transactionalConfig.commit();
//Commit underlying transaction
transaction.commit();
//Communicate schema changes
if (!updatedTypes.isEmpty() || evictGraphFromCache) {
managementLogger.sendCacheEviction(updatedTypes, evictGraphFromCache, updatedTypeTriggers, getOpenInstancesInternal());
for (JanusGraphSchemaVertex schemaVertex : updatedTypes) {
schemaCache.expireSchemaElement(schemaVertex.longId());
}
}
if (graphShutdownRequired) graph.close();
close();
}
@Override
public synchronized void rollback() {
ensureOpen();
transactionalConfig.rollback();
transaction.rollback();
close();
}
@Override
public boolean isOpen() {
return isOpen;
}
private void close() {
isOpen = false;
}
public StandardJanusGraphTx getWrappedTx() {
return transaction;
}
private JanusGraphEdge addSchemaEdge(JanusGraphVertex out, JanusGraphVertex in, TypeDefinitionCategory def, Object modifier) {
return transaction.addSchemaEdge(out, in, def, modifier);
}
// ###### INDEXING SYSTEM #####################
/* --------------
Type Indexes
--------------- */
public JanusGraphSchemaElement getSchemaElement(long id) {
JanusGraphVertex v = transaction.getVertex(id);
if (v == null) return null;
if (v instanceof RelationType) {
if (((InternalRelationType) v).getBaseType() == null) return (RelationType) v;
return new RelationTypeIndexWrapper((InternalRelationType) v);
}
if (v instanceof JanusGraphSchemaVertex) {
JanusGraphSchemaVertex sv = (JanusGraphSchemaVertex) v;
if (sv.getDefinition().containsKey(TypeDefinitionCategory.INTERNAL_INDEX)) {
return new JanusGraphIndexWrapper(sv.asIndexType());
}
}
throw new IllegalArgumentException("Not a valid schema element vertex: " + id);
}
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, PropertyKey... sortKeys) {
return buildRelationTypeIndex(label, name, direction, Order.ASC, sortKeys);
}
@Override
public RelationTypeIndex buildEdgeIndex(EdgeLabel label, String name, Direction direction, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
return buildRelationTypeIndex(label, name, direction, Order.convert(sortOrder), sortKeys);
}
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, PropertyKey... sortKeys) {
return buildRelationTypeIndex(key, name, Direction.OUT, Order.ASC, sortKeys);
}
@Override
public RelationTypeIndex buildPropertyIndex(PropertyKey key, String name, org.apache.tinkerpop.gremlin.process.traversal.Order sortOrder, PropertyKey... sortKeys) {
return buildRelationTypeIndex(key, name, Direction.OUT, Order.convert(sortOrder), sortKeys);
}
private RelationTypeIndex buildRelationTypeIndex(RelationType type, String name, Direction direction, Order sortOrder, PropertyKey... sortKeys) {
Preconditions.checkArgument(type != null && direction != null && sortOrder != null && sortKeys != null);
Preconditions.checkArgument(StringUtils.isNotBlank(name), "Name cannot be blank: %s", name);
Token.verifyName(name);
Preconditions.checkArgument(sortKeys.length > 0, "Need to specify sort keys");
for (RelationType key : sortKeys) Preconditions.checkArgument(key != null, "Keys cannot be null");
Preconditions.checkArgument(!(type instanceof EdgeLabel) || !((EdgeLabel) type).isUnidirected() || direction == Direction.OUT,
"Can only index uni-directed labels in the out-direction: %s", type);
Preconditions.checkArgument(!((InternalRelationType) type).multiplicity().isUnique(direction),
"The relation type [%s] has a multiplicity or cardinality constraint in direction [%s] and can therefore not be indexed", type, direction);
String composedName = composeRelationTypeIndexName(type, name);
StandardRelationTypeMaker maker;
if (type.isEdgeLabel()) {
StandardEdgeLabelMaker lm = (StandardEdgeLabelMaker) transaction.makeEdgeLabel(composedName);
lm.unidirected(direction);
maker = lm;
} else {
assert type.isPropertyKey();
assert direction == Direction.OUT;
StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName);
lm.dataType(((PropertyKey) type).dataType());
maker = lm;
}
boolean canIndexBeEnabled = type.isNew() || Arrays.stream(sortKeys).anyMatch(key -> key.isNew());
maker.status(canIndexBeEnabled ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
maker.invisible();
maker.multiplicity(Multiplicity.MULTI);
maker.sortKey(sortKeys);
maker.sortOrder(sortOrder);
//Compose signature
long[] typeSig = ((InternalRelationType) type).getSignature();
Set<PropertyKey> signature = Sets.newHashSet();
for (long typeId : typeSig) signature.add(transaction.getExistingPropertyKey(typeId));
for (RelationType sortType : sortKeys) signature.remove(sortType);
if (!signature.isEmpty()) {
PropertyKey[] sig = signature.toArray(new PropertyKey[signature.size()]);
maker.signature(sig);
}
RelationType typeIndex = maker.make();
addSchemaEdge(type, typeIndex, TypeDefinitionCategory.RELATIONTYPE_INDEX, null);
RelationTypeIndexWrapper index = new RelationTypeIndexWrapper((InternalRelationType) typeIndex);
if (!canIndexBeEnabled) updateIndex(index, SchemaAction.REGISTER_INDEX);
return index;
}
private static String composeRelationTypeIndexName(RelationType type, String name) {
return String.valueOf(type.longId()) + RELATION_INDEX_SEPARATOR + name;
}
@Override
public boolean containsRelationIndex(RelationType type, String name) {
return getRelationIndex(type, name) != null;
}
@Override
public RelationTypeIndex getRelationIndex(RelationType type, String name) {
Preconditions.checkArgument(type != null);
Preconditions.checkArgument(StringUtils.isNotBlank(name));
String composedName = composeRelationTypeIndexName(type, name);
//Don't use SchemaCache to make code more compact and since we don't need the extra performance here
JanusGraphVertex v = Iterables.getOnlyElement(QueryUtil.getVertices(transaction, BaseKey.SchemaName, JanusGraphSchemaCategory.getRelationTypeName(composedName)), null);
if (v == null) return null;
assert v instanceof InternalRelationType;
return new RelationTypeIndexWrapper((InternalRelationType) v);
}
@Override
public Iterable<RelationTypeIndex> getRelationIndexes(final RelationType type) {
Preconditions.checkArgument(type != null && type instanceof InternalRelationType, "Invalid relation type provided: %s", type);
return Iterables.transform(Iterables.filter(((InternalRelationType) type).getRelationIndexes(), internalRelationType -> !type.equals(internalRelationType)), new Function<InternalRelationType, RelationTypeIndex>() {
@Nullable
@Override
public RelationTypeIndex apply(@Nullable InternalRelationType internalType) {
return new RelationTypeIndexWrapper(internalType);
}
});
}
/* --------------
Graph Indexes
--------------- */
public static IndexType getGraphIndexDirect(String name, StandardJanusGraphTx transaction) {
JanusGraphSchemaVertex v = transaction.getSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX.getSchemaName(name));
if (v == null) return null;
return v.asIndexType();
}
@Override
public boolean containsGraphIndex(String name) {
return getGraphIndex(name) != null;
}
@Override
public JanusGraphIndex getGraphIndex(String name) {
IndexType index = getGraphIndexDirect(name, transaction);
return index == null ? null : new JanusGraphIndexWrapper(index);
}
@Override
public Iterable<JanusGraphIndex> getGraphIndexes(final Class<? extends Element> elementType) {
return StreamSupport.stream(
QueryUtil.getVertices(transaction, BaseKey.SchemaCategory, JanusGraphSchemaCategory.GRAPHINDEX).spliterator(), false)
.map(janusGraphVertex -> {
assert janusGraphVertex instanceof JanusGraphSchemaVertex;
return ((JanusGraphSchemaVertex) janusGraphVertex).asIndexType();
})
.filter(indexType -> indexType.getElement().subsumedBy(elementType))
.map(JanusGraphIndexWrapper::new)
.collect(Collectors.toList());
}
@Override
public String printSchema() {
StringBuilder sb = new StringBuilder();
sb.append(printVertexLabels(false));
sb.append(printEdgeLabels(false));
sb.append(printPropertyKeys(false));
sb.append(printIndexes(false));
return sb.toString();
}
@Override
public String printVertexLabels() {
return this.printVertexLabels(true);
}
private String printVertexLabels(boolean calledDirectly) {
StringBuilder sb = new StringBuilder();
String pattern = "%-30s | %-11s | %-50s |%n";
Iterable<VertexLabel> labels = getVertexLabels();
boolean hasResults = false;
sb.append(FIRSTDASH);
sb.append(String.format(pattern, "Vertex Label Name", "Partitioned", "Static"));
sb.append(DASHBREAK);
for(VertexLabel label: labels) {
hasResults = true;
sb.append(String.format(pattern, label.name(), label.isPartitioned(), label.isStatic()));
}
if (hasResults && calledDirectly) {
sb.append(DASHBREAK);
}
return sb.toString();
}
@Override
public String printEdgeLabels() {
return this.printEdgeLabels(true);
}
private String printEdgeLabels(boolean calledDirectly) {
StringBuilder sb = new StringBuilder();
String pattern = "%-30s | %-11s | %-11s | %-36s |%n";
Iterable<EdgeLabel> labels = getRelationTypes(EdgeLabel.class);
boolean hasResults = false;
if (calledDirectly) {
sb.append(FIRSTDASH);
} else {
sb.append(DASHBREAK);
}
sb.append(String.format(pattern, "Edge Label Name", "Directed", "Unidirected", "Multiplicity"));
sb.append(DASHBREAK);
for (EdgeLabel label: labels) {
hasResults = true;
sb.append(String.format(pattern, label.name(), label.isDirected() ,label.isUnidirected(), label.multiplicity()));
}
if (hasResults && calledDirectly) {
sb.append(DASHBREAK);
}
return sb.toString();
}
@Override
public String printPropertyKeys() {
return this.printPropertyKeys(true);
}
private String printPropertyKeys(boolean calledDirectly) {
StringBuilder sb = new StringBuilder();
String pattern = "%-30s | %-11s | %-50s |\n";
Iterable<PropertyKey> keys = getRelationTypes(PropertyKey.class);
boolean hasResults = false;
if (calledDirectly) {
sb.append(FIRSTDASH);
} else {
sb.append(DASHBREAK);
}
sb.append(String.format(pattern, "Property Key Name", "Cardinality", "Data Type"));
sb.append(DASHBREAK);
for (PropertyKey key: keys) {
hasResults = true;
sb.append(String.format(pattern, key.name(), key.cardinality(), key.dataType()));
}
if (hasResults && calledDirectly) {
sb.append(DASHBREAK);
}
return sb.toString();
}
@Override
public String printIndexes() {
return this.printIndexes(true);
}
private String printIndexes(boolean calledDirectly) {
StringBuilder sb = new StringBuilder();
String pattern = "%-30s | %-11s | %-9s | %-14s | %-10s %10s |%n";
String relationPattern = "%-30s | %-11s | %-9s | %-14s | %-8s | %10s |%n";
Iterable<JanusGraphIndex> vertexIndexes = getGraphIndexes(Vertex.class);
Iterable<JanusGraphIndex> edgeIndexes = getGraphIndexes(Edge.class);
Iterable<RelationType> relationTypes = getRelationTypes(RelationType.class);
LinkedList<RelationTypeIndex> relationIndexes = new LinkedList<>();
for (RelationType rt :relationTypes) {
Iterable<RelationTypeIndex> rti = getRelationIndexes(rt);
rti.forEach(relationIndexes::add);
}
if (calledDirectly) {
sb.append(FIRSTDASH);
} else {
sb.append(DASHBREAK);
}
sb.append(String.format(pattern, "Vertex Index Name", "Type", "Unique", "Backing", "Key:", "Status"));
sb.append(DASHBREAK);
sb.append(iterateIndexes(pattern, vertexIndexes));
sb.append(DASHBREAK);
sb.append(String.format(pattern, "Edge Index (VCI) Name", "Type", "Unique", "Backing", "Key:", "Status"));
sb.append(DASHBREAK);
sb.append(iterateIndexes(pattern, edgeIndexes));
sb.append(DASHBREAK);
sb.append(String.format(relationPattern,"Relation Index", "Type", "Direction", "Sort Key", "Order", "Status"));
sb.append(DASHBREAK);
for (RelationTypeIndex ri: relationIndexes) {
sb.append(String.format(relationPattern, ri.name(), ri.getType(), ri.getDirection(), ri.getSortKey()[0], ri.getSortOrder(), ri.getIndexStatus().name()));
}
if (!relationIndexes.isEmpty()) {
sb.append(DASHBREAK);
}
return sb.toString();
}
private String iterateIndexes(String pattern, Iterable<JanusGraphIndex> indexes) {
StringBuilder sb = new StringBuilder();
for (JanusGraphIndex index: indexes) {
String type = getIndexType(index);
PropertyKey[] keys = index.getFieldKeys();
String[][] keyStatus = getKeyStatus(keys, index);
sb.append(String.format(pattern, index.name(), type, index.isUnique(), index.getBackingIndex(), keyStatus[0][0] + ":", keyStatus[0][1]));
if (keyStatus.length > 1) {
for (int i = 1; i < keyStatus.length; i++) {
sb.append(String.format(pattern, "", "", "", "", keyStatus[i][0] + ":", keyStatus[i][1]));
}
}
}
return sb.toString();
}
private String[][] getKeyStatus(PropertyKey[] keys, JanusGraphIndex index) {
String[][] keyStatus = new String[keys.length][2];
for (int i = 0; i < keys.length; i++) {
keyStatus[i][0] = keys[i].name();
keyStatus[i][1] = index.getIndexStatus(keys[i]).name();
}
return keyStatus.length > 0 ? keyStatus: new String[][] {{"",""}};
}
private String getIndexType(JanusGraphIndex index) {
String type;
if (index.isCompositeIndex()) {
type = "Composite";
} else if (index.isMixedIndex()) {
type = "Mixed";
} else {
type = "Unknown";
}
return type;
}
/**
* Returns a {@link GraphIndexStatusWatcher} configured to watch
* {@code graphIndexName} through graph {@code g}.
* <p>
* This method just instantiates an object.
* Invoke {@link GraphIndexStatusWatcher#call()} to wait.
*
* @param g the graph through which to read index information
* @param graphIndexName the name of a graph index to watch
* @return
*/
public static GraphIndexStatusWatcher awaitGraphIndexStatus(JanusGraph g, String graphIndexName) {
return new GraphIndexStatusWatcher(g, graphIndexName);
}
/**
* Returns a {@link RelationIndexStatusWatcher} configured to watch the index specified by
* {@code relationIndexName} and {@code relationIndexType} through graph {@code g}.
* <p>
* This method just instantiates an object.
* Invoke {@link RelationIndexStatusWatcher#call()} to wait.
*
* @param g the graph through which to read index information
* @param relationIndexName the name of the relation index to watch
* @param relationTypeName the type on the relation index to watch
* @return
*/
public static RelationIndexStatusWatcher awaitRelationIndexStatus(JanusGraph g,
String relationIndexName,
String relationTypeName) {
return new RelationIndexStatusWatcher(g, relationIndexName, relationTypeName);
}
private void checkIndexName(String indexName) {
Preconditions.checkArgument(StringUtils.isNotBlank(indexName));
Preconditions.checkArgument(getGraphIndex(indexName) == null, "An index with name '%s' has already been defined", indexName);
}
private JanusGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory,
JanusGraphSchemaType constraint, String backingIndex) {
Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex);
checkIndexName(indexName);
TypeDefinitionMap def = new TypeDefinitionMap();
def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false);
def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex);
def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED);
JanusGraphSchemaVertex indexVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX, indexName, def);
Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof JanusGraphSchemaVertex));
if (constraint != null) {
addSchemaEdge(indexVertex, (JanusGraphSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
}
updateSchemaVertex(indexVertex);
return new JanusGraphIndexWrapper(indexVertex.asIndexType());
}
@Override
public void addIndexKey(final JanusGraphIndex index, final PropertyKey key, Parameter... parameters) {
Preconditions.checkArgument(index != null && key != null && index instanceof JanusGraphIndexWrapper
&& !(key instanceof BaseKey), "Need to provide valid index and key");
if (parameters == null) parameters = new Parameter[0];
IndexType indexType = ((JanusGraphIndexWrapper) index).getBaseIndex();
Preconditions.checkArgument(indexType instanceof MixedIndexType, "Can only add keys to an external index, not %s", index.name());
Preconditions.checkArgument(indexType instanceof IndexTypeWrapper && key instanceof JanusGraphSchemaVertex
&& ((IndexTypeWrapper) indexType).getSchemaBase() instanceof JanusGraphSchemaVertex);
JanusGraphSchemaVertex indexVertex = (JanusGraphSchemaVertex) ((IndexTypeWrapper) indexType).getSchemaBase();
for (IndexField field : indexType.getFieldKeys())
Preconditions.checkArgument(!field.getFieldKey().equals(key), "Key [%s] has already been added to index %s", key.name(), index.name());
//Assemble parameters
boolean addMappingParameter = !ParameterType.MAPPED_NAME.hasParameter(parameters);
Parameter[] extendedParas = new Parameter[parameters.length + 1 + (addMappingParameter ? 1 : 0)];
System.arraycopy(parameters, 0, extendedParas, 0, parameters.length);
int arrPosition = parameters.length;
if (addMappingParameter) extendedParas[arrPosition++] = ParameterType.MAPPED_NAME.getParameter(
graph.getIndexSerializer().getDefaultFieldName(key, parameters, indexType.getBackingIndexName()));
extendedParas[arrPosition] = ParameterType.STATUS.getParameter(key.isNew() ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
addSchemaEdge(indexVertex, key, TypeDefinitionCategory.INDEX_FIELD, extendedParas);
updateSchemaVertex(indexVertex);
indexType.resetCache();
//Check to see if the index supports this
if (!graph.getIndexSerializer().supports((MixedIndexType) indexType, ParameterIndexField.of(key, parameters))) {
throw new JanusGraphException("Could not register new index field '" + key.name() + "' with index backend as the data type, cardinality or parameter combination is not supported.");
}
try {
IndexSerializer.register((MixedIndexType) indexType, key, transaction.getTxHandle());
} catch (BackendException e) {
throw new JanusGraphException("Could not register new index field with index backend", e);
}
if (!indexVertex.isNew()) updatedTypes.add(indexVertex);
if (!key.isNew()) updateIndex(index, SchemaAction.REGISTER_INDEX);
}
private JanusGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, JanusGraphSchemaType constraint, PropertyKey... keys) {
checkIndexName(indexName);
Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName);
Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName);
boolean allSingleKeys = true;
boolean oneNewKey = false;
for (PropertyKey key : keys) {
Preconditions.checkArgument(key != null && key instanceof PropertyKeyVertex, "Need to provide valid keys: %s", key);
if (key.cardinality() != Cardinality.SINGLE) allSingleKeys = false;
if (key.isNew()) oneNewKey = true;
else updatedTypes.add((PropertyKeyVertex) key);
}
Cardinality indexCardinality;
if (unique) indexCardinality = Cardinality.SINGLE;
else indexCardinality = (allSingleKeys ? Cardinality.SET : Cardinality.LIST);
boolean canIndexBeEnabled = oneNewKey || (constraint != null && constraint.isNew());
TypeDefinitionMap def = new TypeDefinitionMap();
def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, true);
def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
def.setValue(TypeDefinitionCategory.BACKING_INDEX, Token.INTERNAL_INDEX_NAME);
def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, indexCardinality);
def.setValue(TypeDefinitionCategory.STATUS, canIndexBeEnabled ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
JanusGraphSchemaVertex indexVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX, indexName, def);
for (int i = 0; i < keys.length; i++) {
Parameter[] paras = {ParameterType.INDEX_POSITION.getParameter(i)};
addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras);
}
Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof JanusGraphSchemaVertex));
if (constraint != null) {
addSchemaEdge(indexVertex, (JanusGraphSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
}
updateSchemaVertex(indexVertex);
JanusGraphIndexWrapper index = new JanusGraphIndexWrapper(indexVertex.asIndexType());
if (!canIndexBeEnabled) updateIndex(index, SchemaAction.REGISTER_INDEX);
return index;
}
@Override
public JanusGraphManagement.IndexBuilder buildIndex(String indexName, Class<? extends Element> elementType) {
return new IndexBuilder(indexName, ElementCategory.getByClazz(elementType));
}
private class IndexBuilder implements JanusGraphManagement.IndexBuilder {
private final String indexName;
private final ElementCategory elementCategory;
private boolean unique = false;
private JanusGraphSchemaType constraint = null;
private final Map<PropertyKey, Parameter[]> keys = new HashMap<>();
private IndexBuilder(String indexName, ElementCategory elementCategory) {
this.indexName = indexName;
this.elementCategory = elementCategory;
}
@Override
public JanusGraphManagement.IndexBuilder addKey(PropertyKey key) {
Preconditions.checkArgument(key != null && (key instanceof PropertyKeyVertex), "Key must be a user defined key: %s", key);
keys.put(key, null);
return this;
}
@Override
public JanusGraphManagement.IndexBuilder addKey(PropertyKey key, Parameter... parameters) {
Preconditions.checkArgument(key != null && (key instanceof PropertyKeyVertex), "Key must be a user defined key: %s", key);
keys.put(key, parameters);
return this;
}
@Override
public JanusGraphManagement.IndexBuilder indexOnly(JanusGraphSchemaType schemaType) {
Preconditions.checkNotNull(schemaType);
Preconditions.checkArgument(elementCategory.isValidConstraint(schemaType), "Need to specify a valid schema type for this index definition: %s", schemaType);
constraint = schemaType;
return this;
}
@Override
public JanusGraphManagement.IndexBuilder unique() {
unique = true;
return this;
}
@Override
public JanusGraphIndex buildCompositeIndex() {
Preconditions.checkArgument(!keys.isEmpty(), "Need to specify at least one key for the composite index");
PropertyKey[] keyArr = new PropertyKey[keys.size()];
int pos = 0;
for (Map.Entry<PropertyKey, Parameter[]> entry : keys.entrySet()) {
Preconditions.checkArgument(entry.getValue() == null, "Cannot specify parameters for composite index: %s", entry.getKey());
keyArr[pos++] = entry.getKey();
}
return createCompositeIndex(indexName, elementCategory, unique, constraint, keyArr);
}
@Override
public JanusGraphIndex buildMixedIndex(String backingIndex) {
Preconditions.checkArgument(StringUtils.isNotBlank(backingIndex), "Need to specify backing index name");
Preconditions.checkArgument(!unique, "An external index cannot be unique");
JanusGraphIndex index = createMixedIndex(indexName, elementCategory, constraint, backingIndex);
for (Map.Entry<PropertyKey, Parameter[]> entry : keys.entrySet()) {
addIndexKey(index, entry.getKey(), entry.getValue());
}
return index;
}
}
/* --------------
Schema Update
--------------- */
@Override
public IndexJobFuture updateIndex(Index index, SchemaAction updateAction) {
Preconditions.checkArgument(index != null, "Need to provide an index");
Preconditions.checkArgument(updateAction != null, "Need to provide update action");
JanusGraphSchemaVertex schemaVertex = getSchemaVertex(index);
Set<JanusGraphSchemaVertex> dependentTypes;
Set<PropertyKeyVertex> keySubset = ImmutableSet.of();
if (index instanceof RelationTypeIndex) {
dependentTypes = ImmutableSet.of((JanusGraphSchemaVertex) ((InternalRelationType) schemaVertex).getBaseType());
if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
return null;
} else if (index instanceof JanusGraphIndex) {
IndexType indexType = schemaVertex.asIndexType();
dependentTypes = Sets.newHashSet();
if (indexType.isCompositeIndex()) {
if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
return null;
for (PropertyKey key : ((JanusGraphIndex) index).getFieldKeys()) {
dependentTypes.add((PropertyKeyVertex) key);
}
} else {
keySubset = Sets.newHashSet();
MixedIndexType mixedIndexType = (MixedIndexType) indexType;
Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
for (ParameterIndexField field : mixedIndexType.getFieldKeys()) {
if (applicableStatus.contains(field.getStatus()))
keySubset.add((PropertyKeyVertex) field.getFieldKey());
}
if (keySubset.isEmpty())
return null;
dependentTypes.addAll(keySubset);
}
} else throw new UnsupportedOperationException("Updates not supported for index: " + index);
IndexIdentifier indexId = new IndexIdentifier(index);
StandardScanner.Builder builder;
IndexJobFuture future;
switch (updateAction) {
case REGISTER_INDEX:
setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
updatedTypes.add(schemaVertex);
updatedTypes.addAll(dependentTypes);
setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
future = new EmptyIndexJobFuture();
break;
case REINDEX:
builder = graph.getBackend().buildEdgeScanJob();
builder.setFinishJob(indexId.getIndexJobFinisher(graph, SchemaAction.ENABLE_INDEX));
builder.setJobId(indexId);
builder.setJob(VertexJobConverter.convert(graph, new IndexRepairJob(indexId.indexName, indexId.relationTypeName)));
try {
future = builder.execute();
} catch (BackendException e) {
throw new JanusGraphException(e);
}
break;
case ENABLE_INDEX:
setStatus(schemaVertex, SchemaStatus.ENABLED, keySubset);
updatedTypes.add(schemaVertex);
if (!keySubset.isEmpty()) updatedTypes.addAll(dependentTypes);
future = new EmptyIndexJobFuture();
break;
case DISABLE_INDEX:
setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
updatedTypes.add(schemaVertex);
if (!keySubset.isEmpty()) updatedTypes.addAll(dependentTypes);
setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.DISABLED, keySubset));
future = new EmptyIndexJobFuture();
break;
case REMOVE_INDEX:
if (index instanceof RelationTypeIndex) {
builder = graph.getBackend().buildEdgeScanJob();
} else {
JanusGraphIndex graphIndex = (JanusGraphIndex) index;
if (graphIndex.isMixedIndex())
throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
builder = graph.getBackend().buildGraphIndexScanJob();
}
builder.setFinishJob(indexId.getIndexJobFinisher());
builder.setJobId(indexId);
builder.setJob(new IndexRemoveJob(graph, indexId.indexName, indexId.relationTypeName));
try {
future = builder.execute();
} catch (BackendException e) {
throw new JanusGraphException(e);
}
break;
default:
throw new UnsupportedOperationException("Update action not supported: " + updateAction);
}
return future;
}
/**
* Upon the open managementsystem's commit, this graph will be asynchronously evicted from the cache on all JanusGraph nodes in your
* cluster, once there are no open transactions on this graph on each respective JanusGraph node
* and assuming each node is correctly configured to use the {@link org.janusgraph.graphdb.management.JanusGraphManager}.
*/
public void evictGraphFromCache() {
this.evictGraphFromCache = true;
setUpdateTrigger(new GraphCacheEvictionCompleteTrigger(this.graph.getGraphName()));
}
private static class GraphCacheEvictionCompleteTrigger implements Callable<Boolean> {
private static final Logger log = LoggerFactory.getLogger(GraphCacheEvictionCompleteTrigger.class);
private final String graphName;
private GraphCacheEvictionCompleteTrigger(String graphName) {
this.graphName = graphName;
}
@Override
public Boolean call() {
log.info("Graph {} has been removed from the graph cache on every JanusGraph node in the cluster.", graphName);
return true;
}
}
private static class EmptyIndexJobFuture implements IndexJobFuture {
@Override
public ScanMetrics getIntermediateResult() {
return null;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return true;
}
@Override
public ScanMetrics get() throws InterruptedException, ExecutionException {
return null;
}
@Override
public ScanMetrics get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
}
private static class UpdateStatusTrigger implements Callable<Boolean> {
private static final Logger log =
LoggerFactory.getLogger(UpdateStatusTrigger.class);
private final StandardJanusGraph graph;
private final long schemaVertexId;
private final SchemaStatus newStatus;
private final Set<Long> propertyKeys;
private UpdateStatusTrigger(StandardJanusGraph graph, JanusGraphSchemaVertex vertex, SchemaStatus newStatus, Iterable<PropertyKeyVertex> keys) {
this.graph = graph;
this.schemaVertexId = vertex.longId();
this.newStatus = newStatus;
this.propertyKeys = Sets.newHashSet(Iterables.transform(keys, new Function<PropertyKey, Long>() {
@Nullable
@Override
public Long apply(@Nullable PropertyKey propertyKey) {
return propertyKey.longId();
}
}));
}
@Override
public Boolean call() throws Exception {
ManagementSystem management = (ManagementSystem) graph.openManagement();
try {
JanusGraphVertex vertex = management.transaction.getVertex(schemaVertexId);
Preconditions.checkArgument(vertex != null && vertex instanceof JanusGraphSchemaVertex);
JanusGraphSchemaVertex schemaVertex = (JanusGraphSchemaVertex) vertex;
Set<PropertyKeyVertex> keys = Sets.newHashSet();
for (Long keyId : propertyKeys) keys.add((PropertyKeyVertex) management.transaction.getVertex(keyId));
management.setStatus(schemaVertex, newStatus, keys);
management.updatedTypes.addAll(keys);
management.updatedTypes.add(schemaVertex);
if (log.isInfoEnabled()) {
Set<String> propNames = Sets.newHashSet();
for (PropertyKeyVertex v : keys) {
try {
propNames.add(v.name());
} catch (Throwable t) {
log.warn("Failed to get name for property key with id {}", v.longId(), t);
propNames.add("(ID#" + v.longId() + ")");
}
}
String schemaName = "(ID#" + schemaVertexId + ")";
try {
schemaName = schemaVertex.name();
} catch (Throwable t) {
log.warn("Failed to get name for schema vertex with id {}", schemaVertexId, t);
}
log.info("Set status {} on schema element {} with property keys {}", newStatus, schemaName, propNames);
}
management.commit();
return true;
} catch (RuntimeException e) {
management.rollback();
throw e;
}
}
@Override
public int hashCode() {
return Long.hashCode(schemaVertexId);
}