-
Notifications
You must be signed in to change notification settings - Fork 525
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
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fa0dbe9
feat: support task auto manager by server role
zyxxoo 2e3c28c
chore: improve
zyxxoo b0f9970
chore: improve
zyxxoo 6dbcc63
chore: computer node don't elected
zyxxoo 3344b34
chore: improve
zyxxoo 35141ff
chore:redirect CUD task to master
zyxxoo 88ac56c
chore: improve stable
zyxxoo 2e84b0f
fix: 401
zyxxoo 842df32
improve
zyxxoo ebef96d
fix: inject failed
zyxxoo c5db1cf
chore: improve code
zyxxoo d7832cf
chore: improve code
zyxxoo a9e4c24
chore: improve
zyxxoo 3b09579
chore: improve code
zyxxoo 67b8404
chore: improve
zyxxoo 5523064
optimize all imports & tiny improve
imbajin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...graph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilterDynamicFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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