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

feat: support task auto manage by server role state machine #2130

Merged
merged 16 commits into from
Mar 8, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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 java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jakarta.ws.rs.NameBinding;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.Log;
import org.glassfish.hk2.api.IterableProvider;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.jersey.message.internal.HeaderUtils;
import org.slf4j.Logger;

public class RedirectFilter implements ContainerRequestFilter {

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

private static final String X_HG_REDIRECT = "x-hg-redirect";

private static volatile Client client = null;

@Context
private IterableProvider<GraphManager> managerProvider;

private static final Set<String> MUST_BE_NULL = new HashSet<>();

static {
MUST_BE_NULL.add("DELETE");
MUST_BE_NULL.add("GET");
MUST_BE_NULL.add("HEAD");
MUST_BE_NULL.add("TRACE");
}

@Override
public void filter(ContainerRequestContext context) throws IOException {
ServiceHandle<GraphManager> handle = this.managerProvider.getHandle();
E.checkState(handle != null, "Context GraphManager is absent");
GraphManager manager = handle.getService();
E.checkState(manager != null, "Context GraphManager is absent");
GlobalMasterInfo globalMasterInfo = manager.globalMasterInfo();
if (globalMasterInfo == null || !globalMasterInfo.isFeatureSupport()) {
return;
}

String redirectTag = context.getHeaderString(X_HG_REDIRECT);
if (StringUtils.isNotEmpty(redirectTag)) {
return;
}

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

URI redirectUri = null;
try {
URIBuilder redirectURIBuilder = new URIBuilder(context.getUriInfo().getRequestUri());
URI masterURI = URI.create(url);
redirectURIBuilder.setHost(masterURI.getHost());
redirectURIBuilder.setPort(masterURI.getPort());
redirectURIBuilder.setScheme(masterURI.getScheme());

redirectUri = redirectURIBuilder.build();
} catch (URISyntaxException e) {
LOG.error("Redirect request exception occurred", e);
return;
}
this.initClientIfNeeded();
Response response = this.forwardRequest(context, redirectUri);
context.abortWith(response);
}

private Response forwardRequest(ContainerRequestContext requestContext, URI redirectUri) {
MultivaluedMap<String, String> headers = requestContext.getHeaders();
MultivaluedMap<String, Object> newHeaders = HeaderUtils.createOutbound();
if (headers != null) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
newHeaders.add(entry.getKey(), value);
}
}
}
newHeaders.add(X_HG_REDIRECT, new Date().getTime());
Invocation.Builder builder = client.target(redirectUri)
.request()
.headers(newHeaders);
Response response = null;
if (MUST_BE_NULL.contains(requestContext.getMethod())) {
response = builder.method(requestContext.getMethod());
} else {
response = builder.method(requestContext.getMethod(),
Entity.json(requestContext.getEntityStream()));
}
return response;
}

private void initClientIfNeeded() {
if (client != null) {
return;
}

synchronized (RedirectFilter.class) {
if (client != null) {
return;
}

client = ClientBuilder.newClient();
}
}

@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 @@ -30,6 +30,7 @@
import jakarta.ws.rs.core.UriInfo;

import org.apache.hugegraph.api.filter.CompressInterceptor.Compress;
import org.apache.hugegraph.api.filter.RedirectFilter;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.metrics.MetricsUtil;
import com.codahale.metrics.Histogram;
Expand All @@ -51,6 +52,7 @@ public class GremlinAPI extends GremlinQueryAPI {
@Compress
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RedirectFilter.RedirectMasterRole
public Response post(@Context HugeConfig conf,
@Context HttpHeaders headers,
String request) {
Expand Down
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 Down Expand Up @@ -73,6 +74,7 @@ public class GremlinAPI extends API {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=gremlin_execute"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> post(@Context GraphManager manager,
@PathParam("graph") String graph,
GremlinRequest request) {
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 @@ -52,6 +53,7 @@ public class RebuildAPI extends API {
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe ACCEPTED and RedirectMasterRole often appear simultaneously

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, if ACCEPTED appear, the RedirectMasterRole also appear

public Map<String, Id> vertexLabelRebuild(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand All @@ -68,6 +70,7 @@ public Map<String, Id> vertexLabelRebuild(@Context GraphManager manager,
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> edgeLabelRebuild(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name) {
Expand All @@ -84,6 +87,7 @@ public Map<String, Id> edgeLabelRebuild(@Context GraphManager manager,
@Status(Status.ACCEPTED)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "$owner=$graph $action=index_write"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> indexLabelRebuild(@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 @@ -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 @@ -34,6 +34,7 @@
import jakarta.ws.rs.QueryParam;
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 @@ -151,6 +152,7 @@ public Map<String, String> setLeader(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> addPeer(@Context GraphManager manager,
@PathParam("graph") String graph,
@QueryParam("group") @DefaultValue("default")
Expand Down Expand Up @@ -180,6 +182,7 @@ public Map<String, Id> addPeer(@Context GraphManager manager,
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
@RedirectFilter.RedirectMasterRole
public Map<String, Id> removePeer(@Context GraphManager manager,
@PathParam("graph") String graph,
@QueryParam("group")
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
Loading