forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
615 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 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,14 @@ | ||
description = 'IDE Debugging for SQL' | ||
|
||
dependencies { | ||
testCompile project(path: ":client:transport") | ||
testCompile project(path: ":modules:aggs-matrix-stats") | ||
testCompile project(path: xpackModule('sql')) | ||
testCompile project(path: xpackModule('core')) | ||
} | ||
|
||
forbiddenApisTest.enabled = false | ||
namingConventions.enabled = false | ||
integTest.enabled = false | ||
testingConventions.enabled = false | ||
|
43 changes: 43 additions & 0 deletions
43
...lugin/sql/qa/debug/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/ClientReference.java
This file contains 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,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.qa.jdbc; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.inject.Module; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.plugins.Plugin; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Collection; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
public class ClientReference extends Plugin { | ||
|
||
static final ClientInvocationHandler HANDLER = new ClientInvocationHandler(); | ||
private static final Client CLIENT = | ||
(Client) Proxy.newProxyInstance(ClientReference.class.getClassLoader(), new Class[]{Client.class}, HANDLER); | ||
private static final XPackLicenseState LICENSE = new XPackLicenseState(Settings.EMPTY); | ||
|
||
@Override | ||
public Collection<Module> createGuiceModules() { | ||
return asList(b -> b.bind(Client.class).toInstance(CLIENT), b -> b.bind(XPackLicenseState.class).toInstance(LICENSE)); | ||
} | ||
|
||
static class ClientInvocationHandler implements InvocationHandler { | ||
|
||
volatile Client actualClient; | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
return method.invoke(actualClient, args); | ||
} | ||
} | ||
} |
This file contains 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 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
105 changes: 105 additions & 0 deletions
105
...gin/sql/qa/debug/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/EmbeddedSqlServer.java
This file contains 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,105 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.qa.jdbc; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.elasticsearch.client.transport.TransportClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.search.aggregations.matrix.MatrixAggregationPlugin; | ||
import org.elasticsearch.transport.client.PreBuiltTransportClient; | ||
import org.elasticsearch.xpack.sql.plugin.SqlPlugin; | ||
import org.junit.rules.ExternalResource; | ||
|
||
import java.net.InetAddress; | ||
import java.security.AccessControlException; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* Embedded JDBC server that uses the transport client to power | ||
* the jdbc endpoints in the same JVM as the tests. | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public class EmbeddedSqlServer extends ExternalResource implements AutoCloseable { | ||
|
||
private final Properties properties; | ||
private TransportClient client; | ||
private SqlHttpServer server; | ||
private String jdbcUrl; | ||
|
||
EmbeddedSqlServer() { | ||
this(false); | ||
} | ||
|
||
private EmbeddedSqlServer(boolean debug) { | ||
properties = new Properties(); | ||
if (debug) { | ||
properties.setProperty("debug", "true"); | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({"resource"}) | ||
protected void before() throws Throwable { | ||
try { | ||
Settings settings = Settings.builder() | ||
.put("client.transport.ignore_cluster_name", true) | ||
.build(); | ||
TransportAddress transportAddress = new TransportAddress(InetAddress.getLoopbackAddress(), 9300); | ||
client = new PreBuiltTransportClient(settings, MatrixAggregationPlugin.class, ClientReference.class, SqlPlugin.class) | ||
.addTransportAddress(transportAddress); | ||
|
||
// update static reference | ||
ClientReference.HANDLER.actualClient = client; | ||
} catch (ExceptionInInitializerError e) { | ||
if (e.getCause() instanceof AccessControlException) { | ||
throw new RuntimeException(getClass().getSimpleName() + " is not available with the security manager", e); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
server = new SqlHttpServer(client); | ||
server.start(0); | ||
jdbcUrl = server.jdbcUrl(); | ||
|
||
LogManager.getLogger(EmbeddedSqlServer.class).info("Embedded SQL started at [{}]", server.url()); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
after(); | ||
} | ||
|
||
@Override | ||
protected void after() { | ||
if (client != null) { | ||
client.close(); | ||
client = null; | ||
} | ||
if (server != null) { | ||
server.stop(); | ||
server = null; | ||
} | ||
} | ||
|
||
Connection connection(Properties props) throws SQLException { | ||
assertNotNull("ES JDBC Server is null - make sure ES is properly run as a @ClassRule", client); | ||
Properties p = new Properties(properties); | ||
p.putAll(props); | ||
return DriverManager.getConnection(jdbcUrl, p); | ||
// JdbcDataSource dataSource = new JdbcDataSource(); | ||
// dataSource.setProperties(properties); | ||
// dataSource.setUrl(jdbcUrl); | ||
// return dataSource.getConnection(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ck/plugin/sql/qa/debug/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/RootHandler.java
This file contains 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,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.qa.jdbc; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.SuppressForbidden; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
|
||
@SuppressForbidden(reason = "use http server") | ||
@SuppressWarnings("restriction") | ||
class RootHandler implements HttpHandler { | ||
|
||
private static final Logger log = LogManager.getLogger(RootHandler.class.getName()); | ||
|
||
@Override | ||
public void handle(HttpExchange http) throws IOException { | ||
log.debug("Received query call..."); | ||
|
||
if ("HEAD".equals(http.getRequestMethod())) { | ||
http.sendResponseHeaders(RestStatus.OK.getStatus(), 0); | ||
http.close(); | ||
return; | ||
} | ||
|
||
fail(http, new UnsupportedOperationException("only HEAD allowed")); | ||
} | ||
|
||
private void fail(HttpExchange http, Exception ex) { | ||
log.error("Caught error while transmitting response", ex); | ||
try { | ||
// the error conversion has failed, halt | ||
if (http.getResponseHeaders().isEmpty()) { | ||
http.sendResponseHeaders(RestStatus.INTERNAL_SERVER_ERROR.getStatus(), -1); | ||
} | ||
} catch (IOException ioEx) { | ||
log.error("Caught error while trying to catch error", ex); | ||
} finally { | ||
http.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.