Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can't dynamic create graph if auth enabled #1708

Merged
merged 1 commit into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,18 @@ public void resumeSnapshot() {
this.hugegraph.resumeSnapshot();
}

@Override
public void drop() {
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
this.hugegraph.drop();
}

@Override
public HugeConfig cloneConfig() {
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
return this.hugegraph.cloneConfig();
}

private <V> Cache<Id, V> cache(String prefix, long capacity,
long expiredTime) {
String name = prefix + "-" + this.hugegraph.name();
Expand Down Expand Up @@ -1169,7 +1181,7 @@ public HugeUser deleteUser(Id id) {
E.checkArgument(!HugeAuthenticator.USER_ADMIN.equals(user.name()),
"Can't delete user '%s'", user.name());
verifyUserPermission(HugePermission.DELETE, user);
auditLimiters.invalidate(user.id());
HugeGraphAuthProxy.this.auditLimiters.invalidate(user.id());
this.invalidRoleCache();
return this.authManager.deleteUser(id);
}
Expand Down Expand Up @@ -1472,7 +1484,7 @@ public UserWithRole validateUser(String username, String password) {

try {
Id userKey = IdGenerator.of(username + password);
return usersRoleCache.getOrFetch(userKey, id -> {
return HugeGraphAuthProxy.this.usersRoleCache.getOrFetch(userKey, id -> {
return this.authManager.validateUser(username, password);
});
} catch (Exception e) {
Expand All @@ -1491,7 +1503,7 @@ public UserWithRole validateUser(String token) {

try {
Id userKey = IdGenerator.of(token);
return usersRoleCache.getOrFetch(userKey, id -> {
return HugeGraphAuthProxy.this.usersRoleCache.getOrFetch(userKey, id -> {
return this.authManager.validateUser(token);
});
} catch (Exception e) {
Expand Down Expand Up @@ -1522,7 +1534,7 @@ private void switchAuthManager(AuthManager authManager) {
}

private void invalidRoleCache() {
usersRoleCache.clear();
HugeGraphAuthProxy.this.usersRoleCache.clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

import com.baidu.hugegraph.HugeFactory;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.auth.AuthManager;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.auth.AuthManager;
import com.baidu.hugegraph.auth.HugeAuthenticator;
import com.baidu.hugegraph.auth.HugeFactoryAuthProxy;
import com.baidu.hugegraph.auth.HugeGraphAuthProxy;
Expand Down Expand Up @@ -169,8 +169,7 @@ public HugeGraph cloneGraph(String name, String newName,
"The graph '%s' has existed", newName);
PropertiesConfiguration propConfig = this.buildConfig(configText);

HugeConfig config = (HugeConfig) g.configuration();
HugeConfig cloneConfig = (HugeConfig) config.clone();
HugeConfig cloneConfig = g.cloneConfig();
cloneConfig.setDelimiterParsingDisabled(true);
// Use the passed config to overwrite the old one
propConfig.getKeys().forEachRemaining(key -> {
Expand Down Expand Up @@ -234,8 +233,10 @@ private PropertiesConfiguration buildConfig(String configText) {
private void checkOptions(HugeConfig config) {
// The store cannot be the same as the existing graph
this.checkOptionsUnique(config, CoreOptions.STORE);
// NOTE: rocksdb can't use same data path for different graph,
// but it's not easy to check here
/*
* NOTE: rocksdb can't use same data path for different graph,
* but it's not easy to check here
*/
String backend = config.get(CoreOptions.BACKEND);
if (backend.equalsIgnoreCase("rocksdb")) {
// TODO: should check data path...
Expand All @@ -248,16 +249,9 @@ public void dropGraph(String name) {
E.checkArgument(this.graphs.size() > 1,
"The graph '%s' is the only one, not allowed to delete",
name);
g.clearBackend();
try {
g.close();
} catch (Exception e) {
LOG.warn("Failed to close graph", e);
}
g.drop();
// Let gremlin server and rest server context remove graph
this.eventHub.notify(Events.GRAPH_DROP, name);
HugeConfig config = (HugeConfig) g.configuration();
ConfigUtil.deleteFile(config.getFile());
}

public Set<String> graphs() {
Expand Down Expand Up @@ -496,11 +490,10 @@ private void checkOptionsUnique(HugeConfig config,
Object incomingValue = config.get(option);
for (String graphName : this.graphs.keySet()) {
HugeGraph graph = this.graph(graphName);
HugeConfig oldConfig = (HugeConfig) graph.configuration();
Object existedValue = oldConfig.get(option);
Object existedValue = graph.option(option);
E.checkArgument(!incomingValue.equals(existedValue),
"The option '%s' conflict with existed",
option.name());
"The value '%s' of option '%s' conflicts with " +
"existed graph", incomingValue, option.name());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static HugeGraph open(URL url) {
return open(getRemoteConfig(url));
}

public static void remove(HugeGraph graph) {
String name = graph.option(CoreOptions.STORE);
graphs.remove(name);
}

public static void checkGraphName(String name, String configFile) {
E.checkArgument(name.matches(NAME_REGEX),
"Invalid graph name '%s' in %s, " +
Expand Down Expand Up @@ -144,10 +149,4 @@ public static void shutdown(long timeout) {
throw new HugeException("Failed to shutdown", e);
}
}

public static void remove(HugeGraph graph) {
HugeConfig config = (HugeConfig) graph.configuration();
String name = config.get(CoreOptions.STORE);
graphs.remove(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.baidu.hugegraph.backend.store.BackendFeatures;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.raft.RaftGroupManager;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.TypedOption;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Client;
import com.baidu.hugegraph.rpc.RpcServiceConfig4Server;
Expand Down Expand Up @@ -157,6 +158,10 @@ public interface HugeGraph extends Graph {
public void createSnapshot();
public void resumeSnapshot();

public void drop();

public HugeConfig cloneConfig();

@Override
public HugeFeatures features();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.GraphReadMode;
import com.baidu.hugegraph.type.define.NodeRole;
import com.baidu.hugegraph.util.ConfigUtil;
import com.baidu.hugegraph.util.DateUtil;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Events;
Expand Down Expand Up @@ -129,7 +130,8 @@ public class StandardHugeGraph implements HugeGraph {
CoreOptions.OLTP_COLLECTION_TYPE,
CoreOptions.VERTEX_DEFAULT_LABEL,
CoreOptions.VERTEX_ENCODE_PK_NUMBER,
CoreOptions.STORE_GRAPH
CoreOptions.STORE_GRAPH,
CoreOptions.STORE
);

private static final Logger LOG = Log.logger(HugeGraph.class);
Expand Down Expand Up @@ -904,6 +906,30 @@ public synchronized void close() throws Exception {
this.name);
}

@Override
public void drop() {
this.clearBackend();
ConfigUtil.deleteFile(this.configuration().getFile());

try {
/*
* It's hard to ensure all threads close the tx.
* TODO:
* - schedule a tx-close to each thread,
* or
* - add forceClose() method to backend store.
*/
this.close();
} catch (Throwable e) {
LOG.warn("Failed to close graph {}", e, this);
}
}

@Override
public HugeConfig cloneConfig() {
return (HugeConfig) this.configuration().clone();
}

@Override
public HugeFeatures features() {
return this.features;
Expand Down