This commit is contained in:
Zoltan Haindrich 2024-05-16 13:01:53 +00:00
parent 1fb9fac159
commit 1d2a79f5be
1 changed files with 0 additions and 379 deletions

View File

@ -25,16 +25,11 @@ import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.util.Modules;
import org.apache.calcite.avatica.server.AbstractAvaticaHandler;
import org.apache.druid.cli.GuiceRunnable;
import org.apache.druid.cli.QueryJettyServerInitializer;
import org.apache.druid.client.BrokerSegmentWatcherConfig;
@ -70,7 +65,6 @@ import org.apache.druid.guice.SegmentWranglerModule;
import org.apache.druid.guice.ServerModule;
import org.apache.druid.guice.ServerTypeConfig;
import org.apache.druid.guice.ServerViewModule;
import org.apache.druid.guice.StartupInjectorBuilder;
import org.apache.druid.guice.StartupLoggingModule;
import org.apache.druid.guice.StorageNodeModule;
import org.apache.druid.guice.annotations.Client;
@ -80,12 +74,9 @@ import org.apache.druid.guice.security.AuthenticatorModule;
import org.apache.druid.guice.security.AuthorizerModule;
import org.apache.druid.guice.security.DruidAuthModule;
import org.apache.druid.initialization.CoreInjectorBuilder;
import org.apache.druid.initialization.DruidModule;
import org.apache.druid.initialization.Log4jShutterDownerModule;
import org.apache.druid.initialization.ServerInjectorBuilder;
import org.apache.druid.initialization.TombstoneDataStorageModule;
import org.apache.druid.java.util.common.FileUtils;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.io.Closer;
import org.apache.druid.java.util.common.lifecycle.Lifecycle;
import org.apache.druid.java.util.common.logger.Logger;
@ -100,7 +91,6 @@ import org.apache.druid.segment.join.JoinableFactoryWrapper;
import org.apache.druid.segment.writeout.SegmentWriteOutMediumModule;
import org.apache.druid.server.BrokerQueryResource;
import org.apache.druid.server.ClientInfoResource;
import org.apache.druid.server.DruidNode;
import org.apache.druid.server.QueryLifecycleFactory;
import org.apache.druid.server.QueryScheduler;
import org.apache.druid.server.QuerySchedulerProvider;
@ -118,20 +108,14 @@ import org.apache.druid.server.log.TestRequestLogger;
import org.apache.druid.server.metrics.NoopServiceEmitter;
import org.apache.druid.server.router.TieredBrokerConfig;
import org.apache.druid.server.security.AuthenticatorMapper;
import org.apache.druid.server.security.AuthorizerMapper;
import org.apache.druid.server.security.Escalator;
import org.apache.druid.server.security.TLSCertificateCheckerModule;
import org.apache.druid.sql.avatica.AvaticaMonitor;
import org.apache.druid.sql.avatica.DruidAvaticaJsonHandler;
import org.apache.druid.sql.avatica.DruidMeta;
import org.apache.druid.sql.calcite.SqlTestFrameworkConfig;
import org.apache.druid.sql.calcite.SqlTestFrameworkConfig.ConfigurationInstance;
import org.apache.druid.sql.calcite.SqlTestFrameworkConfig.SqlTestFrameworkConfigStore;
import org.apache.druid.sql.calcite.planner.CalciteRulesManager;
import org.apache.druid.sql.calcite.planner.CatalogResolver;
import org.apache.druid.sql.calcite.planner.PlannerConfig;
import org.apache.druid.sql.calcite.run.SqlEngine;
import org.apache.druid.sql.calcite.schema.DruidSchemaCatalog;
import org.apache.druid.sql.calcite.schema.DruidSchemaName;
import org.apache.druid.sql.calcite.util.CalciteTests;
import org.apache.druid.sql.calcite.util.SqlTestFramework;
@ -142,13 +126,10 @@ import org.apache.druid.sql.guice.SqlModule;
import org.apache.druid.storage.StorageConnectorModule;
import org.apache.druid.timeline.PruneLoadSpec;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.eclipse.jetty.server.Server;
import org.junit.Test;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -157,10 +138,7 @@ import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -181,245 +159,6 @@ public class Launcher
{
}
public Connection connect(String url, Properties info) throws SQLException
{
try {
SqlTestFrameworkConfig config = buildConfigfromURIParams(url);
ConfigurationInstance ci = CONFIG_STORE.getConfigurationInstance(
config,
x -> new AvaticaBasedTestConnectionSupplier(x)
);
AvaticaJettyServer server = ci.framework.injector().getInstance(AvaticaJettyServer.class);
return server.getConnection(info);
}
catch (Exception e) {
throw new SQLException("Can't create testconnection", e);
}
}
static class AvaticaBasedConnectionModule implements DruidModule, Closeable
{
Closer closer = Closer.create();
@Provides
@LazySingleton
public DruidSchemaCatalog getLookupNodeService(QueryRunnerFactoryConglomerate conglomerate,
SpecificSegmentsQuerySegmentWalker walker, PlannerConfig plannerConfig)
{
return CalciteTests.createMockRootSchema(
conglomerate,
walker,
plannerConfig,
CalciteTests.TEST_AUTHORIZER_MAPPER
);
}
@Provides
@LazySingleton
public DruidConnectionExtras getConnectionExtras(ObjectMapper objectMapper)
{
return new DruidConnectionExtras.DruidConnectionExtrasImpl(objectMapper);
}
@Provides
@LazySingleton
public AvaticaJettyServer getAvaticaServer(DruidMeta druidMeta, DruidConnectionExtras druidConnectionExtras)
throws Exception
{
AvaticaJettyServer avaticaJettyServer = new AvaticaJettyServer(druidMeta, druidConnectionExtras);
closer.register(avaticaJettyServer);
return avaticaJettyServer;
}
@Override
public void configure(Binder binder)
{
}
@Override
public void close() throws IOException
{
closer.close();
}
}
static class AvaticaJettyServer implements Closeable
{
final DruidMeta druidMeta;
final Server server;
final String url;
final DruidConnectionExtras connectionExtras;
AvaticaJettyServer(final DruidMeta druidMeta, DruidConnectionExtras druidConnectionExtras) throws Exception
{
this.druidMeta = druidMeta;
server = new Server(0);
server.setHandler(getAvaticaHandler(druidMeta));
server.start();
url = StringUtils.format(
"jdbc:avatica:remote:url=%s",
new URIBuilder(server.getURI()).setPath(DruidAvaticaJsonHandler.AVATICA_PATH).build()
);
connectionExtras = druidConnectionExtras;
}
public Connection getConnection(Properties info) throws SQLException
{
Connection realConnection = DriverManager.getConnection(url, info);
Connection proxyConnection = DynamicComposite.make(
realConnection,
Connection.class,
connectionExtras,
DruidConnectionExtras.class
);
return proxyConnection;
}
@Override
public void close()
{
druidMeta.closeAllConnections();
try {
server.stop();
}
catch (Exception e) {
throw new RuntimeException("Can't stop server", e);
}
}
protected AbstractAvaticaHandler getAvaticaHandler(final DruidMeta druidMeta)
{
return new DruidAvaticaJsonHandler(
druidMeta,
new DruidNode("dummy", "dummy", false, 1, null, true, false),
new AvaticaMonitor()
);
}
}
static class AvaticaBasedTestConnectionSupplier implements QueryComponentSupplier
{
private QueryComponentSupplier delegate;
private AvaticaBasedConnectionModule connectionModule;
public AvaticaBasedTestConnectionSupplier(QueryComponentSupplier delegate)
{
this.delegate = delegate;
this.connectionModule = new AvaticaBasedConnectionModule();
}
@Override
public void gatherProperties(Properties properties)
{
delegate.gatherProperties(properties);
}
@Override
public void configureGuice(DruidInjectorBuilder builder)
{
delegate.configureGuice(builder);
TestRequestLogger testRequestLogger = new TestRequestLogger();
// builder.addModule(connectionModule);
builder.addModule(
binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("test");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
binder.bind(AuthenticatorMapper.class).toInstance(CalciteTests.TEST_AUTHENTICATOR_MAPPER);
binder.bind(AuthorizerMapper.class).toInstance(CalciteTests.TEST_AUTHORIZER_MAPPER);
binder.bind(Escalator.class).toInstance(CalciteTests.TEST_AUTHENTICATOR_ESCALATOR);
binder.bind(RequestLogger.class).toInstance(testRequestLogger);
binder.bind(String.class)
.annotatedWith(DruidSchemaName.class)
.toInstance(CalciteTests.DRUID_SCHEMA_NAME);
binder.bind(ServiceEmitter.class).to(NoopServiceEmitter.class);
binder.bind(QuerySchedulerProvider.class).in(LazySingleton.class);
binder.bind(QueryScheduler.class)
.toProvider(QuerySchedulerProvider.class)
.in(LazySingleton.class);
binder.install(new SqlModule.SqlStatementFactoryModule());
binder.bind(new TypeLiteral<Supplier<DefaultQueryConfig>>()
{
}).toInstance(Suppliers.ofInstance(new DefaultQueryConfig(ImmutableMap.of())));
binder.bind(CalciteRulesManager.class).toInstance(new CalciteRulesManager(ImmutableSet.of()));
binder.bind(CatalogResolver.class).toInstance(CatalogResolver.NULL_RESOLVER);
}
);
}
@Override
public QueryRunnerFactoryConglomerate createCongolmerate(Builder builder, Closer closer)
{
return delegate.createCongolmerate(builder, closer);
}
@Override
public SpecificSegmentsQuerySegmentWalker createQuerySegmentWalker(QueryRunnerFactoryConglomerate conglomerate,
JoinableFactoryWrapper joinableFactory, Injector injector)
{
return delegate.createQuerySegmentWalker(conglomerate, joinableFactory, injector);
}
@Override
public SqlEngine createEngine(QueryLifecycleFactory qlf, ObjectMapper objectMapper, Injector injector)
{
return delegate.createEngine(qlf, objectMapper, injector);
}
@Override
public void configureJsonMapper(ObjectMapper mapper)
{
delegate.configureJsonMapper(mapper);
}
@Override
public JoinableFactoryWrapper createJoinableFactoryWrapper(LookupExtractorFactoryContainerProvider lookupProvider)
{
return delegate.createJoinableFactoryWrapper(lookupProvider);
}
@Override
public void finalizeTestFramework(SqlTestFramework sqlTestFramework)
{
delegate.finalizeTestFramework(sqlTestFramework);
}
@Override
public void close() throws IOException
{
connectionModule.close();
delegate.close();
}
@Override
public PlannerComponentSupplier getPlannerComponentSupplier()
{
return delegate.getPlannerComponentSupplier();
}
}
protected File createTempFolder(String prefix)
{
File tempDir = FileUtils.createTempDir(prefix);
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
try {
FileUtils.deleteDirectory(tempDir);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
return tempDir;
}
public static SqlTestFrameworkConfig buildConfigfromURIParams(String url) throws SQLException
{
Map<String, String> queryParams;
@ -438,111 +177,12 @@ public class Launcher
return new SqlTestFrameworkConfig(queryParams);
}
public static void main(String[] args) throws Exception
{
ConfigurationInstance ci = getCI();
AvaticaJettyServer server = ci.framework.injector().getInstance(AvaticaJettyServer.class);
}
private static ConfigurationInstance getCI() throws SQLException, Exception
{
SqlTestFrameworkConfig config = buildConfigfromURIParams("druidtest:///");
ConfigurationInstance ci = CONFIG_STORE.getConfigurationInstance(
config,
x -> new AvaticaBasedTestConnectionSupplier(x)
);
return ci;
}
@Test
public void runIt() throws Exception
{
Launcher.main3(null);
}
private static Module propOverrideModuel1()
{
Properties localProps = makeLocalProps();
Module m = binder -> binder.bind(Properties.class).toInstance(localProps);
return m;
}
private static Properties makeLocalProps()
{
Properties localProps = new Properties();
localProps.put("druid.enableTlsPort", "false");
localProps.put("druid.zk.service.enabled", "false");
localProps.put("druid.plaintextPort","12345");
localProps.put("druid.host", "localhost");
return localProps;
}
static class CustomStartupInjectorBuilder extends StartupInjectorBuilder {
private List<com.google.inject.Module> overrideModules =new ArrayList<>();
public CustomStartupInjectorBuilder()
{
// Module m = propOverrideModuel();
// addOverride(m);
// addOverride(binder -> {
// binder.bind(SSLClientConfig.class).toProvider(Providers.of(null));
// binder.bind(SSLClientConfig.class).annotatedWith(Global.class).toProvider(Providers.of(null));
// binder.bind(SSLClientConfig.class).annotatedWith(EscalatedGlobal.class).toProvider(Providers.of(null));
// binder.bind(SSLContext.class).toProvider(Providers.of(null));
// binder.bind(SSLContextProvider.class).annotatedWith(Global.class).toProvider(Providers.of(null));
// binder.bind(SSLContextProvider.class).annotatedWith(EscalatedGlobal.class).toProvider(Providers.of(null));
// binder.bind(SSLContext.class).annotatedWith(Global.class).toProvider(Providers.of(null));
// binder.bind(SSLContext.class).annotatedWith(EscalatedGlobal.class).toProvider(Providers.of(null));
// }
// );
}
private void addOverride(com.google.inject.Module m)
{
overrideModules.add(m);
}
@Override
public Injector build()
{
return Guice.createInjector(
Modules.override(modules)
.with( overrideModules.toArray(new com.google.inject.Module[0])));
}
}
private static void main1(Object object) throws Exception
{
final Injector injector = new CustomStartupInjectorBuilder()
.forTests()
.build();
SqlTestFramework framework = getCI().extracted(
new DiscovertModule()
)
;
// SSLContextProvider u = injector.getInstance(SSLContextProvider.class);
// System.out.println(u);
}
private static ConfigurationInstance getCI2() throws SQLException, Exception
{
SqlTestFrameworkConfig config = buildConfigfromURIParams("druidtest:///");
@ -557,12 +197,10 @@ public class Launcher
static class AvaticaBasedTestConnectionSupplier2 implements QueryComponentSupplier
{
private QueryComponentSupplier delegate;
private AvaticaBasedConnectionModule connectionModule;
public AvaticaBasedTestConnectionSupplier2(QueryComponentSupplier delegate)
{
this.delegate = delegate;
this.connectionModule = new AvaticaBasedConnectionModule();
}
@Override
@ -582,25 +220,9 @@ public class Launcher
{
delegate.configureGuice(builder);
TestRequestLogger testRequestLogger = new TestRequestLogger();
// builder.addModule(connectionModule);
overrideModules.add(new DiscovertModule());
if(false) {
builder.addModule(new LegacyBrokerParallelMergeConfigModule());
builder.addModule(new BrokerProcessingModule());
// new QueryableModule(),
builder.addModule(new QueryRunnerFactoryModule());
builder.addModule(new SegmentWranglerModule());
builder.addModule(new JoinableFactoryModule());
builder.addModule(new BrokerServiceModule());
}
// builder.addModule(new StorageNodeModule());
builder.addModule(
binder -> {
// why need to add this?
@ -724,7 +346,6 @@ public class Launcher
@Override
public void close() throws IOException
{
connectionModule.close();
delegate.close();
}