Skip to content

Commit 66cede6

Browse files
authored
Added SessionFactoryCreator and related helpers (deephaven#5386)
This is the java client related changes to support deephaven#5374. Note, that we may need to prioritize deephaven/web-client-ui#1947 as follow-up.
1 parent 8becc41 commit 66cede6

File tree

48 files changed

+1035
-240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1035
-240
lines changed

Util/immutables/src/main/java/io/deephaven/annotations/BuildableStyle.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package io.deephaven.annotations;
55

66
import org.immutables.value.Value;
7+
import org.immutables.value.Value.Immutable;
78
import org.immutables.value.Value.Style.ImplementationVisibility;
89

910
import java.lang.annotation.ElementType;
@@ -12,8 +13,8 @@
1213
import java.lang.annotation.Target;
1314

1415
/**
15-
* A style for objects that should declare a builder interface to use for construction. Recommended for objects with
16-
* more than two fields, or default fields.
16+
* A style for objects that should declare a builder interface to use for construction. Disables
17+
* {@link Immutable#copy()}. Recommended for objects with more than two fields, or default fields.
1718
*/
1819
@Target({ElementType.TYPE, ElementType.PACKAGE})
1920
@Retention(RetentionPolicy.CLASS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
3+
//
4+
package io.deephaven.annotations;
5+
6+
import org.immutables.value.Value;
7+
import org.immutables.value.Value.Style.ImplementationVisibility;
8+
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
/**
15+
* A style for objects that should declare a builder interface to use for construction and allows copy. Recommended for
16+
* objects with more than two fields, or default fields. If copy is not needed, prefer {@link BuildableStyle}.
17+
*/
18+
@Target({ElementType.TYPE, ElementType.PACKAGE})
19+
@Retention(RetentionPolicy.CLASS)
20+
@Value.Style(visibility = ImplementationVisibility.PACKAGE,
21+
defaults = @Value.Immutable(copy = true),
22+
strictBuilder = true,
23+
weakInterning = true,
24+
jdkOnly = true,
25+
includeHashCode = "getClass().hashCode()")
26+
public @interface CopyableStyle {
27+
// Note: this produces ImmutableX.builder()s for the implementation classes
28+
}

extensions/s3/src/main/java/io/deephaven/extensions/s3/S3Instructions.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//
44
package io.deephaven.extensions.s3;
55

6+
import io.deephaven.annotations.CopyableStyle;
67
import io.deephaven.base.log.LogOutput;
78
import io.deephaven.base.log.LogOutputAppendable;
89
import io.deephaven.configuration.Configuration;
9-
import org.immutables.value.Value;
1010
import org.immutables.value.Value.Check;
1111
import org.immutables.value.Value.Default;
1212
import org.immutables.value.Value.Immutable;
@@ -22,12 +22,7 @@
2222
* documented in this class may change in the future. As such, callers may wish to explicitly set the values.
2323
*/
2424
@Immutable
25-
// Almost the same as BuildableStyle, but has copy-ability to support withReadAheadCount
26-
@Value.Style(visibility = Value.Style.ImplementationVisibility.PACKAGE,
27-
defaults = @Value.Immutable(copy = true),
28-
strictBuilder = true,
29-
weakInterning = true,
30-
jdkOnly = true)
25+
@CopyableStyle
3126
public abstract class S3Instructions implements LogOutputAppendable {
3227

3328
private final static int DEFAULT_MAX_CONCURRENT_REQUESTS = 50;

java-client/barrage-dagger/src/main/java/io/deephaven/client/impl/BarrageFactoryBuilderModule.java

-16
This file was deleted.

java-client/barrage-dagger/src/main/java/io/deephaven/client/impl/BarrageSubcomponent.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
@Subcomponent(modules = {SessionImplModule.class, FlightSessionModule.class, BarrageSessionModule.class})
1818
public interface BarrageSubcomponent extends BarrageSessionFactory {
1919

20+
@Override
2021
BarrageSession newBarrageSession();
2122

23+
@Override
24+
ManagedChannel managedChannel();
25+
2226
@Module(subcomponents = {BarrageSubcomponent.class})
2327
interface DeephavenClientSubcomponentModule {
2428

2529
}
2630

2731
@Subcomponent.Builder
28-
interface Builder extends BarrageSessionFactoryBuilder {
32+
interface Builder {
2933
Builder managedChannel(@BindsInstance ManagedChannel channel);
3034

3135
Builder scheduler(@BindsInstance ScheduledExecutorService scheduler);

java-client/barrage-examples/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
dependencies {
7-
implementation project(':java-client-barrage-dagger')
7+
implementation project(':java-client-barrage')
88
implementation project(':java-client-example-utilities')
99

1010
Classpaths.inheritJUnitPlatform(project)

java-client/barrage-examples/src/main/java/io/deephaven/client/examples/BarrageClientExampleBase.java

+24-23
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
package io.deephaven.client.examples;
55

66
import io.deephaven.client.impl.BarrageSession;
7-
import io.deephaven.client.impl.BarrageSessionFactory;
8-
import io.deephaven.client.impl.BarrageSubcomponent.Builder;
9-
import io.deephaven.client.impl.DaggerDeephavenBarrageRoot;
7+
import io.deephaven.client.impl.BarrageSessionFactoryConfig;
8+
import io.deephaven.client.impl.BarrageSessionFactoryConfig.Factory;
9+
import io.deephaven.client.impl.SessionConfig;
1010
import io.deephaven.engine.context.ExecutionContext;
1111
import io.deephaven.engine.updategraph.impl.PeriodicUpdateGraph;
1212
import io.deephaven.util.SafeCloseable;
@@ -34,10 +34,14 @@ abstract class BarrageClientExampleBase implements Callable<Void> {
3434
public final Void call() throws Exception {
3535
final BufferAllocator bufferAllocator = new RootAllocator();
3636
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
37-
ManagedChannel managedChannel = ConnectOptions.open(connectOptions);
38-
37+
final Factory factory = BarrageSessionFactoryConfig.builder()
38+
.clientConfig(ConnectOptions.options(connectOptions).config())
39+
.allocator(bufferAllocator)
40+
.scheduler(scheduler)
41+
.build()
42+
.factory();
3943
Runtime.getRuntime()
40-
.addShutdownHook(new Thread(() -> onShutdown(scheduler, managedChannel)));
44+
.addShutdownHook(new Thread(() -> onShutdown(scheduler, factory.managedChannel())));
4145

4246
// Note that a DEFAULT update graph is required for engine operation. Users may wish to create additional update
4347
// graphs for their own purposes, but the DEFAULT must be created first.
@@ -54,31 +58,28 @@ public final Void call() throws Exception {
5458
.newQueryLibrary()
5559
.setUpdateGraph(updateGraph)
5660
.build();
57-
58-
final Builder builder = DaggerDeephavenBarrageRoot.create().factoryBuilder()
59-
.managedChannel(managedChannel)
60-
.scheduler(scheduler)
61-
.allocator(bufferAllocator);
62-
if (authenticationOptions != null) {
63-
authenticationOptions.ifPresent(builder::authenticationTypeAndValue);
64-
}
65-
final BarrageSessionFactory barrageFactory = builder.build();
66-
final BarrageSession deephavenSession = barrageFactory.newBarrageSession();
67-
try {
68-
try (final SafeCloseable ignored = executionContext.open()) {
61+
try (
62+
final BarrageSession deephavenSession = factory.newBarrageSession(sessionConfig());
63+
final SafeCloseable ignored = executionContext.open()) {
64+
try {
6965
execute(deephavenSession);
7066
} finally {
71-
deephavenSession.close();
67+
deephavenSession.session().closeFuture().get(5, TimeUnit.SECONDS);
7268
}
73-
} finally {
74-
deephavenSession.session().closeFuture().get(5, TimeUnit.SECONDS);
7569
}
76-
7770
scheduler.shutdownNow();
78-
managedChannel.shutdownNow();
71+
factory.managedChannel().shutdownNow();
7972
return null;
8073
}
8174

75+
private SessionConfig sessionConfig() {
76+
final SessionConfig.Builder builder = SessionConfig.builder();
77+
if (authenticationOptions != null) {
78+
authenticationOptions.ifPresent(builder::authenticationTypeAndValue);
79+
}
80+
return builder.build();
81+
}
82+
8283
private static void onShutdown(final ScheduledExecutorService scheduler,
8384
final ManagedChannel managedChannel) {
8485
scheduler.shutdownNow();

java-client/barrage/src/main/java/io/deephaven/client/impl/BarrageSession.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616

1717
public class BarrageSession extends FlightSession implements BarrageSubscription.Factory, BarrageSnapshot.Factory {
1818

19+
/**
20+
* Creates a barrage session. Closing the barrage session does <b>not</b> close {@code channel}.
21+
*
22+
* @param session the session
23+
* @param incomingAllocator the incoming allocator
24+
* @param channel the managed channel
25+
* @return the barrage session
26+
*/
1927
public static BarrageSession of(
2028
SessionImpl session, BufferAllocator incomingAllocator, ManagedChannel channel) {
2129
final FlightClient client = FlightGrpcUtilsExtension.createFlightClientWithSharedChannel(
2230
incomingAllocator, channel, Collections.singletonList(new SessionMiddleware(session)));
23-
return new BarrageSession(session, client, channel);
31+
return new BarrageSession(session, client);
2432
}
2533

2634
protected BarrageSession(
27-
final SessionImpl session, final FlightClient client, final ManagedChannel channel) {
35+
final SessionImpl session, final FlightClient client) {
2836
super(session, client);
2937
}
3038

java-client/barrage/src/main/java/io/deephaven/client/impl/BarrageSessionFactory.java

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
//
44
package io.deephaven.client.impl;
55

6+
import io.grpc.ManagedChannel;
7+
68
public interface BarrageSessionFactory {
9+
10+
/**
11+
* Creates a new {@link BarrageSession}. Closing the session does <b>not</b> close the {@link #managedChannel()}.
12+
*
13+
* @return the new barrage session
14+
*/
715
BarrageSession newBarrageSession();
16+
17+
/**
18+
* The {@link ManagedChannel} associated with {@code this} factory. Use {@link ManagedChannel#shutdown()} when
19+
* {@code this} factory and sessions are no longer needed.
20+
*
21+
* @return the managed channel
22+
*/
23+
ManagedChannel managedChannel();
824
}

java-client/barrage/src/main/java/io/deephaven/client/impl/BarrageSessionFactoryBuilder.java

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
3+
//
4+
package io.deephaven.client.impl;
5+
6+
import io.deephaven.annotations.BuildableStyle;
7+
import io.grpc.ManagedChannel;
8+
import org.apache.arrow.memory.BufferAllocator;
9+
import org.immutables.value.Value.Default;
10+
import org.immutables.value.Value.Immutable;
11+
12+
import java.util.Objects;
13+
import java.util.concurrent.ScheduledExecutorService;
14+
15+
@Immutable
16+
@BuildableStyle
17+
public abstract class BarrageSessionFactoryConfig {
18+
private static final SessionConfig SESSION_CONFIG_EMPTY = SessionConfig.builder().build();
19+
20+
public static Builder builder() {
21+
return ImmutableBarrageSessionFactoryConfig.builder();
22+
}
23+
24+
/**
25+
* The client configuration.
26+
*/
27+
public abstract ClientConfig clientConfig();
28+
29+
/**
30+
* The client channel factory. By default is {@link ClientChannelFactory#defaultInstance()}.
31+
*/
32+
@Default
33+
public ClientChannelFactory clientChannelFactory() {
34+
return ClientChannelFactory.defaultInstance();
35+
}
36+
37+
/**
38+
* The default session config, used by the factory when {@link SessionConfig} is not provided. By default is
39+
* {@code SessionConfig.builder().build()}.
40+
*/
41+
@Default
42+
public SessionConfig sessionConfig() {
43+
return SESSION_CONFIG_EMPTY;
44+
}
45+
46+
/**
47+
* The scheduler, used by the factory when {@link SessionConfig#scheduler()} is not set.
48+
*/
49+
public abstract ScheduledExecutorService scheduler();
50+
51+
/**
52+
* The allocator.
53+
*/
54+
public abstract BufferAllocator allocator();
55+
56+
/**
57+
* Creates a new factory with a new {@link ManagedChannel}.
58+
*
59+
* @return the factory
60+
*/
61+
public final Factory factory() {
62+
return new Factory(SessionFactoryConfig.builder()
63+
.clientConfig(clientConfig())
64+
.clientChannelFactory(clientChannelFactory())
65+
.sessionConfig(sessionConfig())
66+
.scheduler(scheduler())
67+
.build()
68+
.factory());
69+
}
70+
71+
public final class Factory implements BarrageSessionFactory {
72+
private final SessionFactoryConfig.Factory factory;
73+
74+
private Factory(SessionFactoryConfig.Factory factory) {
75+
this.factory = Objects.requireNonNull(factory);
76+
}
77+
78+
@Override
79+
public BarrageSession newBarrageSession() {
80+
final Session session = factory.newSession();
81+
return BarrageSession.of((SessionImpl) session, allocator(), factory.managedChannel());
82+
}
83+
84+
/**
85+
* Creates a new {@link BarrageSession} with {@code sessionConfig}. Closing the session does <b>not</b> close
86+
* the {@link #managedChannel()}.
87+
*
88+
* @param sessionConfig the session config
89+
* @return the new barrage session
90+
*/
91+
public BarrageSession newBarrageSession(SessionConfig sessionConfig) {
92+
final Session session = factory.newSession(sessionConfig);
93+
return BarrageSession.of((SessionImpl) session, allocator(), factory.managedChannel());
94+
}
95+
96+
@Override
97+
public ManagedChannel managedChannel() {
98+
return factory.managedChannel();
99+
}
100+
}
101+
102+
// ------------------------------------------------
103+
104+
public interface Builder {
105+
106+
Builder clientConfig(ClientConfig clientConfig);
107+
108+
Builder clientChannelFactory(ClientChannelFactory clientChannelFactory);
109+
110+
Builder sessionConfig(SessionConfig sessionConfig);
111+
112+
Builder scheduler(ScheduledExecutorService scheduler);
113+
114+
Builder allocator(BufferAllocator allocator);
115+
116+
BarrageSessionFactoryConfig build();
117+
}
118+
}

0 commit comments

Comments
 (0)