-
Notifications
You must be signed in to change notification settings - Fork 8
Feature/jdbc log s3 upload #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9239ca7
d4257ed
9ba88f1
394440d
91f7076
a313e67
42d772e
1d856c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||
|
||||||||||||||||
import com.fasterxml.jackson.annotation.JsonCreator; | ||||||||||||||||
import com.fasterxml.jackson.annotation.JsonValue; | ||||||||||||||||
import java.io.File; | ||||||||||||||||
import java.io.IOException; | ||||||||||||||||
import java.sql.SQLException; | ||||||||||||||||
import java.sql.Types; | ||||||||||||||||
|
@@ -15,6 +16,7 @@ | |||||||||||||||
import org.embulk.config.ConfigSource; | ||||||||||||||||
import org.embulk.config.TaskSource; | ||||||||||||||||
import org.embulk.output.jdbc.*; | ||||||||||||||||
import org.embulk.output.s3.JdbcLogUploader; | ||||||||||||||||
import org.embulk.output.snowflake.PrivateKeyReader; | ||||||||||||||||
import org.embulk.output.snowflake.SnowflakeCopyBatchInsert; | ||||||||||||||||
import org.embulk.output.snowflake.SnowflakeOutputConnection; | ||||||||||||||||
|
@@ -91,6 +93,30 @@ public interface SnowflakePluginTask extends PluginTask { | |||||||||||||||
@ConfigDefault("\"none\"") | ||||||||||||||||
public MatchByColumnName getMatchByColumnName(); | ||||||||||||||||
|
||||||||||||||||
@Config("upload_jdbc_log_to_s3") | ||||||||||||||||
@ConfigDefault("false") | ||||||||||||||||
public boolean getUploadJdbcLogToS3(); | ||||||||||||||||
|
||||||||||||||||
@Config("s3_bucket") | ||||||||||||||||
@ConfigDefault("null") | ||||||||||||||||
public Optional<String> getS3Bucket(); | ||||||||||||||||
|
||||||||||||||||
@Config("s3_prefix") | ||||||||||||||||
@ConfigDefault("null") | ||||||||||||||||
public Optional<String> getS3Prefix(); | ||||||||||||||||
|
||||||||||||||||
@Config("s3_region") | ||||||||||||||||
@ConfigDefault("null") | ||||||||||||||||
public Optional<String> getS3Region(); | ||||||||||||||||
|
||||||||||||||||
@Config("s3_access_key_id") | ||||||||||||||||
@ConfigDefault("null") | ||||||||||||||||
public Optional<String> getS3AccessKeyId(); | ||||||||||||||||
|
||||||||||||||||
@Config("s3_secret_access_key") | ||||||||||||||||
@ConfigDefault("null") | ||||||||||||||||
public Optional<String> getS3SecretAccessKey(); | ||||||||||||||||
|
||||||||||||||||
public void setCopyIntoTableColumnNames(String[] columnNames); | ||||||||||||||||
|
||||||||||||||||
public String[] getCopyIntoTableColumnNames(); | ||||||||||||||||
|
@@ -139,6 +165,9 @@ public static MatchByColumnName fromString(String value) { | |||||||||||||||
private static final int MASTER_TOKEN_INVALID_GS_CODE = 390115; | ||||||||||||||||
private static final int ID_TOKEN_INVALID_LOGIN_REQUEST_GS_CODE = 390195; | ||||||||||||||||
|
||||||||||||||||
private static final String ENCOUNTERED_COMMUNICATION_ERROR_MESSAGE = | ||||||||||||||||
"JDBC driver encountered communication error"; | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
protected Class<? extends PluginTask> getTaskClass() { | ||||||||||||||||
return SnowflakePluginTask.class; | ||||||||||||||||
|
@@ -198,6 +227,10 @@ protected JdbcOutputConnector getConnector(PluginTask task, boolean retryableMet | |||||||||||||||
props.setProperty("CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX", "true"); | ||||||||||||||||
props.setProperty("MULTI_STATEMENT_COUNT", "0"); | ||||||||||||||||
|
||||||||||||||||
if (t.getUploadJdbcLogToS3()) { | ||||||||||||||||
props.setProperty("tracing", "ALL"); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
props.putAll(t.getOptions()); | ||||||||||||||||
|
||||||||||||||||
logConnectionProperties(url, props); | ||||||||||||||||
|
@@ -217,11 +250,53 @@ public ConfigDiff transaction( | |||||||||||||||
try { | ||||||||||||||||
snowflakeCon = (SnowflakeOutputConnection) getConnector(task, true).connect(true); | ||||||||||||||||
snowflakeCon.runCreateStage(stageIdentifier); | ||||||||||||||||
|
||||||||||||||||
configDiff = super.transaction(config, schema, taskCount, control); | ||||||||||||||||
if (t.getDeleteStage()) { | ||||||||||||||||
runDropStageWithRecovery(snowflakeCon, stageIdentifier, task); | ||||||||||||||||
} | ||||||||||||||||
} catch (Exception e) { | ||||||||||||||||
if (e instanceof SQLException) { | ||||||||||||||||
String message = e.getMessage(); | ||||||||||||||||
if (message != null | ||||||||||||||||
&& message.contains(ENCOUNTERED_COMMUNICATION_ERROR_MESSAGE) | ||||||||||||||||
Comment on lines
+259
to
+262
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Relying on message text can be brittle; consider checking the SQL error code or vendor-specific code instead of string matching to detect communication errors more reliably.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
&& t.getUploadJdbcLogToS3() == true) { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||
final Optional<String> s3Bucket = t.getS3Bucket(); | ||||||||||||||||
final Optional<String> s3Prefix = t.getS3Prefix(); | ||||||||||||||||
final Optional<String> s3Region = t.getS3Region(); | ||||||||||||||||
final Optional<String> s3AccessKeyId = t.getS3AccessKeyId(); | ||||||||||||||||
final Optional<String> s3SecretAccessKey = t.getS3SecretAccessKey(); | ||||||||||||||||
if (!s3Bucket.isPresent() || !s3Region.isPresent()) { | ||||||||||||||||
logger.warn("s3_bucket, and s3_region must be set when upload_jdbc_log_to_s3 is true"); | ||||||||||||||||
kentoyoshida marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} else if (s3AccessKeyId.isPresent() != s3SecretAccessKey.isPresent()) { | ||||||||||||||||
logger.warn( | ||||||||||||||||
"Both s3_access_key_id and s3_secret_access_key must be set together or omitted."); | ||||||||||||||||
} else { | ||||||||||||||||
try (JdbcLogUploader jdbcLogUploader = | ||||||||||||||||
new JdbcLogUploader( | ||||||||||||||||
s3Bucket.get(), | ||||||||||||||||
s3Prefix.orElse(""), | ||||||||||||||||
s3Region.get(), | ||||||||||||||||
s3AccessKeyId.orElse(null), | ||||||||||||||||
s3SecretAccessKey.orElse(null))) { | ||||||||||||||||
String tmpDir = System.getProperty("java.io.tmpdir", "/tmp"); | ||||||||||||||||
File logDir = new File(tmpDir); | ||||||||||||||||
File[] logFiles = logDir.listFiles((dir, name) -> name.startsWith("snowflake_jdbc")); | ||||||||||||||||
if (logFiles != null && logFiles.length > 0) { | ||||||||||||||||
Optional<File> latest = | ||||||||||||||||
Arrays.stream(logFiles).max(Comparator.comparingLong(File::lastModified)); | ||||||||||||||||
if (latest.isPresent()) { | ||||||||||||||||
jdbcLogUploader.uploadIfExists(latest.get()); | ||||||||||||||||
} | ||||||||||||||||
} else { | ||||||||||||||||
logger.warn("No snowflake_jdbc*.log file found in {} for upload", tmpDir); | ||||||||||||||||
} | ||||||||||||||||
} catch (Exception uploadException) { | ||||||||||||||||
logger.warn("Failed to upload JDBC log to S3: {}", uploadException.getMessage()); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
if (t.getDeleteStage() && t.getDeleteStageOnError()) { | ||||||||||||||||
try { | ||||||||||||||||
runDropStageWithRecovery(snowflakeCon, stageIdentifier, task); | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.embulk.output.s3; | ||
|
||
import java.io.File; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
public class JdbcLogUploader implements AutoCloseable { | ||
private final Logger logger = LoggerFactory.getLogger(JdbcLogUploader.class); | ||
|
||
private final S3Client s3Client; | ||
private final String bucket; | ||
private final String prefix; | ||
private final Region region; | ||
|
||
public JdbcLogUploader( | ||
String bucket, String prefix, String region, String accessKeyId, String secretAccessKey) { | ||
this.bucket = bucket; | ||
this.prefix = prefix; | ||
this.region = Region.of(region); | ||
|
||
if (accessKeyId != null && secretAccessKey != null) { | ||
// Use explicit credentials | ||
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKeyId, secretAccessKey); | ||
this.s3Client = | ||
S3Client.builder() | ||
.region(this.region) | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCreds)) | ||
.build(); | ||
} else { | ||
// Use default credentials provider (IAM role, environment variables, etc.) | ||
this.s3Client = | ||
S3Client.builder() | ||
.region(this.region) | ||
.credentialsProvider(DefaultCredentialsProvider.create()) | ||
.build(); | ||
} | ||
} | ||
|
||
public void uploadIfExists(File file) { | ||
if (!file.exists()) { | ||
logger.warn("File not found: {}", file.getAbsolutePath()); | ||
return; | ||
} | ||
|
||
// Add timestamp to filename | ||
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); | ||
String originalFileName = file.getName(); | ||
String fileNameWithTimestamp; | ||
|
||
// Insert timestamp before file extension | ||
int lastDotIndex = originalFileName.lastIndexOf('.'); | ||
if (lastDotIndex > 0) { | ||
String nameWithoutExt = originalFileName.substring(0, lastDotIndex); | ||
String extension = originalFileName.substring(lastDotIndex); | ||
fileNameWithTimestamp = nameWithoutExt + "_" + timestamp + extension; | ||
} else { | ||
fileNameWithTimestamp = originalFileName + "_" + timestamp; | ||
} | ||
|
||
String key = prefix.isEmpty() ? fileNameWithTimestamp : prefix + "/" + fileNameWithTimestamp; | ||
try { | ||
PutObjectRequest putObjectRequest = | ||
PutObjectRequest.builder().bucket(bucket).key(key).build(); | ||
|
||
s3Client.putObject(putObjectRequest, RequestBody.fromFile(file)); | ||
logger.info("Uploaded {}", file.getAbsolutePath()); | ||
} catch (Exception e) { | ||
logger.error("Failed to upload {}", file.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (s3Client != null) { | ||
s3Client.close(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref: https://github.com/embulk/embulk-output-s3
(nits)
s3_prefix
could bes3_path_prefix
like embulk-output-s3