-
Notifications
You must be signed in to change notification settings - Fork 6
Upgrade to Vertx 4 #38
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
Open
kkoz
wants to merge
2
commits into
glencoesoftware:master
Choose a base branch
from
kkoz:vertx_4_upgrade
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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
157 changes: 82 additions & 75 deletions
157
src/main/java/com/glencoesoftware/omero/ms/core/OmeroVerticleFactory.java
This file contains hidden or 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 |
---|---|---|
@@ -1,75 +1,82 @@ | ||
/* | ||
* Copyright (C) 2019 Glencoe Software, Inc. All rights reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
package com.glencoesoftware.omero.ms.core; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
import io.vertx.core.Verticle; | ||
import io.vertx.core.spi.VerticleFactory; | ||
|
||
/** | ||
* Spring backed OMERO verticle factory. Implementation cribbed from the | ||
* <code>SpringVerticleFactory</code> of the Vert.x Spring worker example. | ||
* @author Chris Allan <[email protected]> | ||
* | ||
*/ | ||
public class OmeroVerticleFactory | ||
implements VerticleFactory, ApplicationContextAware { | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
@Override | ||
public boolean blockingCreate() { | ||
// Usually verticle instantiation is fast but since our verticles are | ||
// Spring Beans, they might depend on other beans/resources which are | ||
// slow to build/lookup. | ||
return true; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see io.vertx.core.spi.VerticleFactory#prefix() | ||
*/ | ||
@Override | ||
public String prefix() { | ||
return "omero"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see io.vertx.core.spi.VerticleFactory#createVerticle(java.lang.String, java.lang.ClassLoader) | ||
*/ | ||
@Override | ||
public Verticle createVerticle( | ||
String verticleName, ClassLoader classLoader) | ||
throws Exception { | ||
return (Verticle) applicationContext.getBean( | ||
VerticleFactory.removePrefix(verticleName)); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) | ||
*/ | ||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) | ||
throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
} | ||
/* | ||
* Copyright (C) 2019 Glencoe Software, Inc. All rights reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
package com.glencoesoftware.omero.ms.core; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
import io.vertx.core.Promise; | ||
import io.vertx.core.Verticle; | ||
import io.vertx.core.spi.VerticleFactory; | ||
|
||
/** | ||
* Spring backed OMERO verticle factory. Implementation cribbed from the | ||
* <code>SpringVerticleFactory</code> of the Vert.x Spring worker example. | ||
* @author Chris Allan <[email protected]> | ||
* | ||
*/ | ||
public class OmeroVerticleFactory | ||
implements VerticleFactory, ApplicationContextAware { | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
public boolean blockingCreate() { | ||
// Usually verticle instantiation is fast but since our verticles are | ||
// Spring Beans, they might depend on other beans/resources which are | ||
// slow to build/lookup. | ||
return true; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see io.vertx.core.spi.VerticleFactory#prefix() | ||
*/ | ||
@Override | ||
public String prefix() { | ||
return "omero"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see io.vertx.core.spi.VerticleFactory#createVerticle(java.lang.String, java.lang.ClassLoader) | ||
*/ | ||
public Verticle createVerticle( | ||
String verticleName, ClassLoader classLoader) | ||
throws Exception { | ||
return (Verticle) applicationContext.getBean( | ||
VerticleFactory.removePrefix(verticleName)); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) | ||
*/ | ||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) | ||
throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public void createVerticle(String verticleName, ClassLoader classLoader, Promise<Callable<Verticle>> promise) { | ||
Callable<Verticle> callable = () -> createVerticle(verticleName, classLoader); | ||
promise.complete(callable); | ||
} | ||
|
||
} |
206 changes: 103 additions & 103 deletions
206
src/main/java/com/glencoesoftware/omero/ms/core/VertxHttpServerAdapter.java
This file contains hidden or 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 |
---|---|---|
@@ -1,103 +1,103 @@ | ||
/* | ||
* Copyright 2013-2019 The OpenZipkin Authors | ||
* | ||
* Licensed 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 com.glencoesoftware.omero.ms.core; | ||
import brave.Span; | ||
import brave.http.HttpServerAdapter; | ||
import brave.internal.Nullable; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.net.SocketAddress; | ||
/** | ||
* Ported from <code>brave.vertx.web.VertxHttpServerAdapter</code> at tag | ||
* <code>release-5.6.8</code>. That implementation is package private so | ||
* cannot be used by us unless we bring the entire thing in. | ||
* | ||
* Why use a thread local for the http route when parsing {@linkplain HttpServerResponse}?</h3> | ||
* | ||
* <p>When parsing the response, we use a thread local to make the current route's path visible. | ||
* This is an alternative to wrapping {@linkplain HttpServerResponse} or declaring a custom type. We | ||
* don't wrap {@linkplain HttpServerResponse}, because this would lock the instrumentation to the | ||
* signatures currently present on it (for example, if a method is added, we'd have to recompile). | ||
* If a wrapper is eventually provided by vertx, we could use that, but it didn't exist at the time. | ||
* We could also define a custom composite type like ResponseWithTemplate. However, this would | ||
* interfere with people using "instanceof" in http samplers or parsers: they'd have to depend on a | ||
* brave type. The least impact means was to use a thread local, as eventhough this costs a little, | ||
* it prevents revision lock or routine use of custom types. | ||
*/ | ||
class VertxHttpServerAdapter extends HttpServerAdapter<HttpServerRequest, HttpServerResponse> { | ||
@Override public String method(HttpServerRequest request) { | ||
return request.rawMethod(); | ||
} | ||
@Override public String path(HttpServerRequest request) { | ||
return request.path(); | ||
} | ||
@Override public String url(HttpServerRequest request) { | ||
return request.absoluteURI(); | ||
} | ||
@Override public String requestHeader(HttpServerRequest request, String name) { | ||
return request.headers().get(name); | ||
} | ||
@Override public String methodFromResponse(HttpServerResponse ignored) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
return methodAndPath != null ? methodAndPath[0] : null; | ||
} | ||
@Override public String route(HttpServerResponse ignored) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
String result = methodAndPath != null ? methodAndPath[1] : null; | ||
return result != null ? result : ""; | ||
} | ||
@Override @Nullable public Integer statusCode(HttpServerResponse response) { | ||
int result = statusCodeAsInt(response); | ||
return result != 0 ? result : null; | ||
} | ||
@Override public int statusCodeAsInt(HttpServerResponse response) { | ||
return response.getStatusCode(); | ||
} | ||
/** | ||
* This sets the client IP:port to the {@linkplain HttpServerRequest#remoteAddress() remote | ||
* address} if the {@link HttpServerAdapter#parseClientIpAndPort default parsing} fails. | ||
*/ | ||
@Override public boolean parseClientIpAndPort(HttpServerRequest req, Span span) { | ||
if (parseClientIpFromXForwardedFor(req, span)) return true; | ||
SocketAddress addr = req.remoteAddress(); | ||
return span.remoteIpAndPort(addr.host(), addr.port()); | ||
} | ||
/** | ||
* This uses a string array to avoid leaking a Brave type onto a thread local. If we used a Brave | ||
* type, it would prevent unloading Brave classes. | ||
*/ | ||
static final ThreadLocal<String[]> METHOD_AND_PATH = new ThreadLocal<>(); | ||
static void setCurrentMethodAndPath(String method, String path) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
if (methodAndPath == null) { | ||
methodAndPath = new String[2]; | ||
METHOD_AND_PATH.set(methodAndPath); | ||
} | ||
methodAndPath[0] = method; | ||
methodAndPath[1] = path; | ||
} | ||
} | ||
/* | ||
* Copyright 2013-2019 The OpenZipkin Authors | ||
* | ||
* Licensed 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 com.glencoesoftware.omero.ms.core; | ||
|
||
import brave.Span; | ||
import brave.http.HttpServerAdapter; | ||
import brave.internal.Nullable; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.net.SocketAddress; | ||
|
||
/** | ||
* Ported from <code>brave.vertx.web.VertxHttpServerAdapter</code> at tag | ||
* <code>release-5.6.8</code>. That implementation is package private so | ||
* cannot be used by us unless we bring the entire thing in. | ||
* | ||
* Why use a thread local for the http route when parsing {@linkplain HttpServerResponse}?</h3> | ||
* | ||
* <p>When parsing the response, we use a thread local to make the current route's path visible. | ||
* This is an alternative to wrapping {@linkplain HttpServerResponse} or declaring a custom type. We | ||
* don't wrap {@linkplain HttpServerResponse}, because this would lock the instrumentation to the | ||
* signatures currently present on it (for example, if a method is added, we'd have to recompile). | ||
* If a wrapper is eventually provided by vertx, we could use that, but it didn't exist at the time. | ||
* We could also define a custom composite type like ResponseWithTemplate. However, this would | ||
* interfere with people using "instanceof" in http samplers or parsers: they'd have to depend on a | ||
* brave type. The least impact means was to use a thread local, as eventhough this costs a little, | ||
* it prevents revision lock or routine use of custom types. | ||
*/ | ||
class VertxHttpServerAdapter extends HttpServerAdapter<HttpServerRequest, HttpServerResponse> { | ||
|
||
@Override public String method(HttpServerRequest request) { | ||
return request.method().name(); | ||
} | ||
|
||
@Override public String path(HttpServerRequest request) { | ||
return request.path(); | ||
} | ||
|
||
@Override public String url(HttpServerRequest request) { | ||
return request.absoluteURI(); | ||
} | ||
|
||
@Override public String requestHeader(HttpServerRequest request, String name) { | ||
return request.headers().get(name); | ||
} | ||
|
||
@Override public String methodFromResponse(HttpServerResponse ignored) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
return methodAndPath != null ? methodAndPath[0] : null; | ||
} | ||
|
||
@Override public String route(HttpServerResponse ignored) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
String result = methodAndPath != null ? methodAndPath[1] : null; | ||
return result != null ? result : ""; | ||
} | ||
|
||
@Override @Nullable public Integer statusCode(HttpServerResponse response) { | ||
int result = statusCodeAsInt(response); | ||
return result != 0 ? result : null; | ||
} | ||
|
||
@Override public int statusCodeAsInt(HttpServerResponse response) { | ||
return response.getStatusCode(); | ||
} | ||
|
||
/** | ||
* This sets the client IP:port to the {@linkplain HttpServerRequest#remoteAddress() remote | ||
* address} if the {@link HttpServerAdapter#parseClientIpAndPort default parsing} fails. | ||
*/ | ||
@Override public boolean parseClientIpAndPort(HttpServerRequest req, Span span) { | ||
if (parseClientIpFromXForwardedFor(req, span)) return true; | ||
SocketAddress addr = req.remoteAddress(); | ||
return span.remoteIpAndPort(addr.host(), addr.port()); | ||
} | ||
|
||
/** | ||
* This uses a string array to avoid leaking a Brave type onto a thread local. If we used a Brave | ||
* type, it would prevent unloading Brave classes. | ||
*/ | ||
static final ThreadLocal<String[]> METHOD_AND_PATH = new ThreadLocal<>(); | ||
|
||
static void setCurrentMethodAndPath(String method, String path) { | ||
String[] methodAndPath = METHOD_AND_PATH.get(); | ||
if (methodAndPath == null) { | ||
methodAndPath = new String[2]; | ||
METHOD_AND_PATH.set(methodAndPath); | ||
} | ||
methodAndPath[0] = method; | ||
methodAndPath[1] = path; | ||
} | ||
} |
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.
Was this change for testing locally?
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, but also I imagine we'll want to increment the minor version number for a change like this.
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, totally up for incrementing the minor version number for this. Could we make it
0.10.0-SNAPSHOT
for now so that we can aim to merge this without necessarily calling it a full release?Looking into #26 as it might be timely to move these API methods upstream.