Skip to content

Commit f6010b1

Browse files
committed
Enhancements to client stats and addition of a test
1 parent 04cfd4b commit f6010b1

File tree

8 files changed

+721
-38
lines changed

8 files changed

+721
-38
lines changed

driver/src/main/java/oracle/nosql/driver/NoSQLHandleConfig.java

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,26 @@ public class NoSQLHandleConfig implements Cloneable {
201201
/**
202202
* Statistics configuration, optional.
203203
*/
204-
private final static String PROFILE_PROPERTY =
204+
public static final String STATS_PROFILE_PROPERTY =
205205
"com.oracle.nosql.sdk.nosqldriver.stats.profile";
206-
private final static String INTERVAL_PROPERTY =
206+
public static final String STATS_INTERVAL_PROPERTY =
207207
"com.oracle.nosql.sdk.nosqldriver.stats.interval";
208-
private final static String PRETTY_PRINT_PROPERTY =
208+
public static final String STATS_PRETTY_PRINT_PROPERTY =
209209
"com.oracle.nosql.sdk.nosqldriver.stats.pretty-print";
210+
public static final String STATS_ENABLE_LOG_PROPERTY =
211+
"com.oracle.nosql.sdk.nosqldriver.stats.enable-log";
212+
210213
/* Statistics logging interval in seconds. Default 600 sec, ie. 10 min. */
211-
private int statsInterval = 600;
212-
private StatsControl.Profile statsProfile = StatsControl.Profile.NONE;
213-
private boolean statsPrettyPrint = false;
214+
public static final int DEFAULT_STATS_INTERVAL = 600;
215+
public static final StatsControl.Profile DEFAULT_STATS_PROFILE =
216+
StatsControl.Profile.NONE;
217+
public static final boolean DEFAULT_STATS_PRETTY_PRINT = false;
218+
public static final boolean DEFAULT_ENABLE_LOG = true;
219+
220+
private int statsInterval = DEFAULT_STATS_INTERVAL;
221+
private StatsControl.Profile statsProfile = DEFAULT_STATS_PROFILE;
222+
private boolean statsPrettyPrint = DEFAULT_STATS_PRETTY_PRINT;
223+
private boolean statsEnableLog = DEFAULT_ENABLE_LOG;
214224
private StatsControl.StatsHandler statsHandler = null;
215225

216226
/**
@@ -1289,6 +1299,33 @@ public boolean getStatsPrettyPrint() {
12891299
return this.statsPrettyPrint;
12901300
}
12911301

1302+
/**
1303+
* When stats are enabled the logging is automatically turned on. Setting
1304+
* this to false avoids turning the log on when enabling stats.
1305+
* Default is on.
1306+
*
1307+
* @param statsEnableLog flag to enable JSON pretty print
1308+
* @return this
1309+
*
1310+
* @since 5.2.30
1311+
*/
1312+
public NoSQLHandleConfig setStatsEnableLog(boolean statsEnableLog) {
1313+
this.statsEnableLog = statsEnableLog;
1314+
return this;
1315+
}
1316+
1317+
/**
1318+
* Returns the current value of enabling the log when stats are turned on.
1319+
* Default is enabled.
1320+
*
1321+
* @return the current state of stats enable log flag.
1322+
*
1323+
* @since 5.2.30
1324+
*/
1325+
public boolean getStatsEnableLog() {
1326+
return this.statsEnableLog;
1327+
}
1328+
12921329
/**
12931330
* Registers a handler that is called every time the statistics are logged.
12941331
* Note: setting a stats handler will not affect the stats log entries.
@@ -1352,7 +1389,7 @@ static private void checkRegion(Region region,
13521389
}
13531390

13541391
private void setConfigFromEnvironment() {
1355-
String profileProp = System.getProperty(PROFILE_PROPERTY);
1392+
String profileProp = System.getProperty(STATS_PROFILE_PROPERTY);
13561393
if (profileProp != null) {
13571394
try {
13581395
setStatsProfile(StatsControl.Profile.valueOf(
@@ -1361,29 +1398,36 @@ private void setConfigFromEnvironment() {
13611398
if (logger != null) {
13621399
logger.log(Level.SEVERE, StatsControl.LOG_PREFIX +
13631400
"Invalid profile value for system property " +
1364-
PROFILE_PROPERTY + ": " + profileProp);
1401+
STATS_PROFILE_PROPERTY + ": " + profileProp);
13651402
}
13661403
}
13671404
}
13681405

1369-
String intervalProp = System.getProperty(INTERVAL_PROPERTY);
1406+
String intervalProp = System.getProperty(STATS_INTERVAL_PROPERTY);
13701407
if (intervalProp != null) {
13711408
try {
13721409
setStatsInterval(Integer.valueOf(intervalProp));
13731410
} catch (NumberFormatException nfe) {
13741411
if (logger != null) {
13751412
logger.log(Level.SEVERE, "Invalid integer value for " +
1376-
"system property " + INTERVAL_PROPERTY + ": " +
1413+
"system property " + STATS_INTERVAL_PROPERTY + ": " +
13771414
intervalProp);
13781415
}
13791416
}
13801417
}
13811418

1382-
String ppProp = System.getProperty(PRETTY_PRINT_PROPERTY);
1419+
String ppProp = System.getProperty(STATS_PRETTY_PRINT_PROPERTY);
13831420
if (ppProp != null &&
13841421
("true".equals(ppProp.toLowerCase()) || "1".equals(ppProp) ||
13851422
"on".equals(ppProp.toLowerCase()))) {
13861423
statsPrettyPrint = Boolean.valueOf(ppProp);
13871424
}
1425+
1426+
String elProp = System.getProperty(STATS_ENABLE_LOG_PROPERTY);
1427+
if (elProp != null &&
1428+
("false".equals(elProp.toLowerCase()) || "0".equals(elProp) ||
1429+
"off".equals(elProp.toLowerCase()))) {
1430+
statsEnableLog = Boolean.FALSE;
1431+
}
13881432
}
13891433
}

driver/src/main/java/oracle/nosql/driver/Region.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ public class Region {
119119

120120
static {
121121
/* OC1 */
122-
/* AF */
123-
OC1_REGIONS.put(AF_JOHANNESBURG_1.getRegionId(), AF_JOHANNESBURG_1);
124-
125122
/* APAC */
126123
OC1_REGIONS.put(AP_CHUNCHEON_1.getRegionId(), AP_CHUNCHEON_1);
127124
OC1_REGIONS.put(AP_HYDERABAD_1.getRegionId(), AP_HYDERABAD_1);

driver/src/main/java/oracle/nosql/driver/StatsControl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
*
3838
* -Dcom.oracle.nosql.sdk.nosqldriver.stats.pretty-print=true Option
3939
* to enable pretty printing of the JSON data, default value is
40-
* false</li></ul>
40+
* false</li><li>
41+
*
42+
* -Dcom.oracle.nosql.sdk.nosqldriver.stats.enable-log=false Option
43+
* to turn on logging automatically if stats are enabled, default value is
44+
* true</li></ul>
4145
*
4246
* Statistics can also be enabled by using the API:
4347
* {@link NoSQLHandleConfig#setStatsProfile(StatsControl.Profile)} or

driver/src/main/java/oracle/nosql/driver/http/Stats.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ synchronized void observeQuery(QueryRequest queryRequest, boolean error,
366366
private QueryEntryStat getExtraQueryStat(
367367
QueryRequest queryRequest) {
368368
String sql = queryRequest.getStatement();
369+
if (sql == null && queryRequest.getPreparedStatement() != null) {
370+
sql = queryRequest.getPreparedStatement().getSQLText();
371+
}
369372

370373
QueryEntryStat qStat = queries.get(sql);
371374

@@ -599,7 +602,15 @@ void observe(Request kvRequest, boolean error,
599602

600603
connectionStats.observe(connections);
601604

602-
if (extraQueryStats != null) {
605+
if (extraQueryStats == null &&
606+
statsControl.getProfile().ordinal() >=
607+
StatsControl.Profile.ALL.ordinal()) {
608+
extraQueryStats = new ExtraQueryStats(statsControl);
609+
}
610+
611+
if (extraQueryStats != null &&
612+
statsControl.getProfile().ordinal() >=
613+
StatsControl.Profile.ALL.ordinal()) {
603614
if (kvRequest instanceof QueryRequest) {
604615
QueryRequest queryRequest = (QueryRequest)kvRequest;
605616

@@ -614,7 +625,15 @@ void observe(Request kvRequest, boolean error,
614625
* Adds a new statistic entry for this query request.
615626
*/
616627
void observeQuery(QueryRequest qreq) {
617-
if (extraQueryStats != null) {
628+
if (extraQueryStats == null &&
629+
statsControl.getProfile().ordinal() >=
630+
StatsControl.Profile.ALL.ordinal()) {
631+
extraQueryStats = new ExtraQueryStats(statsControl);
632+
}
633+
634+
if (extraQueryStats != null &&
635+
statsControl.getProfile().ordinal() >=
636+
StatsControl.Profile.ALL.ordinal()) {
618637
extraQueryStats.observeQuery(qreq);
619638
}
620639
}

driver/src/main/java/oracle/nosql/driver/http/StatsControlImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public class StatsControlImpl
4242
this.statsHandler = config.getStatsHandler();
4343

4444
if (profile != Profile.NONE) {
45-
logger.setLevel(Level.INFO);
45+
if (config.getStatsEnableLog() &&
46+
logger.getLevel().intValue() > Level.INFO.intValue()) {
47+
logger.setLevel(Level.INFO);
48+
}
4649
logger.log(Level.INFO, LOG_PREFIX +
4750
"{\"sdkName\" : \"Oracle NoSQL SDK for Java" +
4851
"\", \"sdkVersion\" : \"" +
@@ -101,12 +104,12 @@ public StatsHandler getStatsHandler() {
101104

102105
@Override
103106
public void start() {
104-
if (profile == Profile.NONE) {
105-
stats = null;
106-
} else if (stats == null) {
107-
stats = new Stats(this);
108-
enableCollection = true;
107+
if (profile != Profile.NONE) {
108+
if (stats == null) {
109+
stats = new Stats(this);
110+
}
109111
}
112+
enableCollection = true;
110113
}
111114

112115
@Override

driver/src/main/java/oracle/nosql/driver/ops/QueryRequest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,6 @@ public QueryRequest setMathContext(MathContext mathContext) {
454454
* @return the statement, or null if it has not been set
455455
*/
456456
public String getStatement() {
457-
458-
if (statement == null && preparedStatement != null) {
459-
return preparedStatement.getSQLText();
460-
}
461-
462457
return statement;
463458
}
464459

driver/src/main/java/oracle/nosql/driver/package-info.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
* 1:1 with an identity so they cannot be shared by different users. While
5656
* they are not extremely expensive, they have a connection pool and thread
5757
* pool and are not intended to be disposable.
58-
* <p>
59-
* <strong>Configuration and Multi-threaded Applications</strong>
58+
*
59+
* <p><strong>Configuration and Multi-threaded Applications</strong></p>
6060
* <p>
6161
* High performance multi-threaded applications may benefit from using
6262
* some non-default configuration options related to how threads and
@@ -89,8 +89,8 @@
8989
* it's possible to have too many threads and that more threads does not
9090
* equal more performance. The optimal number depends on request latency and
9191
* other I/O performed. It is best to experiment.
92-
* <p>
93-
* <strong>Logging in the SDK</strong>
92+
*
93+
* <p><strong><a id="Logging">Logging in the SDK</a></strong></p>
9494
* <p>
9595
* The SDK uses logging as provided by the <i>java.util.logging</i> package.
9696
* By default the SDK will log to the console at level INFO. If nothing
@@ -155,7 +155,7 @@
155155
* Collection of stats are controlled by the following system
156156
* properties:<ol><li>
157157
* -Dcom.oracle.nosql.sdk.nosqldriver.stats.profile=[none|regular|more|all]
158-
* Specifies the stats profile: <i>none</i> - disabled,
158+
* Specifies the stats profile: <i>none</i> - disabled,
159159
* <i>regular</i> - per request: counters, errors, latencies, delays, retries
160160
* <i>more</i> - stats above with 95th and 99th percentile latencies
161161
* <i>all</i> - stats above with per query information.</li><li>
@@ -164,7 +164,11 @@
164164
* seconds to log the stats, by default is 10 minutes.</li><li>
165165
*
166166
* -Dcom.oracle.nosql.sdk.nosqldriver.stats.pretty-print=true Option
167-
* to enable pretty printing of the JSON data, default value is false</li>
167+
* to enable pretty printing of the JSON data, default value is false</li><li>
168+
*
169+
* -Dcom.oracle.nosql.sdk.nosqldriver.stats.enable-log=false Option
170+
* to turn on logging automatically if stats are enabled, default value is
171+
* true</li>
168172
* </ol><p>
169173
*
170174
* Statistics can also be enabled by using the API:
@@ -316,13 +320,16 @@
316320
* // query statement
317321
* "query" : "SELECT * FROM audienceData ORDER BY cookie_id",
318322
* // query plan description
319-
* "plan" : "SFW([6])\n[\n FROM:\n RECV([3])\n [\n DistributionKind : ALL_PARTITIONS,\n Sort Fields : sort_gen,\n\n ] as $from-0\n\n SELECT:\n FIELD_STEP([6])\n [\n VAR_REF($from-0)([3]),\n audienceData\n ]\n]",
323+
* "plan" : "SFW([6])\n[\n FROM:\n RECV([3])\n [\n DistributionKind :
324+
* ALL_PARTITIONS,\n Sort Fields : sort_gen,\n\n ] as $from-0\n\n
325+
* SELECT:\n FIELD_STEP([6])\n [\n VAR_REF($from-0)([3]),\n
326+
* audienceData\n ]\n]",
320327
* "doesWrites" : false,
321328
* "httpRequestCount" : 12, // number of http calls to the server
322329
* "unprepared" : 1, // number of query requests without prepare
323330
* "simple" : false, // type of query
324-
* "countAPI" : 20, // number of handle.query() API calls
325-
* "errors" : 0, // number of calls trowing exception
331+
* "count" : 20, // number of handle.query() API calls
332+
* "errors" : 0, // number of calls throwing exception
326333
* "httpRequestLatencyMs" : {// response time of http requests in milliseconds
327334
* "min" : 8, // minimum value in interval
328335
* "avg" : 14.58, // average value in interval
@@ -354,6 +361,27 @@
354361
* "max" : 10 // maximum value in interval
355362
* }
356363
* }
357-
* </pre></li></ol>
364+
* </pre></li></ol><p>
365+
*
366+
* The log entries go to the logger configured in NoSQLHandlerConfig. For
367+
* details on how to configure logger, for example to output to a file, see
368+
* previous paragraph: <a href="#Logging">"Logging in the SDK"</a> . By
369+
* default, if no logger is configured the statistics entries, if enabled,
370+
* will be logged to the console.</p><p>
371+
*
372+
* Stats collection is not dependent of logging configuration, even if
373+
* logging is disabled, collection of stats will still happen if stats
374+
* profile other than none is used. In this case, the stats are available by
375+
* using the stats handler.</p><p>
376+
*
377+
* Depending on the type of query, if client processing is required, for
378+
* example in the case of ordered or aggregate queries, indicated by a
379+
* false <i>{@code simple}</i> field, the <i>{@code count}</i> and <i>{@code
380+
* httpRequestsCount}</i> numbers will differ. <i>{@code count}</i> represents
381+
* the number of <i>{@code handle.query()}</i> API calls and
382+
* <i>{@code httpRequestCount}</i> represents the number of internal http
383+
* requests from server. For these type of queries, the driver executes
384+
* several simpler queries, per shard or partition, and than combines the
385+
* results locally.</p>
358386
*/
359387
package oracle.nosql.driver;

0 commit comments

Comments
 (0)