Skip to content

Commit

Permalink
Log config name in VFS error flow
Browse files Browse the repository at this point in the history
Related issue: wso2/api-manager#1945
  • Loading branch information
dulanjalidilmi committed Aug 31, 2023
1 parent b77eaa7 commit 54cb6eb
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public String getFileURI() {
} catch (UnknownHostException | FileSystemException e) {
String message = "Unable to resolve the hostname of transport.vfs.FileURI : " +
VFSUtils.maskURLPassword(fileURI);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, e);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e);
}
}
return fileURI;
Expand All @@ -173,7 +173,7 @@ public String getReplyFileURI() {
} catch (UnknownHostException | FileSystemException e) {
String message = "Unable to resolve the hostname of transport.vfs.ReplyFileURI : " +
VFSUtils.maskURLPassword(replyFileURI);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, e);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e);
}
}
return replyFileURI;
Expand Down Expand Up @@ -210,7 +210,7 @@ public String getMoveAfterProcess() {
} catch (UnknownHostException | FileSystemException e) {
String message = "Unable to resolve the hostname of transport.vfs.MoveAfterProcess: " +
VFSUtils.maskURLPassword(moveAfterProcess);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, e);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e);
}
}
return moveAfterProcess;
Expand All @@ -223,7 +223,7 @@ public String getMoveAfterMoveFailure() {
} catch (UnknownHostException | FileSystemException e) {
String message = "Unable to resolve the hostname of transport.vfs.MoveAfterFailedMove: " +
VFSUtils.maskURLPassword(moveAfterMoveFailure);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, e);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e);
}
}
return moveAfterMoveFailure;
Expand Down Expand Up @@ -295,7 +295,7 @@ public String getMoveAfterFailure() {
} catch (UnknownHostException | FileSystemException e) {
String message = "Unable to resolve the hostname of transport.vfs.MoveAfterFailure: " +
VFSUtils.maskURLPassword(moveAfterFailure);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, e);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, getServiceName(), e);
}
}
return moveAfterFailure;
Expand Down Expand Up @@ -509,7 +509,6 @@ public void setSubfolderTimestamp(String subfolderTimestamp) {

@Override
public boolean loadConfiguration(ParameterInclude params) throws AxisFault {

decryptParamsIfRequired(params);
this.params = params;
resolveHostsDynamically = ParamUtils.getOptionalParamBoolean(params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public static void logException(Log log, LogType type, String message, Exception
}
}

/**
* This method is used to log exceptions with exception
* @param log Log
* @param type {@link LogType}
* @param message String message to be logged
* @param configName String name of the configuration
* @param e Exception
*/
public static void logException(Log log, LogType type, String message, String configName, Exception e) {
message = constructLogMessage(message, configName);
logException(log, type, message, e);
}

/**
* This method is used to log exceptions without exception
* @param log Log
Expand All @@ -80,6 +93,18 @@ public static void logException(Log log, LogType type, String message) {
}
}

/**
* This method is used to log exceptions without exception
* @param log Log
* @param type {@link LogType}
* @param message String message to be logged
* @param configName String name of the configuration
*/
public static void logException(Log log, LogType type, String message, String configName) {
message = constructLogMessage(message, configName);
logException(log, type, message);
}

/**
* This method is used to handle exceptions. Log error message and throws an AxisFault with the exception
* @param log Log
Expand All @@ -92,6 +117,19 @@ public static void handleException(Log log, String message, Exception e) throws
throw new AxisFault(message, e);
}

/**
* This method is used to handle exceptions. Log error message and throws an AxisFault with the exception
* @param log Log
* @param message String message to be logged
* @param configName String name of the configuration
* @param e Exception
* @throws AxisFault
*/
public static void handleException(Log log, String message, String configName, Exception e) throws AxisFault {
logException(log, LogType.ERROR, message, configName, e);
throw new AxisFault(message, e);
}

/**
* This method is used to handle exceptions. Log error message and throws an AxisFault
* @param log Log
Expand All @@ -103,6 +141,18 @@ public static void handleException(Log log, String message) throws AxisFault {
throw new AxisFault(message);
}

/**
* This method is used to handle exceptions. Log error message and throws an AxisFault
* @param log Log
* @param message String message to be logged
* @param configName String name of the configuration
* @throws AxisFault
*/
public static void handleException(Log log, String message, String configName) throws AxisFault {
logException(log, LogType.ERROR, message, configName);
throw new AxisFault(message);
}

/**
* This method is used to handle print the stack trace
* @param e InterruptedException
Expand All @@ -129,4 +179,17 @@ public enum LogType {
ERROR,
FATAL
}

/**
* This method is used to construct the log message
* @param message String message to be logged
* @param configName String name of the configuration
* @return String constructed log message
*/
public static String constructLogMessage(String message, String configName) {
if (null == configName || configName.trim().isEmpty()) {
return message;
}
return "[Service: ".concat(configName).concat("] - ").concat(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ protected void poll(PollTableEntry entry) {
* @param fileURI the file or directory to be scanned
*/
protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
String serviceName = entry.getServiceName();
if (log.isDebugEnabled()) {
log.debug("Polling: " + VFSUtils.maskURLPassword(fileURI));
}
Expand Down Expand Up @@ -225,7 +226,7 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
fso = VFSUtils.attachFileSystemOptions(entry.getVfsSchemeProperties(), getFsManager());
} catch (Exception e) {
VFSTransportErrorHandler.logException(log, LogType.ERROR,
"Error while attaching VFS file system properties. ", e);
"Error while attaching VFS file system properties. ", serviceName, e);
}

FileObject fileObject = null;
Expand Down Expand Up @@ -268,10 +269,11 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
VFSUtils.maskURLPassword(fileURI) + ", in attempt " + retryCount +
", " + e.getMessage() + " Retrying in " + reconnectionTimeout +
" milliseconds.";
VFSTransportErrorHandler.logException(log, LogType.WARN, message);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, serviceName);
}
} catch (Exception e) {
VFSTransportErrorHandler.logException(log, LogType.WARN, "Runtime error may have occurred. ", e);
VFSTransportErrorHandler.logException(log, LogType.WARN, "Runtime error may have occurred. ",
serviceName, e);
closeCachedFileSystem(fileURI, fso);
}

Expand All @@ -281,7 +283,7 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
VFSTransportErrorHandler.logException(log, LogType.ERROR,
"Thread was interrupted while waiting to reconnect.", e2);
"Thread was interrupted while waiting to reconnect.", serviceName, e2);
}
}
}
Expand All @@ -297,7 +299,7 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
} catch (FileNotFolderException ignored) {
} catch (FileSystemException ex) {
closeFileSystem(fileObject);
VFSTransportErrorHandler.logException(log, LogType.ERROR, ex.getMessage(), ex);
VFSTransportErrorHandler.logException(log, LogType.ERROR, ex.getMessage(), serviceName, ex);
}

// if this is a file that would translate to a single message
Expand Down Expand Up @@ -330,12 +332,12 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
String message = "Error processing File URI : " +
VFSUtils.maskURLPassword(fileObject.getName().toString()) +
". This can be due to file moved from another process.";
VFSTransportErrorHandler.logException(log, LogType.WARN, message);
VFSTransportErrorHandler.logException(log, LogType.WARN, message, serviceName);
runPostProcess = false;
} else {
String message = "Error processing File URI : " +
VFSUtils.maskURLPassword(fileObject.getName().getURI());
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, e);
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName, e);
entry.setLastPollState(PollTableEntry.FAILED);
metrics.incrementFaultsReceiving();
}
Expand All @@ -348,7 +350,8 @@ protected void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {
String message = "File object '" +
VFSUtils.maskURLPassword(fileObject.getURL().toString()) +
"' " + "cloud not be moved";
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, axisFault);
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName,
axisFault);
entry.setLastPollState(PollTableEntry.FAILED);
String timeStamp = VFSUtils.getSystemTime(entry.getFailedRecordTimestampFormat());
addFailedRecord(entry, fileObject, timeStamp);
Expand Down Expand Up @@ -671,6 +674,7 @@ protected boolean acquireLock(FileSystemManager fsManager, FileObject fileObject
protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObject fileObject, FileSystemOptions fso)
throws AxisFault {

String serviceName = entry.getServiceName();
String moveToDirectoryURI = null;
try {
switch (entry.getLastPollState()) {
Expand Down Expand Up @@ -721,7 +725,7 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec
VFSUtils.parseSchemeFileOptions(moveToDirectoryURI, entry.getParams()), getFsManager());
} catch (Exception e) {
VFSTransportErrorHandler.logException(log, LogType.WARN,
"Unable to set options for processed file location ", e);
"Unable to set options for processed file location ", serviceName, e);
}
FileObject moveToDirectory = getFsManager().resolveFile(moveToDirectoryURI, destinationFSO);
String prefix;
Expand Down Expand Up @@ -749,7 +753,7 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec
closeFileSystem(fileObject);
String message = "Error moving file : " + VFSUtils.maskURLPassword(fileObject.toString()) + " to " +
VFSUtils.maskURLPassword(moveToDirectoryURI);
VFSTransportErrorHandler.handleException(log, message, e);
VFSTransportErrorHandler.handleException(log, message, serviceName, e);
}finally{
try {
fileObject.close();
Expand All @@ -767,12 +771,13 @@ protected void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObjec
if (!fileObject.delete()) {
String msg = "Cannot delete file : "
+ VFSUtils.maskURLPassword(fileObject.toString());
VFSTransportErrorHandler.handleException(log, msg);
VFSTransportErrorHandler.handleException(log, msg, serviceName);
}
} catch (FileSystemException e) {
closeFileSystem(fileObject);
log.error("Error deleting file : "
+ VFSUtils.maskURLPassword(fileObject.toString()), e);
String msg = "Error deleting file : "
+ VFSUtils.maskURLPassword(fileObject.toString());
VFSTransportErrorHandler.handleException(log, msg, serviceName, e);
}
}

Expand Down Expand Up @@ -985,6 +990,7 @@ private Properties generateSecureVaultProperties(TransportInDescription inDescri
protected synchronized void addFailedRecord(PollTableEntry pollTableEntry,
FileObject failedObject,
String timeString) {
String serviceName = pollTableEntry.getServiceName();
try {
String record = failedObject.getName().getBaseName() + VFSConstants.FAILED_RECORD_DELIMITER
+ timeString;
Expand All @@ -1007,7 +1013,8 @@ protected synchronized void addFailedRecord(PollTableEntry pollTableEntry,
FileUtils.writeLines(failedRecordFile, content);
}
} catch (IOException e) {
VFSTransportErrorHandler.logException(log, LogType.FATAL, "Failure while writing the failed records!", e);
VFSTransportErrorHandler.logException(log, LogType.FATAL,
"Failure while writing the failed records!", serviceName, e);
}
}

Expand Down Expand Up @@ -1070,6 +1077,7 @@ public void run() {
boolean isDeletionSucceed = false;
int nextRetryDuration = pollTableEntry.getNextRetryDuration();
int count = 0;
String serviceName = pollTableEntry.getServiceName();
while (!isDeletionSucceed) {
try {
reTryFailedMove(pollTableEntry, failedFileObject, fileSystemOptions);
Expand All @@ -1082,11 +1090,11 @@ public void run() {
+ VFSUtils.maskURLPassword(failedFileObject.getURL().toString())
+ "', next re-try will be " +"after '"
+ nextRetryDuration + "' milliseconds";
VFSTransportErrorHandler.logException(log, LogType.ERROR, message);
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName);
} catch (FileSystemException e) {
String message = "Error while retrying the file url of the file object '" +
VFSUtils.maskURLPassword(failedFileObject.toString()) + "'";
VFSTransportErrorHandler.logException(log, LogType.ERROR, message);
VFSTransportErrorHandler.logException(log, LogType.ERROR, message, serviceName);
}
try {
Thread.sleep(nextRetryDuration);
Expand All @@ -1099,6 +1107,7 @@ public void run() {

private synchronized void reTryFailedMove(PollTableEntry entry, FileObject fileObject, FileSystemOptions fso)
throws AxisFault {
String serviceName = entry.getServiceName();
try {

String moveToDirectoryURI = entry.getMoveAfterMoveFailure();
Expand All @@ -1124,14 +1133,14 @@ private synchronized void reTryFailedMove(PollTableEntry entry, FileObject fileO
closeFileSystem(fileObject);
String message = "Error moving the failed file : "
+ VFSUtils.maskURLPassword(fileObject.toString()) + " to " + moveToDirectoryURI;
VFSTransportErrorHandler.handleException(log, message, e);
VFSTransportErrorHandler.handleException(log, message, serviceName, e);
}
} catch (FileSystemException e) {
String message = "Cloud not move the failed file object '"
+ VFSUtils.maskURLPassword(fileObject.toString()) + "'";
VFSTransportErrorHandler.handleException(log, message, e);
VFSTransportErrorHandler.handleException(log, message, serviceName, e);
} catch (IOException e) {
VFSTransportErrorHandler.handleException(log, "Cloud not create the folder", e);
VFSTransportErrorHandler.handleException(log, "Cloud not create the folder", serviceName, e);
}
}
}
Expand Down
Loading

0 comments on commit 54cb6eb

Please sign in to comment.