Skip to content

Commit f7f6cde

Browse files
Better logs when doing code updates
* Using MDC keys like `user` and `session` for context specific things: * `user` is the jurisdiction, i.e. Massachusetts or Illinois * `session` is the court location, i.e. `adams` * `request` is the specific codes table, i.e. `partytype`, etc. * Better variable name and reporting on the time took (incremental as well as total duration).
1 parent 2efefe9 commit f7f6cde

2 files changed

Lines changed: 48 additions & 18 deletions

File tree

proxyserver/src/main/java/edu/suffolk/litlab/efsp/ecfcodes/CodeUpdater.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import edu.suffolk.litlab.efsp.server.ecf4.Ecf4Helper;
66
import edu.suffolk.litlab.efsp.server.ecf4.SoapClientChooser;
77
import edu.suffolk.litlab.efsp.server.utils.HeaderSigner;
8+
import edu.suffolk.litlab.efsp.server.utils.MDCWrappers;
89
import edu.suffolk.litlab.efsp.server.utils.ServiceHelpers;
910
import edu.suffolk.litlab.efsp.server.utils.SoapX509CallbackHandler;
1011
import edu.suffolk.litlab.efsp.stdlib.StdLib;
@@ -49,6 +50,7 @@
4950
import org.apache.cxf.headers.Header;
5051
import org.slf4j.Logger;
5152
import org.slf4j.LoggerFactory;
53+
import org.slf4j.MDC;
5254
import tyler.efm.latest.services.schema.authenticaterequest.AuthenticateRequestType;
5355
import tyler.efm.latest.services.schema.authenticateresponse.AuthenticateResponseType;
5456
import tyler.efm.wsdl.webservicesprofile_implementation_4_0.FilingReviewMDEService;
@@ -162,9 +164,9 @@ public static InputStream getCodesZip(String toRead, String authHeader) throws I
162164
}
163165
}
164166

165-
private Duration downloads = Duration.ZERO;
166-
private Duration updates = Duration.ZERO;
167-
private Duration soaps = Duration.ZERO;
167+
private Duration downloadDuration = Duration.ZERO;
168+
private Duration updateDuration = Duration.ZERO;
169+
private Duration soapDuration = Duration.ZERO;
168170

169171
/**
170172
* @param toRead
@@ -177,14 +179,16 @@ private boolean downloadAndProcessZip(
177179
Instant startTable = Instant.now(Clock.systemUTC());
178180
try (InputStream urlStream = getCodesZip(toRead, signedTime)) {
179181
// Write out the zip file
180-
downloads = downloads.plus(Duration.between(startTable, Instant.now(Clock.systemUTC())));
182+
downloadDuration =
183+
downloadDuration.plus(Duration.between(startTable, Instant.now(Clock.systemUTC())));
181184

182185
ZipInputStream zip = new ZipInputStream(urlStream);
183186
zip.getNextEntry();
184187

185188
Instant updateTableLoc = Instant.now(Clock.systemUTC());
186189
boolean success = process.apply(zip);
187-
updates = updates.plus(Duration.between(updateTableLoc, Instant.now(Clock.systemUTC())));
190+
updateDuration =
191+
updateDuration.plus(Duration.between(updateTableLoc, Instant.now(Clock.systemUTC())));
188192
zip.close();
189193
return success;
190194
} catch (IOException ex) {
@@ -197,6 +201,7 @@ private boolean downloadAndProcessZip(
197201

198202
private boolean downloadSystemTables(String baseUrl, CodeDatabase cd, HeaderSigner signer)
199203
throws SQLException, IOException, JAXBException {
204+
MDC.put(MDCWrappers.SESSION_ID, "system");
200205
Map<String, String> codeUrls =
201206
Map.of(
202207
"version",
@@ -208,11 +213,14 @@ private boolean downloadSystemTables(String baseUrl, CodeDatabase cd, HeaderSign
208213
"/CodeService/codes/error");
209214

210215
for (Map.Entry<String, String> urlSuffix : codeUrls.entrySet()) {
216+
MDC.put(MDCWrappers.REQUEST_ID, urlSuffix.getKey());
211217
cd.createTableIfAbsent(urlSuffix.getKey());
212218
}
213219

220+
MDC.put(MDCWrappers.REQUEST_ID, "installedversion");
214221
// On first download, there won't be installed versions of things yet.
215222
cd.createTableIfAbsent("installedversion");
223+
MDC.remove(MDCWrappers.REQUEST_ID);
216224

217225
cd.commit();
218226

@@ -229,6 +237,7 @@ private boolean downloadSystemTables(String baseUrl, CodeDatabase cd, HeaderSign
229237
cd.deleteFromTable(urlSuffix.getKey());
230238
final Function<InputStream, Boolean> process =
231239
(is) -> {
240+
MDC.put(MDCWrappers.REQUEST_ID, urlSuffix.getKey());
232241
try {
233242
// "0" is used as the system court location.
234243
cd.updateTable(urlSuffix.getKey(), "0", is);
@@ -240,12 +249,14 @@ private boolean downloadSystemTables(String baseUrl, CodeDatabase cd, HeaderSign
240249
};
241250
boolean updateSuccess =
242251
downloadAndProcessZip(baseUrl + urlSuffix.getValue(), signedTime.get(), process);
252+
MDC.remove(MDCWrappers.REQUEST_ID);
243253
if (!updateSuccess) {
244254
cd.rollback(sp);
245255
return false;
246256
}
247257
}
248258
cd.commit();
259+
MDC.remove(MDCWrappers.SESSION_ID);
249260
return true;
250261
}
251262

@@ -335,6 +346,7 @@ private boolean downloadCourtTables(
335346
CourtPolicyResponseMessageType policyResp,
336347
String baseUrl)
337348
throws JAXBException, IOException, SQLException {
349+
MDC.put(MDCWrappers.SESSION_ID, location);
338350
log.info("Doing updates for: {}, tables: {}", location, tables);
339351
Instant downloadStart = Instant.now(Clock.systemUTC());
340352
// TODO(brycew-later): check that the effective date is later than today
@@ -366,15 +378,19 @@ private boolean downloadCourtTables(
366378
Optional<String> signedTime = signer.signedCurrentTime();
367379
if (signedTime.isEmpty()) {
368380
log.error("Couldn't get signed time to download codes, skipping all");
381+
MDC.remove(MDCWrappers.SESSION_ID);
369382
return false;
370383
}
371384
Map<String, DownloadedCodes> downloaded =
372385
streamDownload(signedTime.get(), location, toDownload.parallel(), tables);
373-
downloads = downloads.plus(Duration.between(downloadStart, Instant.now(Clock.systemUTC())));
374-
log.info("Location: {}: Downloads took: {}", location, downloads);
386+
var downloadInc = Duration.between(downloadStart, Instant.now(Clock.systemUTC()));
387+
downloadDuration = downloadDuration.plus(downloadInc);
388+
log.info(
389+
"Location: {}: Downloads took: {} (total: {})", location, downloadInc, downloadDuration);
375390

376391
Instant updateStart = Instant.now(Clock.systemUTC());
377392
for (DownloadedCodes down : downloaded.values()) {
393+
MDC.put(MDCWrappers.REQUEST_ID, down.tableName());
378394
try {
379395
cd.updateTable(down.tableName(), down.location(), down.xsr());
380396
} catch (Exception ex) {
@@ -384,10 +400,13 @@ private boolean downloadCourtTables(
384400
down.input().close();
385401
}
386402
}
387-
updates = updates.plus(Duration.between(updateStart, Instant.now(Clock.systemUTC())));
403+
var updateInc = Duration.between(updateStart, Instant.now(Clock.systemUTC()));
404+
updateDuration = updateDuration.plus(updateInc);
388405

389406
cd.commit();
390-
log.info("Location: {}: updates took: {}", location, updates);
407+
log.info("Location: {}: updates took: {} (total: {})", location, updateInc, updateDuration);
408+
MDC.remove(MDCWrappers.REQUEST_ID);
409+
MDC.remove(MDCWrappers.SESSION_ID);
391410
return true;
392411
}
393412

@@ -436,7 +455,8 @@ public boolean updateAll(String baseUrl, FilingReviewMDEPort filingPort, CodeDat
436455
// Will ignore tables that don't exist.
437456
cd.deleteFromTable(table, courtLocation);
438457

439-
updates = updates.plus(Duration.between(deleteFromTable, Instant.now(Clock.systemUTC())));
458+
updateDuration =
459+
updateDuration.plus(Duration.between(deleteFromTable, Instant.now(Clock.systemUTC())));
440460
}
441461
}
442462
log.info(
@@ -445,8 +465,9 @@ public boolean updateAll(String baseUrl, FilingReviewMDEPort filingPort, CodeDat
445465
Instant startPolicy = Instant.now(Clock.systemUTC());
446466
Map<String, CourtPolicyResponseMessageType> policies =
447467
streamPolicies(versionsToUpdate.keySet().stream().parallel(), cd.getDomain(), filingPort);
448-
soaps = soaps.plus(Duration.between(startPolicy, Instant.now(Clock.systemUTC())));
449-
log.info("Soaps: {}", soaps);
468+
var soapInc = Duration.between(startPolicy, Instant.now(Clock.systemUTC()));
469+
soapDuration = soapDuration.plus(soapInc);
470+
log.info("Soaps took: {} (total: {})", soapInc, soapDuration);
450471

451472
for (var policy : policies.entrySet()) {
452473
final String courtLocation = policy.getKey();
@@ -486,9 +507,9 @@ public boolean replaceSome(
486507
}
487508
cd.commit();
488509

489-
downloads = Duration.ZERO;
490-
soaps = Duration.ZERO;
491-
updates = Duration.ZERO;
510+
downloadDuration = Duration.ZERO;
511+
soapDuration = Duration.ZERO;
512+
updateDuration = Duration.ZERO;
492513
if (locs.isEmpty()) {
493514
locs = cd.getAllLocations();
494515
}
@@ -497,15 +518,19 @@ public boolean replaceSome(
497518
Instant startPolicy = Instant.now(Clock.systemUTC());
498519
Map<String, CourtPolicyResponseMessageType> policies =
499520
streamPolicies(locs.stream(), cd.getDomain(), filingPort);
500-
soaps = soaps.plus(Duration.between(startPolicy, Instant.now(Clock.systemUTC())));
501-
log.info("Soaps: {}", soaps);
521+
soapDuration = soapDuration.plus(Duration.between(startPolicy, Instant.now(Clock.systemUTC())));
522+
log.info("Soaps: {}", soapDuration);
502523
for (var policy : policies.entrySet()) {
503524
final String location = policy.getKey();
504525
log.info("Downloading tables for {}", location);
505526
success &=
506527
downloadCourtTables(location, Optional.empty(), cd, signer, policy.getValue(), baseUrl);
507528
}
508-
log.info("Downloads took: {}, and updates took: {}, soaps took: {}", downloads, updates, soaps);
529+
log.info(
530+
"Downloads took: {}, updates took: {}, soaps took: {}",
531+
downloadDuration,
532+
updateDuration,
533+
soapDuration);
509534
cd.commit();
510535
cd.setAutoCommit(true);
511536
cd.vacuumAll();
@@ -633,6 +658,9 @@ public static void main(String[] args) throws Exception {
633658
List<String> jurisdictions = List.of(System.getenv("TYLER_JURISDICTIONS").split(" "));
634659
var env = TylerEnv.parse(System.getenv("TYLER_ENV"));
635660
for (String jurisdiction : jurisdictions) {
661+
// Reusing USER for Jurisdiction, SESSION for the court / location, and REQUEST for the table
662+
// name.
663+
MDC.put(MDCWrappers.USER_ID, jurisdiction);
636664
try (Connection conn = ds.getConnection()) {
637665
executeCommand(
638666
new CodeDatabase(jurisdiction, env, conn),

proxyserver/src/main/java/edu/suffolk/litlab/efsp/server/utils/UpdateCodeVersions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
5858
log.error("Couldn't connect to Codes db from Job Executor: ", e);
5959
success = false;
6060
}
61+
log.info("Finished code updates with status: {}", success);
6162
if (!success) {
6263
Monitor.sendImmediateErrorNotification(
6364
"UpdateCodeVersions error on " + Monitor.EXTERNAL_DOMAIN,
@@ -72,5 +73,6 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
7273
"error_timestamp",
7374
LocalDate.now().toString()));
7475
}
76+
MDCWrappers.removeAllMDCs();
7577
}
7678
}

0 commit comments

Comments
 (0)