dlmarion commented on code in PR #5018: URL: https://github.com/apache/accumulo/pull/5018#discussion_r1819514845
########## server/base/src/main/java/org/apache/accumulo/server/rest/ThriftDeserializer.java: ########## @@ -0,0 +1,83 @@ +/* + * 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 + * + * https://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.accumulo.server.rest; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.accumulo.server.rest.ThriftSerializer.ENCODED; +import static org.apache.accumulo.server.rest.ThriftSerializer.TYPE; + +import java.io.IOException; +import java.lang.reflect.Constructor; + +import org.apache.thrift.TBase; +import org.apache.thrift.TDeserializer; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TJSONProtocol; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; + +/** + * Jackson deserializer for thrift objects that delegates the deserialization of thrift to + * {@link TDeserializer}. It handles previously encoded serialized objects from + * {@link ThriftSerializer} + */ +public class ThriftDeserializer<T extends TBase<?,?>> extends JsonDeserializer<T> { + @Override + public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + JsonNode tree = p.readValueAsTree(); + + try { + var thriftClassName = tree.get(TYPE).asText(); + var encoded = tree.get(ENCODED).asText(); + + Constructor<T> constructor = getThriftClass(thriftClassName).getDeclaredConstructor(); + T obj = constructor.newInstance(); + deserialize(obj, encoded); + + return obj; + } catch (ReflectiveOperationException e) { + throw new IOException(e); + } + } + + @SuppressWarnings("unchecked") + private Class<T> getThriftClass(String className) throws ClassNotFoundException { + var clazz = Class.forName(className, false, ThriftDeserializer.class.getClassLoader()); + // Note: This check is important to prevent potential security issues + // We don't want to allow arbitrary classes to be loaded + if (!TBase.class.isAssignableFrom(clazz)) { + throw new IllegalArgumentException("Class " + clazz + " is not assignable to TBase"); + } + return (Class<T>) clazz; + } + + // TODO: It doesn't seem like TDeserializer is thread safe, is there a way + // to prevent creating a new deserializer for every object? + private static <T extends TBase<?,?>> void deserialize(T obj, String json) throws IOException { + try { + final TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory()); Review Comment: The Factory is thread-safe, right? I can imagine that the Deserializer is not. You could put the TDeserializer and TSerializers in a ThreadLocal. It looks like they reset their internals for reuse. ########## server/manager/src/main/java/org/apache/accumulo/manager/Manager.java: ########## @@ -1134,6 +1153,30 @@ public void run() { clientService = sa.server; log.info("Started Manager client service at {}", sa.address); + int restPort = 8999; + try { + restClientService = new EmbeddedRpcWebServer(this, restPort); + restClientService.addServlet(getRestServlet(), "/rest/*"); + restClientService.start(); + + if (!restClientService.isRunning()) { + throw new RuntimeException("Unable to start rpc http server on port: " + restPort); + } + } catch (Exception e) { + throw new IllegalStateException("Unable to start embedded web server " + getHostname(), e); + } + + String advertiseHost = getHostname(); + if (advertiseHost.equals("0.0.0.0")) { + try { + advertiseHost = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + log.error("Unable to get hostname", e); + } + } + HostAndPort restHostAndPort = HostAndPort.fromParts(advertiseHost, restPort); Review Comment: I think you can use the value of `sa.address` from the code above this. In the code above the Thrift server was started on an interface, I assume we want to use the same one, just a different port. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
