Skip to content
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

refactor(s3stream): show advices in error msg #841

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public DefaultS3Operator(String endpoint, String region, String bucket, boolean
.setSecretKey(secretKey)
.build();
LOGGER.info("You are using s3Context: {}", s3Context);
checkAvailable();
checkAvailable(s3Context);
}

public static Builder builder() {
Expand Down Expand Up @@ -575,7 +575,7 @@ private void checkConfig() {
}
}

private void checkAvailable() {
private void checkAvailable(S3Utils.S3Context s3Context) {
byte[] content = new Date().toString().getBytes(StandardCharsets.UTF_8);
String path = String.format("check_available/%d", System.nanoTime());
String multipartPath = String.format("check_available_multipart/%d", System.nanoTime());
Expand All @@ -594,8 +594,13 @@ private void checkAvailable() {
read.release();
this.delete(multipartPath).get(30, TimeUnit.SECONDS);
} catch (Throwable e) {
LOGGER.error("Try to write/read/delete object to S3 fail ", e);
throw new RuntimeException("Try connect s3 fail, please re-check the server configs", e);
LOGGER.error("Failed to write/read/delete object on S3 ", e);
String exceptionMsg = String.format("Failed to write/read/delete object on S3. You are using s3Context: %s.", s3Context);
List<String> advices = s3Context.advices();
if (!advices.isEmpty()) {
exceptionMsg += "\nHere are some advices: \n" + String.join("\n", advices);
}
throw new RuntimeException(exceptionMsg, e);
}
}

Expand Down
39 changes: 39 additions & 0 deletions s3stream/src/main/java/com/automq/stream/utils/S3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -412,6 +413,44 @@ public S3Context(String endpoint, String accessKey, String secretKey, String buc
this.forcePathStyle = forcePathStyle;
}

public List<String> advices() {
List<String> advises = new ArrayList<>();
if (StringUtils.isBlank(bucketName)) {
advises.add("bucketName is blank. Please supply a valid bucketName.");
}
if (StringUtils.isBlank(endpoint)) {
advises.add("endpoint is blank. Please supply a valid endpoint.");
} else {
if (endpoint.startsWith("https")) {
advises.add("You are using https endpoint. Please make sure your object storage service supports https.");
}
String[] splits = endpoint.split("//");
if (splits.length < 2) {
advises.add("endpoint is invalid. Please supply a valid endpoint.");
} else {
String[] dotSplits = splits[1].split("\\.");
if (dotSplits.length == 0 || StringUtils.isBlank(dotSplits[0])) {
advises.add("endpoint is invalid. Please supply a valid endpoint.");
} else if (!StringUtils.isBlank(bucketName) && Objects.equals(bucketName.toLowerCase(), dotSplits[0].toLowerCase())) {
advises.add("bucket name should not be included in endpoint.");
}
}
}
if (StringUtils.isBlank(accessKey)) {
advises.add("accessKey is blank. Please supply a valid accessKey.");
}
if (StringUtils.isBlank(secretKey)) {
advises.add("secretKey is blank. Please supply a valid secretKey.");
}
if (StringUtils.isBlank(region)) {
advises.add("region is blank. Please supply a valid region.");
}
if (!forcePathStyle) {
advises.add("forcePathStyle is set as false. Please set it as true if you are using minio.");
}
return advises;
}

@Override
public String toString() {
return "S3CheckContext{" +
Expand Down
Loading