Skip to content

Commit

Permalink
chore:redirect CUD task to master
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Feb 28, 2023
1 parent ac55871 commit fab5a17
Show file tree
Hide file tree
Showing 29 changed files with 348 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.apache.hugegraph.api.filter;

import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URI;
import java.net.URISyntaxException;

import jakarta.ws.rs.NameBinding;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.hugegraph.election.GlobalMasterInfo;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;

public class RedirectFilter implements ContainerRequestFilter {

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

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
GlobalMasterInfo instance = GlobalMasterInfo.instance();
if (!instance.isFeatureSupport()) {
return;
}

String url = "";
synchronized (instance) {
if (instance.isMaster() || StringUtils.isEmpty(instance.url())) {
return;
}
url = instance.url();
}

URI redirectUri = null;
try {
URIBuilder redirectURIBuilder = new URIBuilder(requestContext.getUriInfo().getAbsolutePath());
String[] host = url.split(":");
redirectURIBuilder.setHost(host[0]);
if (host.length == 2 && StringUtils.isNotEmpty(host[1].trim())) {
redirectURIBuilder.setPort(Integer.parseInt(host[1].trim()));
}

redirectUri = redirectURIBuilder.build();
} catch (URISyntaxException e) {
LOG.error("Redirect request exception occurred", e);
return;
}
requestContext.abortWith(Response.temporaryRedirect(redirectUri).build());
}

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface RedirectMasterRole {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.apache.hugegraph.api.filter;

import jakarta.ws.rs.container.DynamicFeature;
import jakarta.ws.rs.container.ResourceInfo;
import jakarta.ws.rs.core.FeatureContext;
import jakarta.ws.rs.ext.Provider;

@Provider
public class RedirectFilterDynamicFeature implements DynamicFeature {

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (resourceInfo.getResourceMethod().isAnnotationPresent(RedirectFilter.RedirectMasterRole.class)) {
context.register(RedirectFilter.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Map;

import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.server.RestServer;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class AlgorithmAPI extends API {
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RedirectFilter.RedirectMasterRole
public Map<String, Id> post(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String algorithm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;

import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.slf4j.Logger;

Expand Down Expand Up @@ -58,6 +59,7 @@ public class ComputerAPI extends API {
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RedirectFilter.RedirectMasterRole
public Map<String, Id> post(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String computer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;

import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.apache.hugegraph.metrics.MetricsUtil;
Expand All @@ -58,6 +59,7 @@
@Path("graphs/{graph}/jobs/gremlin")
@Singleton
@Tag(name = "GremlinAPI")
@RedirectFilter.RedirectMasterRole
public class GremlinAPI extends API {

private static final Logger LOG = Log.logger(GremlinAPI.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;

import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.api.filter.StatusFilter.Status;
import org.apache.hugegraph.core.GraphManager;
import org.slf4j.Logger;
Expand All @@ -42,6 +43,7 @@
@Path("graphs/{graph}/jobs/rebuild")
@Singleton
@Tag(name = "RebuildAPI")
@RedirectFilter.RedirectMasterRole
public class RebuildAPI extends API {

private static final Logger LOG = Log.logger(RebuildAPI.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import jakarta.ws.rs.core.Context;

import org.apache.groovy.util.Maps;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.api.filter.StatusFilter.Status;
import org.apache.hugegraph.core.GraphManager;
import org.slf4j.Logger;
Expand Down Expand Up @@ -132,6 +133,7 @@ public Map<String, Object> get(@Context GraphManager manager,
@DELETE
@Timed
@Path("{id}")
@RedirectFilter.RedirectMasterRole
public void delete(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("id") long id) {
Expand All @@ -147,6 +149,7 @@ public void delete(@Context GraphManager manager,
@Path("{id}")
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RedirectFilter.RedirectMasterRole
public Map<String, Object> update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("id") long id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.ws.rs.core.Context;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class EdgeLabelAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=edge_label_write"})
@RedirectFilter.RedirectMasterRole
public String create(@Context GraphManager manager,
@PathParam("graph") String graph,
JsonEdgeLabel jsonEdgeLabel) {
Expand All @@ -86,6 +88,7 @@ public String create(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=edge_label_write"})
@RedirectFilter.RedirectMasterRole
public String update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name,
Expand Down Expand Up @@ -156,6 +159,7 @@ public String get(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=edge_label_delete"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> delete(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.ws.rs.core.Context;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class IndexLabelAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_label_write"})
@RedirectFilter.RedirectMasterRole
public String create(@Context GraphManager manager,
@PathParam("graph") String graph,
JsonIndexLabel jsonIndexLabel) {
Expand All @@ -88,6 +90,7 @@ public String create(@Context GraphManager manager,
@Path("{name}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RedirectFilter.RedirectMasterRole
public String update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name,
Expand Down Expand Up @@ -157,6 +160,7 @@ public String get(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_label_delete"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> delete(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.ws.rs.core.Context;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class PropertyKeyAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=property_key_write"})
@RedirectFilter.RedirectMasterRole
public String create(@Context GraphManager manager,
@PathParam("graph") String graph,
JsonPropertyKey jsonPropertyKey) {
Expand All @@ -93,6 +95,7 @@ public String create(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=property_key_write"})
@RedirectFilter.RedirectMasterRole
public String update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name,
Expand Down Expand Up @@ -178,6 +181,7 @@ public String get(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=property_key_delete"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> delete(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.ws.rs.core.Context;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class VertexLabelAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=vertex_label_write"})
@RedirectFilter.RedirectMasterRole
public String create(@Context GraphManager manager,
@PathParam("graph") String graph,
JsonVertexLabel jsonVertexLabel) {
Expand All @@ -87,6 +89,7 @@ public String create(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=vertex_label_write"})
@RedirectFilter.RedirectMasterRole
public String update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name,
Expand Down Expand Up @@ -159,6 +162,7 @@ public String get(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=vertex_label_delete"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> delete(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void setup(HugeConfig config) {
String raftGroupPeers = config.get(ServerOptions.RAFT_GROUP_PEERS);
graphConfig.addProperty(ServerOptions.RAFT_GROUP_PEERS.name(),
raftGroupPeers);
this.addRoleWorkerConfig(graphConfig, config);

this.graph = (HugeGraph) GraphFactory.open(graphConfig);

Expand All @@ -137,6 +138,21 @@ public void setup(HugeConfig config) {
}
}

private void addRoleWorkerConfig(HugeConfig graphConfig, HugeConfig config) {
graphConfig.addProperty(CoreOptions.NODE_EXTERNAL_URL.name(),
config.get(CoreOptions.NODE_EXTERNAL_URL));
graphConfig.addProperty(CoreOptions.BASE_TIMEOUT_MILLISECOND.name(),
config.get(CoreOptions.BASE_TIMEOUT_MILLISECOND));
graphConfig.addProperty(CoreOptions.EXCEEDS_FAIL_COUNT.name(),
config.get(CoreOptions.EXCEEDS_FAIL_COUNT));
graphConfig.addProperty(CoreOptions.RANDOM_TIMEOUT_MILLISECOND.name(),
config.get(CoreOptions.RANDOM_TIMEOUT_MILLISECOND));
graphConfig.addProperty(CoreOptions.HEARTBEAT_INTERVAL_SECOUND.name(),
config.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND));
graphConfig.addProperty(CoreOptions.EXCEEDS_WORKER_COUNT.name(),
config.get(CoreOptions.EXCEEDS_WORKER_COUNT));
}

/**
* Verify if a user is legal
* @param username the username for authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ private void loadGraph(String name, String graphConfPath) {
String raftGroupPeers = this.conf.get(ServerOptions.RAFT_GROUP_PEERS);
config.addProperty(ServerOptions.RAFT_GROUP_PEERS.name(),
raftGroupPeers);
this.addRoleWorkerConfig(config);

Graph graph = GraphFactory.open(config);
this.graphs.put(name, graph);
Expand All @@ -386,6 +387,21 @@ private void loadGraph(String name, String graphConfPath) {
}
}

private void addRoleWorkerConfig(HugeConfig config) {
config.addProperty(CoreOptions.NODE_EXTERNAL_URL.name(),
this.conf.get(CoreOptions.NODE_EXTERNAL_URL));
config.addProperty(CoreOptions.BASE_TIMEOUT_MILLISECOND.name(),
this.conf.get(CoreOptions.BASE_TIMEOUT_MILLISECOND));
config.addProperty(CoreOptions.EXCEEDS_FAIL_COUNT.name(),
this.conf.get(CoreOptions.EXCEEDS_FAIL_COUNT));
config.addProperty(CoreOptions.RANDOM_TIMEOUT_MILLISECOND.name(),
this.conf.get(CoreOptions.RANDOM_TIMEOUT_MILLISECOND));
config.addProperty(CoreOptions.HEARTBEAT_INTERVAL_SECOUND.name(),
this.conf.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND));
config.addProperty(CoreOptions.EXCEEDS_WORKER_COUNT.name(),
this.conf.get(CoreOptions.EXCEEDS_WORKER_COUNT));
}

private void waitGraphsReady() {
if (!this.rpcServer.enabled()) {
LOG.info("RpcServer is not enabled, skip wait graphs ready");
Expand Down
Loading

0 comments on commit fab5a17

Please sign in to comment.