-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed usePrefixPath and table descriptions cache (#81)
- Loading branch information
Showing
15 changed files
with
324 additions
and
237 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
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 |
---|---|---|
@@ -1,126 +1,34 @@ | ||
package tech.ydb.jdbc; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import tech.ydb.jdbc.impl.YdbTracerImpl; | ||
|
||
|
||
|
||
/** | ||
* | ||
* @author Aleksandr Gorshenin | ||
*/ | ||
public class YdbTracer { | ||
private static final Logger LOGGER = Logger.getLogger(YdbTracer.class.getName()); | ||
private static final ThreadLocal<YdbTracer> LOCAL = new ThreadLocal<>(); | ||
private static final AtomicLong ANONYMOUS_COUNTER = new AtomicLong(0); | ||
|
||
private final Date startDate = new Date(); | ||
private final long startedAt = System.currentTimeMillis(); | ||
private final List<Record> records = new ArrayList<>(); | ||
|
||
private String txID = null; | ||
private String label = null; | ||
private boolean isMarked = false; | ||
private boolean isClosed = false; | ||
|
||
private class Record { | ||
private final long executedAt = System.currentTimeMillis(); | ||
private final String message; | ||
private final boolean isRequest; | ||
|
||
Record(String message, boolean isRequest) { | ||
this.message = message; | ||
this.isRequest = isRequest; | ||
} | ||
public interface YdbTracer { | ||
static void clear() { | ||
YdbTracerImpl.clear(); | ||
} | ||
|
||
public static void clear() { | ||
LOCAL.remove(); | ||
static YdbTracer current() { | ||
return YdbTracerImpl.current(); | ||
} | ||
|
||
public static YdbTracer current() { | ||
YdbTracer tracer = LOCAL.get(); | ||
if (tracer == null || tracer.isClosed) { | ||
tracer = new YdbTracer(); | ||
LOCAL.set(tracer); | ||
} | ||
|
||
return tracer; | ||
} | ||
|
||
public void trace(String message) { | ||
records.add(new Record(message, false)); | ||
} | ||
|
||
public void traceRequest(String message) { | ||
records.add(new Record(message, true)); | ||
} | ||
|
||
public void setId(String id) { | ||
if (!Objects.equals(id, txID)) { | ||
this.txID = id; | ||
trace("set-id " + id); | ||
} | ||
} | ||
|
||
public void markToPrint() { | ||
if (!isMarked) { | ||
this.isMarked = true; | ||
trace("markToPrint"); | ||
} | ||
} | ||
|
||
public void markToPrint(String label) { | ||
if (!isMarked) { | ||
this.isMarked = true; | ||
this.label = label; | ||
trace("markToPrint " + label); | ||
} | ||
} | ||
void setId(String id); | ||
|
||
public void close() { | ||
isClosed = true; | ||
void trace(String message); | ||
|
||
LOCAL.remove(); | ||
void query(String queryText); | ||
|
||
final Level level = isMarked ? Level.INFO : Level.FINE; | ||
if (!LOGGER.isLoggable(level) || records.isEmpty()) { | ||
return; | ||
} | ||
void markToPrint(String label); | ||
|
||
long finishedAt = System.currentTimeMillis(); | ||
long requestsTime = 0; | ||
void close(); | ||
|
||
String id = txID != null ? txID : "anonymous-" + ANONYMOUS_COUNTER.incrementAndGet(); | ||
String traceID = label == null ? id : label + "-" + id; | ||
LOGGER.log(level, "Trace[{0}] started at {1}", new Object[] {traceID, startDate}); | ||
long last = startedAt; | ||
long requestsCount = 0; | ||
boolean lastIsRequest = false; | ||
for (Record record: records) { | ||
if (record.isRequest) { | ||
requestsCount++; | ||
lastIsRequest = true; | ||
if (record.message != null) { | ||
LOGGER.log(level, "Query[{0}]: {1}", new Object[] {traceID, record.message.replaceAll("\\s", " ")}); | ||
} | ||
} else { | ||
long ms = record.executedAt - last; | ||
if (lastIsRequest) { | ||
requestsTime += ms; | ||
lastIsRequest = false; | ||
} | ||
LOGGER.log(level, "Trace[{0}] {1} ms {2}", new Object[] {traceID, ms, record.message}); | ||
last = record.executedAt; | ||
} | ||
} | ||
LOGGER.log(level, "Trace[{0}] finished in {1} ms, {2} requests take {3} ms", new Object[] { | ||
traceID, finishedAt - startedAt, requestsCount, requestsTime | ||
}); | ||
default void markToPring() { | ||
markToPrint(null); | ||
} | ||
} |
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
Oops, something went wrong.