Skip to content

Commit 2af01bc

Browse files
zyxxooLinary
andauthored
Add system schema store (#1891)
* Add meta table into system store * Add system schema store * Add test case for read version * Let AbstractBackendStore impl default storedVersion() * fix small bug og HugeProject * add test for SystemSchemaStore * fix initStore schema missed Change-Id: I5c9d65c4ae022cb84592e8f2aa57bd7485cd12e4 Co-authored-by: liningrui <[email protected]>
1 parent b9266b2 commit 2af01bc

File tree

54 files changed

+1002
-394
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1002
-394
lines changed

hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import com.baidu.hugegraph.backend.id.IdGenerator;
7575
import com.baidu.hugegraph.backend.query.Query;
7676
import com.baidu.hugegraph.backend.store.BackendFeatures;
77-
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
77+
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
7878
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
7979
import com.baidu.hugegraph.config.AuthOptions;
8080
import com.baidu.hugegraph.config.HugeConfig;
@@ -604,15 +604,9 @@ public String backend() {
604604
}
605605

606606
@Override
607-
public String backendVersion() {
608-
this.verifyAnyPermission();
609-
return this.hugegraph.backendVersion();
610-
}
611-
612-
@Override
613-
public BackendStoreSystemInfo backendStoreSystemInfo() {
607+
public BackendStoreInfo backendStoreInfo() {
614608
this.verifyAdminPermission();
615-
return this.hugegraph.backendStoreSystemInfo();
609+
return this.hugegraph.backendStoreInfo();
616610
}
617611

618612
@Override
@@ -734,6 +728,12 @@ public void truncateBackend() {
734728
}
735729
}
736730

731+
@Override
732+
public void initSystemInfo() {
733+
this.verifyAdminPermission();
734+
this.hugegraph.initSystemInfo();
735+
}
736+
737737
@Override
738738
public void createSnapshot() {
739739
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
@@ -998,6 +998,12 @@ public HugeGraph graph() {
998998
return this.taskScheduler.graph();
999999
}
10001000

1001+
@Override
1002+
public void init() {
1003+
verifyAdminPermission();
1004+
this.taskScheduler.init();
1005+
}
1006+
10011007
@Override
10021008
public int pendingTasks() {
10031009
verifyTaskPermission(HugePermission.READ);
@@ -1161,6 +1167,12 @@ private String currentUsername() {
11611167
return null;
11621168
}
11631169

1170+
@Override
1171+
public void init() {
1172+
verifyAdminPermission();
1173+
this.authManager.init();
1174+
}
1175+
11641176
@Override
11651177
public boolean close() {
11661178
verifyAdminPermission();

hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.baidu.hugegraph.backend.cache.CacheManager;
5050
import com.baidu.hugegraph.backend.id.Id;
5151
import com.baidu.hugegraph.backend.id.IdGenerator;
52-
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
52+
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
5353
import com.baidu.hugegraph.config.CoreOptions;
5454
import com.baidu.hugegraph.config.HugeConfig;
5555
import com.baidu.hugegraph.config.ServerOptions;
@@ -393,7 +393,7 @@ private void checkBackendVersionOrExit(HugeConfig config) {
393393
}
394394
}
395395
}
396-
BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo();
396+
BackendStoreInfo info = hugegraph.backendStoreInfo();
397397
if (!info.exists()) {
398398
throw new BackendException(
399399
"The backend store of '%s' has not been initialized",

hugegraph-cassandra/src/main/java/com/baidu/hugegraph/backend/store/cassandra/CassandraMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import org.slf4j.Logger;
3939

4040
import com.baidu.hugegraph.backend.store.BackendMetrics;
41+
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
4142
import com.baidu.hugegraph.backend.store.BackendTable;
4243
import com.baidu.hugegraph.backend.store.cassandra.CassandraTables.Edge;
4344
import com.baidu.hugegraph.backend.store.cassandra.CassandraTables.Vertex;
44-
import com.baidu.hugegraph.config.CoreOptions;
4545
import com.baidu.hugegraph.config.HugeConfig;
4646
import com.baidu.hugegraph.testutil.Whitebox;
4747
import com.baidu.hugegraph.util.E;
@@ -77,7 +77,7 @@ public CassandraMetrics(HugeConfig conf,
7777
assert this.username != null && this.password != null;
7878

7979
this.keyspace = keyspace;
80-
String g = conf.get(CoreOptions.STORE_GRAPH);
80+
String g = BackendStoreProvider.GRAPH_STORE;
8181
String v = BackendTable.joinTableName(g, Vertex.TABLE);
8282
String oe = BackendTable.joinTableName(g, "o" + Edge.TABLE_SUFFIX);
8383
String ie = BackendTable.joinTableName(g, "i" + Edge.TABLE_SUFFIX);

hugegraph-cassandra/src/main/java/com/baidu/hugegraph/backend/store/cassandra/CassandraStore.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,15 @@ public Iterator<BackendEntry> query(Query query) {
299299
String tableName = query.olap() ? this.olapTableName(type) :
300300
type.string();
301301
CassandraTable table = this.table(tableName);
302-
Iterator<BackendEntry> entries = table.query(this.session(), query);
302+
Iterator<BackendEntry> entries = table.query(this.session(null), query);
303303
// Merge olap results as needed
304304
Set<Id> olapPks = query.olapPks();
305305
if (this.isGraphStore && !olapPks.isEmpty()) {
306306
List<Iterator<BackendEntry>> iterators = new ArrayList<>();
307307
for (Id pk : olapPks) {
308308
Query q = query.copy();
309309
table = this.table(this.olapTableName(pk));
310-
iterators.add(table.query(this.session(), q));
310+
iterators.add(table.query(this.session(null), q));
311311
}
312312
entries = new MergeIterator<>(entries, iterators,
313313
BackendEntry::mergeable);
@@ -608,11 +608,6 @@ protected CassandraSessionPool.Session session(HugeType type) {
608608
return this.sessions.session();
609609
}
610610

611-
protected CassandraSessionPool.Session session() {
612-
this.checkOpened();
613-
return this.sessions.session();
614-
}
615-
616611
protected final void checkClusterConnected() {
617612
E.checkState(this.sessions != null && this.sessions.clusterConnected(),
618613
"Cassandra cluster has not been connected");
@@ -758,7 +753,7 @@ public void checkAndRegisterOlapTable(Id id) {
758753
@Override
759754
public void createOlapTable(Id id) {
760755
CassandraTable table = new CassandraTables.Olap(this.store(), id);
761-
table.init(this.session());
756+
table.init(this.session(null));
762757
registerTableManager(this.olapTableName(id), table);
763758
}
764759

@@ -769,7 +764,7 @@ public void clearOlapTable(Id id) {
769764
if (table == null || !this.existsTable(table.table())) {
770765
throw new HugeException("Not exist table '%s'", name);
771766
}
772-
table.truncate(this.session());
767+
table.truncate(this.session(null));
773768
}
774769

775770
@Override
@@ -779,8 +774,42 @@ public void removeOlapTable(Id id) {
779774
if (table == null || !this.existsTable(table.table())) {
780775
throw new HugeException("Not exist table '%s'", name);
781776
}
782-
table.dropTable(this.session());
777+
table.dropTable(this.session(null));
783778
this.unregisterTableManager(name);
784779
}
785780
}
781+
782+
public static class CassandraSystemStore extends CassandraGraphStore {
783+
784+
private final CassandraTables.Meta meta;
785+
786+
public CassandraSystemStore(BackendStoreProvider provider,
787+
String keyspace, String store) {
788+
super(provider, keyspace, store);
789+
790+
this.meta = new CassandraTables.Meta();
791+
}
792+
793+
@Override
794+
public void init() {
795+
super.init();
796+
this.checkOpened();
797+
String driverVersion = this.provider().driverVersion();
798+
this.meta.writeVersion(this.session(null), driverVersion);
799+
LOG.info("Write down the backend version: {}", driverVersion);
800+
}
801+
802+
@Override
803+
public String storedVersion() {
804+
CassandraSessionPool.Session session = this.session(null);
805+
return this.meta.readVersion(session);
806+
}
807+
808+
@Override
809+
protected Collection<CassandraTable> tables() {
810+
List<CassandraTable> tables = new ArrayList<>(super.tables());
811+
tables.add(this.meta);
812+
return tables;
813+
}
814+
}
786815
}

hugegraph-cassandra/src/main/java/com/baidu/hugegraph/backend/store/cassandra/CassandraStoreProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.baidu.hugegraph.backend.store.BackendStore;
2424
import com.baidu.hugegraph.backend.store.cassandra.CassandraStore.CassandraGraphStore;
2525
import com.baidu.hugegraph.backend.store.cassandra.CassandraStore.CassandraSchemaStore;
26+
import com.baidu.hugegraph.backend.store.cassandra.CassandraStore.CassandraSystemStore;
2627
import com.baidu.hugegraph.config.HugeConfig;
2728

2829
public class CassandraStoreProvider extends AbstractBackendStoreProvider {
@@ -41,13 +42,18 @@ protected BackendStore newGraphStore(HugeConfig config, String store) {
4142
return new CassandraGraphStore(this, this.keyspace(), store);
4243
}
4344

45+
@Override
46+
protected BackendStore newSystemStore(HugeConfig config, String store) {
47+
return new CassandraSystemStore(this, this.keyspace(), store);
48+
}
49+
4450
@Override
4551
public String type() {
4652
return "cassandra";
4753
}
4854

4955
@Override
50-
public String version() {
56+
public String driverVersion() {
5157
/*
5258
* Versions history:
5359
* [1.0] HugeGraph-1328: supports backend table version checking
@@ -64,6 +70,7 @@ public String version() {
6470
* [1.9] #295: support ttl for vertex and edge
6571
* [1.10] #1333: support read frequency for property key
6672
* [1.11] #1506: support olap properties
73+
* [1.11] #1533: add meta table in system store
6774
*/
6875
return "1.11";
6976
}

hugegraph-cassandra/src/main/java/com/baidu/hugegraph/backend/store/cassandra/CassandraTables.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,48 @@ public class CassandraTables {
7272

7373
private static final long COMMIT_DELETE_BATCH = Query.COMMIT_BATCH;
7474

75+
public static class Meta extends CassandraTable {
76+
77+
public static final String TABLE = HugeType.META.string();
78+
79+
public Meta() {
80+
super(TABLE);
81+
}
82+
83+
@Override
84+
public void init(CassandraSessionPool.Session session) {
85+
ImmutableMap<HugeKeys, DataType> pkeys = ImmutableMap.of(
86+
HugeKeys.NAME, DataType.text()
87+
);
88+
ImmutableMap<HugeKeys, DataType> ckeys = ImmutableMap.of();
89+
ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of(
90+
HugeKeys.VALUE, DataType.text()
91+
);
92+
93+
this.createTable(session, pkeys, ckeys, columns);
94+
}
95+
96+
public void writeVersion(CassandraSessionPool.Session session,
97+
String version) {
98+
Insert insert = QueryBuilder.insertInto(TABLE);
99+
insert.value(formatKey(HugeKeys.NAME), formatKey(HugeKeys.VERSION));
100+
insert.value(formatKey(HugeKeys.VALUE), version);
101+
session.execute(insert);
102+
}
103+
104+
public String readVersion(CassandraSessionPool.Session session) {
105+
Clause where = formatEQ(HugeKeys.NAME, formatKey(HugeKeys.VERSION));
106+
Select select = QueryBuilder.select(formatKey(HugeKeys.VALUE))
107+
.from(TABLE);
108+
select.where(where);
109+
Row row = session.execute(select).one();
110+
if (row == null) {
111+
return null;
112+
}
113+
return row.getString(formatKey(HugeKeys.VALUE));
114+
}
115+
}
116+
75117
public static class Counters extends CassandraTable {
76118

77119
public static final String TABLE = HugeType.COUNTER.string();

hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.baidu.hugegraph.backend.id.Id;
3737
import com.baidu.hugegraph.backend.query.Query;
3838
import com.baidu.hugegraph.backend.store.BackendFeatures;
39-
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
39+
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
4040
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
4141
import com.baidu.hugegraph.config.HugeConfig;
4242
import com.baidu.hugegraph.config.TypedOption;
@@ -179,12 +179,10 @@ public interface HugeGraph extends Graph {
179179

180180
String backend();
181181

182-
String backendVersion();
183-
184-
BackendStoreSystemInfo backendStoreSystemInfo();
185-
186182
BackendFeatures backendStoreFeatures();
187183

184+
BackendStoreInfo backendStoreInfo();
185+
188186
GraphMode mode();
189187

190188
void mode(GraphMode mode);
@@ -209,6 +207,8 @@ public interface HugeGraph extends Graph {
209207

210208
void truncateBackend();
211209

210+
void initSystemInfo();
211+
212212
void createSnapshot();
213213

214214
void resumeSnapshot();

hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraphParams.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.baidu.hugegraph;
2121

2222
import com.baidu.hugegraph.analyzer.Analyzer;
23+
import com.baidu.hugegraph.backend.LocalCounter;
2324
import com.baidu.hugegraph.backend.serializer.AbstractSerializer;
2425
import com.baidu.hugegraph.backend.store.BackendFeatures;
2526
import com.baidu.hugegraph.backend.store.BackendStore;
@@ -80,6 +81,8 @@ public interface HugeGraphParams {
8081

8182
ServerInfoManager serverManager();
8283

84+
LocalCounter counter();
85+
8386
AbstractSerializer serializer();
8487

8588
Analyzer analyzer();

0 commit comments

Comments
 (0)