Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.glencoesoftware.omero'
version = '0.9.2-SNAPSHOT'
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

version = '0.10.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down Expand Up @@ -62,8 +62,8 @@ dependencies {
implementation 'io.zipkin.brave:brave-instrumentation-http:5.6.8'
implementation 'io.lettuce:lettuce-core:5.2.0.RELEASE'
implementation 'org.openmicroscopy:omero-blitz:5.8.0'
implementation 'io.vertx:vertx-web:3.8.1'
implementation 'io.vertx:vertx-jdbc-client:3.8.1'
implementation 'io.vertx:vertx-web:4.5.14'
implementation 'io.vertx:vertx-jdbc-client:4.5.14'
implementation 'io.kaitai:kaitai-struct-runtime:0.8'
implementation 'commons-lang:commons-lang:2.6'
implementation 'com.zeroc:icegrid:3.6.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void handle(Void event) {
return;
}
VertxHttpServerAdapter.setCurrentMethodAndPath(
context.request().rawMethod(),
context.request().method().name(),
context.currentRoute().getPath()
);
try {
Expand Down
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);
}

}
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;
}
}