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 32075c4
Show file tree
Hide file tree
Showing 27 changed files with 316 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 @@ -17,6 +17,7 @@

package org.apache.hugegraph.server;

import org.apache.hugegraph.api.filter.RedirectFilterDynamicFeature;
import org.apache.tinkerpop.gremlin.server.util.MetricManager;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.MultiException;
Expand Down Expand Up @@ -67,6 +68,8 @@ public ApplicationConfig(HugeConfig conf, EventHub hub) {
// Register to use the jsr250 annotations @RolesAllowed
register(RolesAllowedDynamicFeature.class);

register(RedirectFilterDynamicFeature.class);

// Register HugeConfig to context
register(new ConfFactory(conf));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public void serverStarted(Id serverId, NodeRole serverRole) {

private void initRoleStateWorker(Id serverId) {
Config roleStateMachineConfig = new HugeRoleStateMachineConfig(serverId.toString(),
this.configuration.get(CoreOptions.NODE_EXTERNAL_URL),
this.configuration.get(CoreOptions.EXCEEDS_FAIL_COUNT),
this.configuration.get(CoreOptions.RANDOM_TIMEOUT_MILLISECOND),
this.configuration.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,14 @@ public static synchronized CoreOptions instance() {
2
);

public static final ConfigOption<String> NODE_EXTERNAL_URL =
new ConfigOption<>(
"server.role.node_external_url",
"The url of external accessibility",
disallowEmpty(),
"127.0.0.1:8080"
);

public static final ConfigOption<Integer> RANDOM_TIMEOUT_MILLISECOND =
new ConfigOption<>(
"server.role.random_timeout",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface Config {

String node();

String url();

int exceedsFailCount();

long randomTimeoutMillisecond();
Expand Down
Loading

0 comments on commit 32075c4

Please sign in to comment.