Skip to content

Commit

Permalink
3.11 compatibility improvements (#257)
Browse files Browse the repository at this point in the history
* Improve compatibility with 3.11, by using deprecated Netty methods..

* Downgrade jackson-dataformat to 2.13.2

* Do not start /metrics endpoint if Cassandra version is older than 3.11.13

* Compare version with compateTo instead of isSupportedBy
  • Loading branch information
burmanm authored Feb 2, 2023
1 parent f85032f commit 8b3fd9e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject httpObject) th
if (httpObject instanceof HttpRequest) {
HttpRequest req = (HttpRequest) httpObject;

URI uri = new URI(req.uri());
URI uri = new URI(req.getUri());
if (!uri.getPath().equals("/metrics")) {
// Send 404?
FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
Expand All @@ -51,21 +51,21 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject httpObject) th

private boolean writeResponse(HttpRequest request, ChannelHandlerContext ctx, String contentType) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpUtil.isKeepAlive(request);
boolean keepAlive = HttpHeaders.isKeepAlive(request); // Keep HttpHeaders instead of HttpUtil to get Netty 4.0.x compatibility
// Build the response object.
// TODO We should probably use something more performant.. copyBuffer for single thread (blocking) seems weird
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, request.decoderResult().isSuccess() ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST,
HttpVersion.HTTP_1_1, request.getDecoderResult().isSuccess() ? HttpResponseStatus.OK : HttpResponseStatus.BAD_REQUEST,
Unpooled.copiedBuffer(writer.toString(), CharsetUtil.UTF_8));

response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);

if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}

// Write the response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.cassandra.metrics.CassandraMetricsRegistry;
import org.apache.cassandra.utils.CassandraVersion;
import org.apache.cassandra.utils.FBUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,6 +25,8 @@ public class MetricsInterceptor
{
private static final Logger logger = LoggerFactory.getLogger(MetricsInterceptor.class);

private static final CassandraVersion MIN_CASSANDRA_VERSION = new CassandraVersion("3.11.13");

public static ElementMatcher<? super TypeDescription> type()
{
return ElementMatchers.nameEndsWith(".CassandraDaemon");
Expand All @@ -34,6 +38,12 @@ public static Transformer transformer()
}

public static void intercept(@SuperCall Callable<Void> zuper) throws Exception {
CassandraVersion cassandraVersion = new CassandraVersion(FBUtilities.getReleaseVersionString());
if(MIN_CASSANDRA_VERSION.compareTo(cassandraVersion) > 0) {
logger.error("/metrics endpoint is not supported in versions older than {}", MIN_CASSANDRA_VERSION);
return;
}

logger.info("Starting Metric Collector for Apache Cassandra");

// Read Configuration file
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<mockito.version>3.5.13</mockito.version>
<prometheus.version>0.16.0</prometheus.version>
<dropwizard-metrics.version>3.1.5</dropwizard-metrics.version> <!-- This old version is used by Cassandra 4.x -->
<jackson-dataformat.version>2.14.0</jackson-dataformat.version>
<jackson-dataformat.version>2.13.2</jackson-dataformat.version>
</properties>

<dependencies>
Expand Down

0 comments on commit 8b3fd9e

Please sign in to comment.