Skip to content
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

ConfigService gRPC and JS API #2979

Merged
merged 13 commits into from
Oct 31, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.deephaven.extensions.barrage;

import io.deephaven.barrage.flatbuf.BarrageMessageWrapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Properties;

/**
* Resolves the runtime version of Barrage, in case it has been updated on the classpath beyond what this project was
* built with. Should be invoked before it will first be requested to ensure it is set properly.
*/
public class VersionLookup {
public static void lookup() {
String version;

try (InputStream is = BarrageMessageWrapper.class
.getResourceAsStream("/META-INF/maven/io.deephaven.barrage/barrage-format/pom.properties")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Barrage provide Implementation-Version? If not, should we add it there instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, looks like it does, and this way is not used right now? barrage=io.deephaven.barrage.flatbuf.BarrageMessageWrapper ? I strongly lean towards getImplementationVersion().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I just wanted more than one option to discuss, see description. I think this is the better option, it is slightly more generally adopted, but you still can't assume it is present.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a way we can have gradle output a "manifest" file of all the GAV coordinates it actually uses at runtime (in the context of jetty-server-app, etc).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also discussed in slack)
Certainly possible, not even difficult - but it does but a burden on downstream consumers of the io.deephaven:server-jetty artifact to produce their own such artifact if they happen to manage any dependency differently that DHC itself specifies it. For example, if a bugfix release of barrage were to emerge that io.dh:server-jetty is compatible with, a project could specify that newer one in maven/ivy/sbt/gradle, but the version file wouldn't update unless we had scripts for their given build system.

In contrast, while the Package.getImplementationVersion() is not uniformly applied, it is part of Java itself, so a reasonable deployment should include it. In the event that a dependency doesn't include it but a project needs it, a "dummy" jar could be added to the project that exposes the dependency version, or set a regular configuration property and expose it through this service.

Properties properties = new Properties();
properties.load(is);
version = properties.getProperty("version");
} catch (IOException e) {
throw new UncheckedIOException("Failed to load barrage version, is it on the classpath?", e);
}
System.setProperty("barrage.version", version);
}
}
13 changes: 13 additions & 0 deletions props/configs/src/main/resources/dh-defaults.prop
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ default.processEnvironmentFactory=io.deephaven.util.process.DefaultProcessEnviro

OperationInitializationThreadPool.threads=1


AuthHandlers=io.deephaven.auth.AnonymousAuthenticationHandler

# List of configuration properties to provide to unauthenticated clients, so that they can decide how best to prove their
# identity to the server.
authentication.client.configuration.list=AuthHandlers

# List of configuration properties to provide to authenticated clients, so they can interact with the server.
client.configuration.list=java.version,deephaven.version,barrage.version,
# Version list to add to the configuration property list. Each `=`-delimited pair denotes a short name for a versioned
# jar, and a class that is found in that jar. Any such keys will be made available to the client.configuration.list
# as <key>.version.
client.version.list=deephaven=io.deephaven.engine.table.Table,barrage=io.deephaven.barrage.flatbuf.BarrageMessageWrapper
4 changes: 4 additions & 0 deletions proto/proto-backplane-grpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RUN set -eux; \
/includes/deephaven/proto/application.proto \
/includes/deephaven/proto/inputtable.proto \
/includes/deephaven/proto/partitionedtable.proto \
/includes/deephaven/proto/config.proto \
/includes/deephaven/proto/storage.proto; \
/opt/protoc/bin/protoc \
--plugin=protoc-gen-ts=/usr/src/app/node_modules/.bin/protoc-gen-ts \
Expand All @@ -42,6 +43,7 @@ RUN set -eux; \
/includes/deephaven/proto/application.proto \
/includes/deephaven/proto/inputtable.proto \
/includes/deephaven/proto/partitionedtable.proto \
/includes/deephaven/proto/config.proto \
/includes/deephaven/proto/storage.proto; \
python3 -m grpc_tools.protoc \
--grpc_python_out=/generated/python \
Expand All @@ -55,6 +57,7 @@ RUN set -eux; \
/includes/deephaven/proto/application.proto \
/includes/deephaven/proto/inputtable.proto \
/includes/deephaven/proto/partitionedtable.proto \
/includes/deephaven/proto/config.proto \
/includes/deephaven/proto/storage.proto; \
/opt/protoc/bin/protoc \
--plugin=protoc-gen-go=/opt/protoc-gen-go \
Expand All @@ -72,4 +75,5 @@ RUN set -eux; \
/includes/deephaven/proto/application.proto \
/includes/deephaven/proto/inputtable.proto \
/includes/deephaven/proto/partitionedtable.proto \
/includes/deephaven/proto/config.proto \
/includes/deephaven/proto/storage.proto;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
*/
syntax = "proto3";

package io.deephaven.proto.backplane.grpc;

option java_multiple_files = true;
option optimize_for = SPEED;
option go_package = "github.com/deephaven/deephaven-core/go/internal/proto/config";

/**
* Provides simple configuration data to users. Unauthenticated users may call GetAuthenticationConstants
* to discover hints on how they should proceed with providing their identity, while already-authenticated
* clients may call GetConfigurationConstants for details on using the platform.
*/
service ConfigService {
rpc GetAuthenticationConstants(AuthenticationConstantsRequest) returns (AuthenticationConstantsResponse) {}
rpc GetConfigurationConstants(ConfigurationConstantsRequest) returns (ConfigurationConstantsResponse) {}
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about the "Constants" part of it - at least based on the current impl, users could manually set and change these values at runtime...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, naming is here to be critiqued. I don't think we want the server to push changes to these configuration values, so from a given client's perspective, we should assume that config values received are functionally constants, but at the same time, there is nothing preventing the server from changing them at runtime without a restart.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On DHE the config values are certainly treated as constants. I'm alright if they change at runtime as long as there's a notification there's been a change, such that UI can re-load appropriately. It's definitely less complex if we just treat them as constants.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do have changes, sending a disconnect also would force the client to reconnect/fetch the server config values again as well. I don't think we're expecting any use cases where config values change on the fly, I'd think it's easier to just do disconnect/reload everything if the user does want to change config values (which shouldn't happen often?)

}

message AuthenticationConstantsRequest {}
message ConfigurationConstantsRequest {}
message AuthenticationConstantsResponse {
repeated ConfigPair config_values = 1;
}
message ConfigurationConstantsResponse {
repeated ConfigPair config_values = 1;
}
message ConfigPair {
string key = 1;
oneof value {//leave room for more types
string string_value = 2;
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I do want to talk about pros and cons of our choices here wrt https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/struct.proto. Is there benefit to relying on Struct/Value instead? Or, we could potentially mirror it with our own type and leave out the parts we don't need right now:

message Value {
  // The kind of value.
  oneof kind {
    // Represents a string value.
    string string_value = 3;
  }
}

This might leave us room in the future if we did want to transition to upstream types.
Thoughts on repeated vs map<string, ? We get a bit stronger of typing w/ a map.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, in theory, will make the change.

I'm not sure what you mean by stronger typing though, the key is still a string the value is still whatever fits in the one-of/etc, which is still checked by tag. Indeed, the map<k,v> syntax is just sugar over a message with a 1 and 2 tag of the specified type, so key collisions can happen (though presumably the receiving side's runtime will prune the duplicates, like it would for non-repeated fields). Note especially the warning in the docs at https://developers.google.com/protocol-buffers/docs/proto3#backwards_compatibility:

Any protocol buffers implementation that supports maps must both produce and accept data that can be accepted by the above definition.

Functionally, the only difference here is that the oneof is moved out to its own message type, so we just pay for an extra tag "go decode a value now" "here's the type of the data" instead of just "here's the type of the data".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great link. I didn't realize the semantics of map were equivalent to repeated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, PTAL.

4 changes: 4 additions & 0 deletions proto/raw-js-openapi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require("deephaven/proto/inputtable_pb");
require("deephaven/proto/object_pb");
require("deephaven/proto/partitionedtable_pb");
require("deephaven/proto/storage_pb");
require("deephaven/proto/config_pb");
require("Flight_pb")
require("BrowserFlight_pb")
var sessionService = require("deephaven/proto/session_pb_service");
Expand All @@ -17,6 +18,7 @@ var inputTableService = require("deephaven/proto/inputtable_pb_service");
var objectService = require("deephaven/proto/object_pb_service");
var partitionedTableService = require("deephaven/proto/partitionedtable_pb_service");
var storageService = require("deephaven/proto/storage_pb_service");
var configService = require("deephaven/proto/config_pb_service");
var browserFlightService = require("BrowserFlight_pb_service");
var flightService = require("Flight_pb_service");

Expand Down Expand Up @@ -49,6 +51,8 @@ var io = { deephaven: {
partitionedtable_pb_service: partitionedTableService,
storage_pb: proto.io.deephaven.proto.backplane.grpc,
storage_pb_service: storageService,
config_pb: proto.io.deephaven.proto.backplane.grpc,
config_pb_service: configService,
},
barrage: {
"flatbuf": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
@Module()
public class AuthContextModule {
private static final Logger log = LoggerFactory.getLogger(BarrageStreamGenerator.class);
private static final String[] AUTH_HANDLERS = Configuration.getInstance()
.getStringArrayFromPropertyWithDefault("AuthHandlers",
new String[] {AnonymousAuthenticationHandler.class.getCanonicalName()});
private static final String[] AUTH_HANDLERS =
Configuration.getInstance().getStringArrayFromProperty("AuthHandlers");

private static BasicAuthMarshaller basicAuthMarshaller;
private static Map<String, AuthenticationRequestHandler> authHandlerMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package io.deephaven.server.config;

import io.deephaven.configuration.Configuration;
import io.deephaven.extensions.barrage.util.GrpcUtil;
import io.deephaven.internal.log.LoggerFactory;
import io.deephaven.io.logger.Logger;
import io.deephaven.proto.backplane.grpc.AuthenticationConstantsRequest;
import io.deephaven.proto.backplane.grpc.AuthenticationConstantsResponse;
import io.deephaven.proto.backplane.grpc.ConfigPair;
import io.deephaven.proto.backplane.grpc.ConfigServiceGrpc;
import io.deephaven.proto.backplane.grpc.ConfigurationConstantsRequest;
import io.deephaven.proto.backplane.grpc.ConfigurationConstantsResponse;
import io.grpc.stub.StreamObserver;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;

/**
* Serves specified configuration properties to gRPC clients.
*/
public class ConfigServiceGrpcImpl extends ConfigServiceGrpc.ConfigServiceImplBase {
private static final Logger log = LoggerFactory.getLogger(ConfigServiceGrpcImpl.class);
private static final String VERSION_LIST_PROPERTY = "client.version.list";

private static final String AUTH_CLIENT_CONFIG_PROPERTY = "authentication.client.configuration.list";

// TODO consider a mechanism for roles for these
private static final String CLIENT_CONFIG_PROPERTY = "client.configuration.list";

private final Configuration configuration = Configuration.getInstance();

@Inject
public ConfigServiceGrpcImpl() {
// On startup, lookup the versions to make available.
for (String pair : configuration.getStringArrayFromProperty(VERSION_LIST_PROPERTY)) {
pair = pair.trim();
if (pair.isEmpty()) {
continue;
}
String[] split = pair.split("=");
if (split.length != 2) {
throw new IllegalArgumentException("Missing '=' in " + VERSION_LIST_PROPERTY);
}
String key = split[0] + ".version";
if (configuration.hasProperty(key)) {
throw new IllegalArgumentException("Configuration already has a key for '" + key + "'");
}
String className = split[1];
try {
configuration.setProperty(key, Class.forName(className, false, getClass().getClassLoader()).getPackage()
.getImplementationVersion());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very neat, I didn't know this existed.

} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Failed to find class to get its version '" + className + "'");
}
}
}

@Override
public void getAuthenticationConstants(AuthenticationConstantsRequest request,
StreamObserver<AuthenticationConstantsResponse> responseObserver) {
GrpcUtil.rpcWrapper(log, responseObserver, () -> {
AuthenticationConstantsResponse.Builder builder = AuthenticationConstantsResponse.newBuilder();
collectConfigs(builder::addConfigValues, AUTH_CLIENT_CONFIG_PROPERTY);
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
});
}

@Override
public void getConfigurationConstants(ConfigurationConstantsRequest request,
StreamObserver<ConfigurationConstantsResponse> responseObserver) {
GrpcUtil.rpcWrapper(log, responseObserver, () -> {
ConfigurationConstantsResponse.Builder builder = ConfigurationConstantsResponse.newBuilder();
collectConfigs(builder::addConfigValues, CLIENT_CONFIG_PROPERTY);
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
});
}

private void collectConfigs(Consumer<ConfigPair> addConfigValues, String clientConfigProperty) {
Arrays.stream(configuration.getStringWithDefault(clientConfigProperty, "").split(","))
.map(String::trim)
.filter(key -> !key.isEmpty())
.map(key -> {
String value = configuration.getStringWithDefault(key, null);
if (value == null) {
return null;
}
return ConfigPair.newBuilder()
.setKey(key)
.setStringValue(value)
.build();
})
.filter(Objects::nonNull)
.forEach(addConfigValues);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.deephaven.server.config;

import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoSet;
import io.grpc.BindableService;

@Module
public interface ConfigServiceModule {
@Binds
@IntoSet
BindableService bindConfigServiceGrpcImpl(ConfigServiceGrpcImpl instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.deephaven.configuration.Configuration;
import io.deephaven.engine.updategraph.UpdateGraphProcessor;
import io.deephaven.engine.util.ScriptSession;
import io.deephaven.server.config.ConfigServiceModule;
import io.deephaven.server.notebook.FilesystemStorageServiceModule;
import io.deephaven.server.healthcheck.HealthCheckModule;
import io.deephaven.server.object.ObjectServiceModule;
Expand Down Expand Up @@ -61,6 +62,7 @@
PartitionedTableServiceModule.class,
FilesystemStorageServiceModule.class,
HealthCheckModule.class,
ConfigServiceModule.class,
})
public class DeephavenApiServerModule {

Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/io/deephaven/server/runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.deephaven.base.system.PrintStreamGlobals;
import io.deephaven.configuration.Configuration;
import io.deephaven.extensions.barrage.VersionLookup;
import io.deephaven.internal.log.LoggerFactory;
import io.deephaven.io.logger.LogBufferGlobal;
import io.deephaven.io.logger.LogBufferInterceptor;
Expand Down Expand Up @@ -78,6 +79,9 @@ public static Configuration init(String[] args, Class<?> mainClass) throws IOExc

log.info().append("Starting up ").append(mainClass.getName()).append("...").endl();

// Version properties to bootstrap before we load configuration for the first time
// VersionLookup.lookup();
// System.setProperty("deephaven.version", mainClass.getPackage().getImplementationVersion());
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
final Configuration config = Configuration.getInstance();

// After logging and config are working, redirect any future JUL logging to SLF4J
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package io.deephaven.web.client.api;

import elemental2.core.JsArray;
import elemental2.promise.Promise;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.AuthenticationConstantsRequest;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.AuthenticationConstantsResponse;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.ConfigPair;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.ConfigurationConstantsRequest;
import io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.config_pb.ConfigurationConstantsResponse;
import io.deephaven.web.client.api.storage.JsStorageService;
import io.deephaven.web.shared.fu.JsBiConsumer;
import io.deephaven.web.shared.fu.JsFunction;
import jsinterop.annotations.JsType;

import java.util.function.Consumer;

@JsType(namespace = "dh")
public class CoreClient extends QueryConnectable<CoreClient> {
public static final String EVENT_CONNECT = "connect",
Expand All @@ -26,6 +36,18 @@ public CoreClient(String serverUrl) {
this.serverUrl = serverUrl;
}

private <R> Promise<String[][]> getConfigs(Consumer<JsBiConsumer<Object, R>> rpcCall,
JsFunction<R, JsArray<ConfigPair>> getConfigValues) {
return Callbacks.grpcUnaryPromise(rpcCall).then(response -> {
String[][] result = new String[0][];
getConfigValues.apply(response).forEach((item, index, array) -> {
result[index] = new String[] {item.getKey(), item.getStringValue()};
return null;
});
return Promise.resolve(result);
});
}

@Override
public Promise<CoreClient> running() {
// This assumes that once the connection has been initialized and left a usable state, it cannot be used again
Expand All @@ -42,7 +64,12 @@ public String getServerUrl() {
}

public Promise<String[][]> getAuthConfigValues() {
return Promise.resolve(new String[0][]);
return getConfigs(
c -> connection.get().configServiceClient().getAuthenticationConstants(
new AuthenticationConstantsRequest(),
connection.get().metadata(),
c::apply),
AuthenticationConstantsResponse::getConfigValuesList);
}

public Promise<Void> login(LoginCredentials credentials) {
Expand All @@ -54,7 +81,12 @@ public Promise<Void> relogin(String token) {
}

public Promise<String[][]> getServerConfigValues() {
return Promise.resolve(new String[0][]);
return getConfigs(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure - these are general server information, and we don't expect the user to be authenticated before retrieving them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch - the server will have to auth this and fail the user in this case. i'll double check what dhe does here, but I think this is the same?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, on DHE it checks if you're authenticated for server config values: https://gitlab.deephaven.io/illumon/iris/-/blob/rc/silverheels/web/server-frontend/src/main/java/com/illumon/iris/web/server/WebApiServerImpl.java#L844
You can get the authConfigValues before being authenticated though (as you need them to know what you can login with): https://gitlab.deephaven.io/illumon/iris/-/blob/rc/silverheels/web/server-frontend/src/main/java/com/illumon/iris/web/server/WebApiServerImpl.java#L384
Also looks like there's a minor bug in DHE - if you try client.getServerConfigValues() before logged in, you'll get a Not logged in error (which makes sense), however if you then login with that same client object and call client.getServerConfigValues() afterward, you still get a Not logged in error. This is because the serverConfigValues is just a lazy that initializes the first time the get is called, and does not reset even if there was an error (or you login): https://gitlab.deephaven.io/illumon/iris/-/blob/rc/silverheels/web/client-api/src/main/java/com/illumon/iris/web/client/api/IrisClient.java#L80
Such a minor issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaving this as is, re-querying the server each time, to avoid the bug you describe. The tradeoff is that if the client asks for the config again, they will get an extra round trip, and in theory could get different values, going by the current implementation. That might be a feature too, letting the client poll for values.

c -> connection.get().configServiceClient().getConfigurationConstants(
new ConfigurationConstantsRequest(),
connection.get().metadata(),
c::apply),
ConfigurationConstantsResponse::getConfigValuesList);
}

public Promise<UserInfo> getUserInfo() {
Expand Down
Loading