Skip to content

Commit 3234b50

Browse files
authored
SQL: jdbc debugging enhancement (#53880) (#54081)
* add flush always output option that will flush the output printer after each debug message when enabled (disabled by default) * at debug output initializationtime, log debug output information about OS, JVM and default JVM timezone (cherry picked from commit b5db965)
1 parent 9da589c commit 3234b50

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Debug.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ private static DebugLog logger(JdbcConfiguration info, PrintWriter managedPrinte
113113
synchronized (Debug.class) {
114114
log = OUTPUT_MANAGED.get(managedPrinter);
115115
if (log == null) {
116-
log = new DebugLog(managedPrinter);
116+
log = createLog(managedPrinter, info.flushAlways());
117117
OUTPUT_MANAGED.put(managedPrinter, log);
118118
}
119-
return log;
120119
}
121120
}
122121

@@ -135,7 +134,7 @@ private static DebugLog logger(JdbcConfiguration info, PrintWriter managedPrinte
135134
ERR = null;
136135
}
137136
if (ERR == null) {
138-
ERR = new DebugLog(new PrintWriter(new OutputStreamWriter(sys, StandardCharsets.UTF_8)));
137+
ERR = createLog(new PrintWriter(new OutputStreamWriter(sys, StandardCharsets.UTF_8)), info.flushAlways());
139138
}
140139
return ERR;
141140
}
@@ -154,7 +153,7 @@ private static DebugLog logger(JdbcConfiguration info, PrintWriter managedPrinte
154153
}
155154

156155
if (OUT == null) {
157-
OUT = new DebugLog(new PrintWriter(new OutputStreamWriter(sys, StandardCharsets.UTF_8)));
156+
OUT = createLog(new PrintWriter(new OutputStreamWriter(sys, StandardCharsets.UTF_8)), info.flushAlways());
158157
}
159158
return OUT;
160159
}
@@ -165,7 +164,7 @@ private static DebugLog logger(JdbcConfiguration info, PrintWriter managedPrinte
165164
// must be local file
166165
try {
167166
PrintWriter print = new PrintWriter(Files.newBufferedWriter(Paths.get("").resolve(out), StandardCharsets.UTF_8));
168-
log = new DebugLog(print);
167+
log = createLog(print, info.flushAlways());
169168
OUTPUT_CACHE.put(out, log);
170169
OUTPUT_REFS.put(out, Integer.valueOf(0));
171170
} catch (Exception ex) {
@@ -178,6 +177,12 @@ private static DebugLog logger(JdbcConfiguration info, PrintWriter managedPrinte
178177
return log;
179178
}
180179

180+
private static DebugLog createLog(PrintWriter print, boolean flushAlways) {
181+
DebugLog log = new DebugLog(print, flushAlways);
182+
log.logSystemInfo();
183+
return log;
184+
}
185+
181186
static void release(JdbcConfiguration info) {
182187
if (!info.debug()) {
183188
return;

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DebugLog.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ final class DebugLog {
1717
private static final String HEADER = "%tF/%tT.%tL - ";
1818

1919
final PrintWriter print;
20+
private boolean flushAlways;
2021

21-
DebugLog(PrintWriter print) {
22+
DebugLog(PrintWriter print, boolean flushAlways) {
2223
this.print = print;
24+
this.flushAlways = flushAlways;
2325
}
2426

2527
void logMethod(Method m, Object[] args) {
@@ -31,9 +33,11 @@ void logMethod(Method m, Object[] args) {
3133
m.getName(),
3234
//array(m.getParameterTypes()),
3335
array(args));
36+
if (flushAlways) {
37+
print.flush();
38+
}
3439
}
3540

36-
3741
void logResult(Method m, Object[] args, Object r) {
3842
long time = System.currentTimeMillis();
3943
print.printf(Locale.ROOT, HEADER + "%s#%s(%s) returned %s%n",
@@ -44,6 +48,9 @@ void logResult(Method m, Object[] args, Object r) {
4448
//array(m.getParameterTypes()),
4549
array(args),
4650
r);
51+
if (flushAlways) {
52+
print.flush();
53+
}
4754
}
4855

4956
void logException(Method m, Object[] args, Throwable t) {
@@ -57,6 +64,25 @@ void logException(Method m, Object[] args, Throwable t) {
5764
print.flush();
5865
}
5966

67+
void logSystemInfo() {
68+
long time = System.currentTimeMillis();
69+
print.printf(Locale.ROOT, HEADER + "OS[%s/%s/%s], JVM[%s/%s/%s/%s]",
70+
time, time, time,
71+
System.getProperty("os.name"),
72+
System.getProperty("os.version"),
73+
System.getProperty("os.arch"),
74+
System.getProperty("java.vm.vendor"),
75+
System.getProperty("java.vm.name"),
76+
System.getProperty("java.version"),
77+
System.getProperty("java.vm.version"));
78+
print.println();
79+
time = System.currentTimeMillis();
80+
print.printf(Locale.ROOT, HEADER + "JVM default timezone: %s",
81+
time, time, time,
82+
java.util.TimeZone.getDefault().toString());
83+
print.println();
84+
print.flush();
85+
}
6086

6187
private static String array(Object[] a) {
6288
if (a == null || a.length == 0) {

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfiguration.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class JdbcConfiguration extends ConnectionConfiguration {
4747
// can be out/err/url
4848
static final String DEBUG_OUTPUT_DEFAULT = "err";
4949

50+
static final String DEBUG_FLUSH_ALWAYS = "debug.flushAlways";
51+
// can be buffered/immediate
52+
static final String DEBUG_FLUSH_ALWAYS_DEFAULT = "false";
53+
5054
public static final String TIME_ZONE = "timezone";
5155
// follow the JDBC spec and use the JVM default...
5256
// to avoid inconsistency, the default is picked up once at startup and reused across connections
@@ -63,7 +67,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
6367

6468
// options that don't change at runtime
6569
private static final Set<String> OPTION_NAMES = new LinkedHashSet<>(
66-
Arrays.asList(TIME_ZONE, FIELD_MULTI_VALUE_LENIENCY, INDEX_INCLUDE_FROZEN, DEBUG, DEBUG_OUTPUT));
70+
Arrays.asList(TIME_ZONE, FIELD_MULTI_VALUE_LENIENCY, INDEX_INCLUDE_FROZEN, DEBUG, DEBUG_OUTPUT, DEBUG_FLUSH_ALWAYS));
6771

6872
static {
6973
// trigger version initialization
@@ -76,6 +80,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
7680
// immutable properties
7781
private final boolean debug;
7882
private final String debugOut;
83+
private final boolean flushAlways;
7984

8085
// mutable ones
8186
private ZoneId zoneId;
@@ -158,6 +163,8 @@ private JdbcConfiguration(URI baseURI, String u, Properties props) throws JdbcSQ
158163

159164
this.debug = parseValue(DEBUG, props.getProperty(DEBUG, DEBUG_DEFAULT), Boolean::parseBoolean);
160165
this.debugOut = props.getProperty(DEBUG_OUTPUT, DEBUG_OUTPUT_DEFAULT);
166+
this.flushAlways = parseValue(DEBUG_FLUSH_ALWAYS, props.getProperty(DEBUG_FLUSH_ALWAYS, DEBUG_FLUSH_ALWAYS_DEFAULT),
167+
Boolean::parseBoolean);
161168

162169
this.zoneId = parseValue(TIME_ZONE, props.getProperty(TIME_ZONE, TIME_ZONE_DEFAULT),
163170
s -> TimeZone.getTimeZone(s).toZoneId().normalized());
@@ -184,6 +191,10 @@ public String debugOut() {
184191
return debugOut;
185192
}
186193

194+
public boolean flushAlways() {
195+
return flushAlways;
196+
}
197+
187198
public TimeZone timeZone() {
188199
return zoneId != null ? TimeZone.getTimeZone(zoneId) : null;
189200
}

x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ public void testDebugOut() throws Exception {
7575
assertThat(ci.debugOut(), is("jdbc.out"));
7676
}
7777

78+
public void testDebugFlushAlways() throws Exception {
79+
JdbcConfiguration ci = ci("jdbc:es://a:1/?debug=true&debug.flushAlways=false");
80+
assertThat(ci.baseUri().toString(), is("http://a:1/"));
81+
assertThat(ci.debug(), is(true));
82+
assertThat(ci.flushAlways(), is(false));
83+
84+
ci = ci("jdbc:es://a:1/?debug=true&debug.flushAlways=true");
85+
assertThat(ci.baseUri().toString(), is("http://a:1/"));
86+
assertThat(ci.debug(), is(true));
87+
assertThat(ci.flushAlways(), is(true));
88+
89+
ci = ci("jdbc:es://a:1/?debug=true");
90+
assertThat(ci.baseUri().toString(), is("http://a:1/"));
91+
assertThat(ci.debug(), is(true));
92+
assertThat(ci.flushAlways(), is(false));
93+
}
94+
7895
public void testTypeInParam() throws Exception {
7996
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://a:1/foo/bar/tar?debug=true&debug.out=jdbc.out"));
8097
assertEquals("Unknown parameter [debug.out]; did you mean [debug.output]", e.getMessage());

0 commit comments

Comments
 (0)