Skip to content

Commit

Permalink
Add system schema store
Browse files Browse the repository at this point in the history
Change-Id: I2feabace71f266d6be6c2094c30cd19f74ab7f7d
  • Loading branch information
Linary committed Jul 2, 2021
1 parent f41c6e9 commit 4cd1edb
Show file tree
Hide file tree
Showing 24 changed files with 186 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.store.BackendFeatures;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.config.AuthOptions;
import com.baidu.hugegraph.config.HugeConfig;
Expand Down Expand Up @@ -597,9 +597,9 @@ public String backendVersion() {
}

@Override
public BackendStoreSystemInfo backendStoreSystemInfo() {
public BackendStoreInfo backendStoreInfo() {
this.verifyAdminPermission();
return this.hugegraph.backendStoreSystemInfo();
return this.hugegraph.backendStoreInfo();
}

@Override
Expand Down Expand Up @@ -721,6 +721,18 @@ public void truncateBackend() {
}
}

@Override
public void initSystemInfo() {
this.verifyAdminPermission();
this.hugegraph.initSystemInfo();
}

@Override
public void initBackendInfo() {
this.verifyAdminPermission();
this.hugegraph.initBackendInfo();
}

@Override
public void createSnapshot() {
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
Expand Down Expand Up @@ -967,6 +979,12 @@ public HugeGraph graph() {
return this.taskScheduler.graph();
}

@Override
public void init() {
verifyAdminPermission();
this.taskScheduler.init();
}

@Override
public int pendingTasks() {
verifyTaskPermission(HugePermission.READ);
Expand Down Expand Up @@ -1136,6 +1154,12 @@ private String currentUsername() {
return null;
}

@Override
public void init() {
verifyAdminPermission();
this.authManager.init();
}

@Override
public boolean close() {
verifyAdminPermission();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import com.baidu.hugegraph.backend.cache.Cache;
import com.baidu.hugegraph.backend.cache.CacheManager;
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.exception.NotSupportException;
Expand Down Expand Up @@ -273,7 +273,7 @@ private void checkBackendVersionOrExit(HugeConfig config) {
}
}
}
BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo();
BackendStoreInfo info = hugegraph.backendStoreInfo();
if (!info.exists()) {
throw new BackendException(
"The backend store of '%s' has not been initialized",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.store.BackendFeatures;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.TypedOption;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Client;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Server;
Expand Down Expand Up @@ -134,7 +133,7 @@ public interface HugeGraph extends Graph {
public String name();
public String backend();
public String backendVersion();
public BackendStoreSystemInfo backendStoreSystemInfo();
public BackendStoreInfo backendStoreInfo();
public BackendFeatures backendStoreFeatures();

public GraphMode mode();
Expand All @@ -154,6 +153,9 @@ public interface HugeGraph extends Graph {
public void clearBackend();
public void truncateBackend();

public void initSystemInfo();
public void initBackendInfo();

public void createSnapshot();
public void resumeSnapshot();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
import com.baidu.hugegraph.backend.store.BackendFeatures;
import com.baidu.hugegraph.backend.store.BackendProviderFactory;
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.BackendStoreInfo;
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.raft.RaftBackendStoreProvider;
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.backend.store.ram.RamTable;
Expand Down Expand Up @@ -240,8 +240,8 @@ public String backendVersion() {
}

@Override
public BackendStoreSystemInfo backendStoreSystemInfo() {
return new BackendStoreSystemInfo(this.schemaTransaction());
public BackendStoreInfo backendStoreInfo() {
return new BackendStoreInfo(this.schemaTransaction());
}

@Override
Expand All @@ -251,6 +251,9 @@ public BackendFeatures backendStoreFeatures() {

@Override
public void serverStarted(Id serverId, NodeRole serverRole) {
LOG.info("Init system info for graph '{}'", this.name);
this.initSystemInfo();

LOG.info("Init server info [{}-{}] for graph '{}'...",
serverId, serverRole, this.name);
this.serverInfoManager().initServerInfo(serverId, serverRole);
Expand Down Expand Up @@ -320,7 +323,7 @@ public void initBackend() {
LockUtil.lock(this.name, LockUtil.GRAPH_LOCK);
try {
this.storeProvider.init();
this.storeProvider.initSystemInfo(this);
this.initBackendInfo();
} finally {
LockUtil.unlock(this.name, LockUtil.GRAPH_LOCK);
this.loadGraphStore().close();
Expand Down Expand Up @@ -359,9 +362,7 @@ public void truncateBackend() {
LockUtil.lock(this.name, LockUtil.GRAPH_LOCK);
try {
this.storeProvider.truncate();
this.storeProvider.initSystemInfo(this);
this.serverStarted(this.serverInfoManager().selfServerId(),
this.serverInfoManager().selfServerRole());
this.initBackendInfo();
/*
* Take the initiative to generate a snapshot, it can avoid this
* situation: when the server restart need to read the database
Expand All @@ -380,6 +381,26 @@ public void truncateBackend() {
LOG.info("Graph '{}' has been truncated", this.name);
}

@Override
public void initSystemInfo() {
// Initialize user schema
try {
this.serverInfoManager().init();
this.taskScheduler().init();
this.authManager().init();
} finally {
this.closeTx();
}
LOG.debug("Graph '{}' system info has been initialized", this);
}

@Override
public void initBackendInfo() {
BackendStoreInfo info = this.backendStoreInfo();
info.init();
LOG.debug("Graph '{}' backend info has been initialized", this);
}

@Override
public void createSnapshot() {
LockUtil.lock(this.name, LockUtil.GRAPH_LOCK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

public interface AuthManager {

public void init();
public boolean close();

public Id createUser(HugeUser user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.security.sasl.AuthenticationException;

Expand All @@ -36,20 +35,16 @@
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.config.AuthOptions;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.event.EventListener;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Events;
import com.baidu.hugegraph.util.StringEncoding;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import io.jsonwebtoken.Claims;

public class StandardAuthManager implements AuthManager {

private final HugeGraphParams graph;
private final EventListener eventListener;

// Cache <username, HugeUser>
private final Cache<Id, HugeUser> usersCache;
Expand All @@ -76,7 +71,6 @@ public StandardAuthManager(HugeGraphParams graph) {
this.tokenExpire = config.get(AuthOptions.AUTH_TOKEN_EXPIRE);

this.graph = graph;
this.eventListener = this.listenChanges();
this.usersCache = this.cache("users", capacity, expired);
this.pwdCache = this.cache("users_pwd", capacity, expired);
this.tokenCache = this.cache("token", capacity, expired);
Expand Down Expand Up @@ -108,36 +102,8 @@ private <V> Cache<Id, V> cache(String prefix, long capacity,
return cache;
}

private EventListener listenChanges() {
// Listen store event: "store.inited"
Set<String> storeEvents = ImmutableSet.of(Events.STORE_INITED);
EventListener eventListener = event -> {
// Ensure user schema create after system info initialized
if (storeEvents.contains(event.name())) {
try {
this.initSchemaIfNeeded();
} finally {
this.graph.closeTx();
}
return true;
}
return false;
};
this.graph.loadSystemStore().provider().listen(eventListener);
return eventListener;
}

private void unlistenChanges() {
this.graph.loadSystemStore().provider().unlisten(this.eventListener);
}

@Override
public boolean close() {
this.unlistenChanges();
return true;
}

private void initSchemaIfNeeded() {
public void init() {
this.invalidateUserCache();
HugeUser.schema(this.graph).initSchemaIfNeeded();
HugeGroup.schema(this.graph).initSchemaIfNeeded();
Expand All @@ -146,6 +112,11 @@ private void initSchemaIfNeeded() {
HugeAccess.schema(this.graph).initSchemaIfNeeded();
}

@Override
public boolean close() {
return true;
}

private void invalidateUserCache() {
this.usersCache.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.baidu.hugegraph.backend.store.BackendMutation;
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
import com.baidu.hugegraph.backend.store.SystemSchemaStore;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.StringEncoding;
Expand Down Expand Up @@ -62,6 +63,11 @@ public BackendStoreProvider provider() {
return this.store.provider();
}

@Override
public SystemSchemaStore systemSchemaStore() {
return this.store.systemSchemaStore();
}

@Override
public void open(HugeConfig config) {
this.store.open(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
public abstract class AbstractBackendStore<Session extends BackendSession>
implements BackendStore {

private final SystemSchemaStore systemSchemaStore;
private final MetaDispatcher<Session> dispatcher;

public AbstractBackendStore() {
this.systemSchemaStore = new SystemSchemaStore();
this.dispatcher = new MetaDispatcher<>();
}

Expand All @@ -39,6 +41,11 @@ public void registerMetaHandler(String name, MetaHandler<Session> handler) {
this.dispatcher.registerMetaHandler(name, handler);
}

@Override
public SystemSchemaStore systemSchemaStore() {
return this.systemSchemaStore;
}

// Get metadata by key
@Override
public <R> R metadata(HugeType type, String meta, Object[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.slf4j.Logger;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.store.raft.StoreSnapshotFile;
import com.baidu.hugegraph.event.EventHub;
Expand Down Expand Up @@ -141,16 +140,6 @@ public void truncate() {
LOG.debug("Graph '{}' store has been truncated", this.graph);
}

@Override
public void initSystemInfo(HugeGraph graph) {
this.checkOpened();
BackendStoreSystemInfo info = graph.backendStoreSystemInfo();
info.init();
this.notifyAndWaitEvent(Events.STORE_INITED);

LOG.debug("Graph '{}' system info has been initialized", this.graph);
}

@Override
public void createSnapshot() {
String snapshotPrefix = StoreSnapshotFile.SNAPSHOT_DIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public interface BackendStore {
// Get the parent provider
public BackendStoreProvider provider();

// Get the system schema store
public SystemSchemaStore systemSchemaStore();

// Whether it is the storage of schema
public boolean isSchemaStore();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;

public class BackendStoreSystemInfo {
public class BackendStoreInfo {

private static final Logger LOG = Log.logger(HugeGraph.class);

private static final String PK_BACKEND_INFO = Hidden.hide("backend_info");

private final SchemaTransaction schemaTx;

public BackendStoreSystemInfo(SchemaTransaction schemaTx) {
public BackendStoreInfo(SchemaTransaction schemaTx) {
this.schemaTx = schemaTx;
}

Expand Down
Loading

0 comments on commit 4cd1edb

Please sign in to comment.