-
Notifications
You must be signed in to change notification settings - Fork 3.4k
HBASE-25895 Implement a Cluster Metrics JSON endpoint #4177
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
Merged
ndimiduk
merged 1 commit into
apache:master
from
ndimiduk:25895-cluster-metrics-json-endpoint
Mar 10, 2022
Merged
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions
38
hbase-http/src/main/java/org/apache/hadoop/hbase/http/gson/ByteArraySerializer.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.hadoop.hbase.http.gson; | ||
|
||
import java.lang.reflect.Type; | ||
import org.apache.hadoop.hbase.util.Bytes; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.apache.hbase.thirdparty.com.google.gson.JsonElement; | ||
import org.apache.hbase.thirdparty.com.google.gson.JsonPrimitive; | ||
import org.apache.hbase.thirdparty.com.google.gson.JsonSerializationContext; | ||
import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer; | ||
|
||
/** | ||
* Serialize a {@code byte[]} using {@link Bytes#toString()}. | ||
*/ | ||
@InterfaceAudience.Private | ||
public final class ByteArraySerializer implements JsonSerializer<byte[]> { | ||
|
||
@Override | ||
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(Bytes.toString(src)); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
hbase-http/src/main/java/org/apache/hadoop/hbase/http/gson/GsonMessageBodyWriter.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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.hadoop.hbase.http.gson; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.IllegalCharsetNameException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.charset.UnsupportedCharsetException; | ||
import java.util.Optional; | ||
import javax.inject.Inject; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.apache.hbase.thirdparty.com.google.gson.Gson; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.Produces; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.WebApplicationException; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.core.MultivaluedMap; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.ext.MessageBodyWriter; | ||
|
||
/** | ||
* Implements JSON serialization via {@link Gson} for JAX-RS. | ||
*/ | ||
@InterfaceAudience.Private | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public final class GsonMessageBodyWriter<T> implements MessageBodyWriter<T> { | ||
private static final Logger logger = LoggerFactory.getLogger(GsonMessageBodyWriter.class); | ||
|
||
private final Gson gson; | ||
|
||
@Inject | ||
public GsonMessageBodyWriter(Gson gson) { | ||
this.gson = gson; | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType) { | ||
return mediaType == null || MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType); | ||
} | ||
|
||
@Override | ||
public void writeTo( | ||
T t, | ||
Class<?> type, | ||
Type genericType, | ||
Annotation[] annotations, | ||
MediaType mediaType, | ||
MultivaluedMap<String, Object> httpHeaders, | ||
OutputStream entityStream | ||
) throws IOException, WebApplicationException { | ||
final Charset outputCharset = requestedCharset(mediaType); | ||
try (Writer writer = new OutputStreamWriter(entityStream, outputCharset)) { | ||
gson.toJson(t, writer); | ||
} | ||
} | ||
|
||
private static Charset requestedCharset(MediaType mediaType) { | ||
return Optional.ofNullable(mediaType) | ||
.map(MediaType::getParameters) | ||
.map(params -> params.get("charset")) | ||
.map(c -> { | ||
try { | ||
return Charset.forName(c); | ||
} catch (IllegalCharsetNameException e) { | ||
logger.debug("Client requested illegal Charset '{}'", c); | ||
return null; | ||
} catch (UnsupportedCharsetException e) { | ||
logger.debug("Client requested unsupported Charset '{}'", c); | ||
return null; | ||
} catch (Exception e) { | ||
logger.debug("Error while resolving Charset '{}'", c, e); | ||
return null; | ||
} | ||
}) | ||
.orElse(StandardCharsets.UTF_8); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
hbase-http/src/main/java/org/apache/hadoop/hbase/http/jersey/ResponseEntityMapper.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.hadoop.hbase.http.jersey; | ||
|
||
import java.io.IOException; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.container.ContainerRequestContext; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.container.ContainerResponseContext; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.container.ContainerResponseFilter; | ||
import org.apache.hbase.thirdparty.javax.ws.rs.core.Response.Status; | ||
|
||
/** | ||
* Generate a uniform response wrapper around the Entity returned from the resource. | ||
* @see <a href="https://jsonapi.org/format/#document-top-level">JSON API Document Structure</a> | ||
* @see <a href="https://jsonapi.org/format/#error-objects">JSON API Error Objects</a> | ||
*/ | ||
@InterfaceAudience.Private | ||
public class ResponseEntityMapper implements ContainerResponseFilter { | ||
|
||
@Override | ||
public void filter( | ||
ContainerRequestContext requestContext, | ||
ContainerResponseContext responseContext | ||
) throws IOException { | ||
/* | ||
* Follows very loosely the top-level document specification described in by JSON API. Only | ||
* handles 200 response codes; leaves room for errors and other response types. | ||
*/ | ||
|
||
final int statusCode = responseContext.getStatus(); | ||
if (Status.OK.getStatusCode() != statusCode) { | ||
return; | ||
} | ||
|
||
responseContext.setEntity(ImmutableMap.of("data", responseContext.getEntity())); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
hbase-http/src/main/java/org/apache/hadoop/hbase/http/jersey/SupplierFactoryAdapter.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.hadoop.hbase.http.jersey; | ||
|
||
import java.util.function.Supplier; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.apache.hbase.thirdparty.org.glassfish.hk2.api.Factory; | ||
|
||
/** | ||
* Use a {@link Supplier} of type {@code T} as a {@link Factory} that provides instances of | ||
* {@code T}. Modeled after Jersey's internal implementation. | ||
*/ | ||
@InterfaceAudience.Private | ||
public class SupplierFactoryAdapter<T> implements Factory<T> { | ||
|
||
private final Supplier<T> supplier; | ||
|
||
public SupplierFactoryAdapter(Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
@Override public T provide() { | ||
return supplier.get(); | ||
} | ||
|
||
@Override public void dispose(T instance) { } | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I assume nonprintable characters in the string are url escaped here downstream from this call to the JsonPrimitve constructor.
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.
@apurtell I cannot tell you. Actually, I'm embarrassed to see that this commit doesn't include any tests. I've added some most basic coverage, including a test that shows the behavior of the configured Gson instance produced by the factory. Please advise.
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.
@apurtell
After some research, my understanding is that Gson handles escaping characters according to the JSON RFC's definition of String values. We are free, according to the RFC, to encode more aggressively than what Gson does, but it appears to implement the minimum plus some additional characters related to html. My test demonstrates that there are some binary values that are not encoded and that do not render well (at least on this machine).
We may chose to escape a wider range of code points, or we my consider some alternative encoding for binary values, such as base64 with perhaps some prefix.
I have made every effort to declare this API as IA.Private, so we are free to evolve it as we choose, so long as we can maintain a sufficient level of backward compatibility for our guarantees. Thus I would prefer to press forward with landing this initial implementation of the feature and continue this encoding discussion as a follow-on effort.
Please advise.
https://datatracker.ietf.org/doc/html/rfc4627#section-2.5
https://github.com/google/gson/blob/gson-parent-2.8.9/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L133-L163
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.
Thanks @ndimiduk . I was mainly worried that we might break parsing.