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

Merge: 3.5.x->master #1356

Merged
merged 16 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
04c3bdd
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 21, 2022
a6b5589
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 21, 2022
ce313b3
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 21, 2022
607da5c
perf: 脚本列表,脚本语言的排序需要按照语言名称的字典顺序 #1317
wangyu096 Sep 21, 2022
e91ca6a
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 21, 2022
444c069
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 21, 2022
6e06364
perf: 脚本列表,脚本语言的排序需要按照语言名称的字典顺序 #1317
wangyu096 Sep 22, 2022
66586a5
Merge pull request #1348 from wangyu096/3.5.x
jsonwan Sep 22, 2022
ea2a913
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 22, 2022
49da5a2
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 22, 2022
890e661
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 22, 2022
b594585
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 22, 2022
e514d75
bugfix: 长时间运行后,file-worker下载第三方文件源文件失败 #1346
jsonwan Sep 22, 2022
eac89fc
Merge pull request #1349 from hLinx/3.5.x
hLinx Sep 22, 2022
953107b
Merge pull request #1347 from jsonwan/github_fix/third_file
wangyu096 Sep 22, 2022
330923f
Merge pull request #1353 from hLinx/3.5.x
hLinx Sep 22, 2022
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 @@ -70,6 +70,7 @@
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.message.BasicHeader;
Expand Down Expand Up @@ -237,7 +238,7 @@ private <R> R getArtifactoryRespByReq(
url = getCompleteUrl(url);
String reqStr = "{}";
if (reqBody != null) {
reqStr = JsonUtils.toJsonWithoutSkippedFields(reqBody);
reqStr = JsonUtils.toJson(reqBody);
}
String respStr = null;
long start = System.nanoTime();
Expand Down Expand Up @@ -402,13 +403,13 @@ public NodeDTO getFileNode(String filePath) {
return nodeDTO;
}

public Pair<InputStream, Long> getFileInputStream(String filePath) throws ServiceException {
public Pair<InputStream, HttpRequestBase> getFileInputStream(String filePath) throws ServiceException {
List<String> pathList = parsePath(filePath);
return getFileInputStream(pathList.get(0), pathList.get(1), pathList.get(2));
}

public Pair<InputStream, Long> getFileInputStream(String projectId, String repoName,
String filePath) throws ServiceException {
public Pair<InputStream, HttpRequestBase> getFileInputStream(String projectId, String repoName,
String filePath) throws ServiceException {
DownloadGenericFileReq req = new DownloadGenericFileReq();
req.setProject(projectId);
req.setRepo(repoName);
Expand All @@ -419,17 +420,10 @@ public Pair<InputStream, Long> getFileInputStream(String projectId, String repoN
try {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.BKREPO_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", "download:" + URL_DOWNLOAD_GENERIC_FILE));
resp = longHttpHelper.getRawResp(false, url, getJsonHeaders());
Header contentLengthHeader = resp.getFirstHeader("Content-Length");
Long contentLength = null;
if (contentLengthHeader != null && StringUtils.isNotBlank(contentLengthHeader.getValue())) {
contentLength = Long.parseLong(contentLengthHeader.getValue());
log.debug("Content-Length from header:{}", contentLengthHeader.getValue());
} else {
log.debug("Content-Length from header is null or blank");
}
Pair<HttpRequestBase, CloseableHttpResponse> pair = longHttpHelper.getRawResp(false, url, getJsonHeaders());
resp = pair.getRight();
if (resp.getStatusLine() != null && resp.getStatusLine().getStatusCode() == 200) {
return Pair.of(resp.getEntity().getContent(), contentLength);
return Pair.of(resp.getEntity().getContent(), pair.getLeft());
} else {
log.info("resp.statusLine={},resp.entity={}", resp.getStatusLine(), resp.getEntity());
throw new InternalException(ErrorCode.FAIL_TO_REQUEST_THIRD_FILE_SOURCE_DOWNLOAD_GENERIC_FILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class JsonUtils {

/**
* 序列化时忽略bean中的某些字段,字段需要使用SkipLogFields注解
* 注意:只对第一层的字段生效,嵌套的不生效
*
* @param bean
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public enum Order {
DESCENDING(0), ASCENDING(1);

private int order;
private final int order;

Order(int order) {
this.order = order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
Expand All @@ -57,7 +58,7 @@ CloseableHttpClient getHttpClient() {
}

@Override
public CloseableHttpResponse getRawResp(boolean keepAlive, String url, Header[] header) {
public Pair<HttpRequestBase, CloseableHttpResponse> getRawResp(boolean keepAlive, String url, Header[] header) {
HttpGet get = new HttpGet(url);
if (keepAlive) {
get.setHeader("Connection", "Keep-Alive");
Expand All @@ -66,10 +67,14 @@ public CloseableHttpResponse getRawResp(boolean keepAlive, String url, Header[]
get.setHeaders(header);
}
try {
return getHttpClient().execute(get);
return Pair.of(get, getHttpClient().execute(get));
} catch (IOException e) {
log.error("Get request fail", e);
throw new InternalException(e, ErrorCode.API_ERROR);
} finally {
if (log.isDebugEnabled()) {
log.debug("getRawResp,url={},headers={}", url, header);
}
}
}

Expand All @@ -82,13 +87,28 @@ public Pair<Integer, String> get(boolean keepAlive, String url, Header[] header)
if (header != null && header.length > 0) {
get.setHeaders(header);
}
int httpStatusCode = -1;
String respStr = null;
try (CloseableHttpResponse response = getHttpClient().execute(get)) {
int httpStatusCode = response.getStatusLine().getStatusCode();
httpStatusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
return Pair.of(httpStatusCode, EntityUtils.toString(entity, CHARSET));
respStr = EntityUtils.toString(entity, CHARSET);
return Pair.of(httpStatusCode, respStr);
} catch (IOException e) {
log.error("Get request fail", e);
throw new InternalException(e, ErrorCode.API_ERROR);
} finally {
get.releaseConnection();
if (log.isDebugEnabled()) {
log.debug(
"get:keepAlive={},url={},headers={},httpStatusCode={},respStr={}",
keepAlive,
url,
header,
httpStatusCode,
respStr
);
}
}
}

Expand All @@ -99,30 +119,43 @@ public Pair<Integer, byte[]> post(String url, HttpEntity requestEntity, Header..
post.setHeader("Connection", "Keep-Alive");
post.setHeaders(headers);
post.setEntity(requestEntity);
int httpStatusCode = -1;
String respStr = null;
try (CloseableHttpResponse httpResponse = getHttpClient().execute(post)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
httpStatusCode = httpResponse.getStatusLine().getStatusCode();
if (httpStatusCode != HttpStatus.SC_OK) {
String message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
String content = "";
if (entity != null && entity.getContent() != null) {
content = new String(EntityUtils.toByteArray(entity), CHARSET);
respStr = new String(EntityUtils.toByteArray(entity), CHARSET);
}
log.warn(
"Post request fail, statusCode={}, errorReason={}, body={}, url={}, headers={}",
statusCode,
"Post request fail, httpStatusCode={}, errorReason={}, body={}, url={}, headers={}",
httpStatusCode,
message,
content,
respStr,
url,
headers
);
throw new InternalException(message, ErrorCode.API_ERROR);
}
HttpEntity entity = httpResponse.getEntity();
return Pair.of(statusCode, EntityUtils.toByteArray(entity));
return Pair.of(httpStatusCode, EntityUtils.toByteArray(entity));
} catch (IOException e) {
log.error("Post request fail", e);
throw new InternalException(e, ErrorCode.API_ERROR);
} finally {
post.releaseConnection();
if (log.isDebugEnabled()) {
log.debug(
"post:url={},headers={},requestEntity={},httpStatusCode={},respStr={}",
url,
headers,
requestEntity,
httpStatusCode,
respStr
);
}
}
}

Expand All @@ -133,19 +166,38 @@ public Pair<Integer, String> put(String url, HttpEntity requestEntity, Header...
put.setHeader("Connection", "Keep-Alive");
put.setHeaders(headers);
put.setEntity(requestEntity);
int httpStatusCode = -1;
String respStr = null;
try (CloseableHttpResponse httpResponse = getHttpClient().execute(put)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
httpStatusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
String content = new String(EntityUtils.toByteArray(entity), CHARSET);
if (statusCode != HttpStatus.SC_OK) {
respStr = new String(EntityUtils.toByteArray(entity), CHARSET);
if (httpStatusCode != HttpStatus.SC_OK) {
String message = httpResponse.getStatusLine().getReasonPhrase();
log.info("Put request fail, statusCode={}, errorReason={}, content={}", statusCode, message, content);
log.warn(
"Put request fail, httpStatusCode={}, errorReason={}, respStr={}",
httpStatusCode,
message,
respStr
);
throw new InternalException(message, ErrorCode.API_ERROR);
}
return Pair.of(statusCode, content);
return Pair.of(httpStatusCode, respStr);
} catch (IOException e) {
log.error("Put request fail", e);
throw new InternalException(e, ErrorCode.API_ERROR);
} finally {
put.releaseConnection();
if (log.isDebugEnabled()) {
log.debug(
"put:url={},headers={},requestEntity={},httpStatusCode={},respStr={}",
url,
headers,
requestEntity,
httpStatusCode,
respStr
);
}
}
}

Expand All @@ -163,23 +215,39 @@ public Pair<Integer, String> delete(String url, String content, Header... header
delete.setEntity(requestEntity);
}
delete.setHeaders(headers);
int httpStatusCode = -1;
String respStr = null;
try (CloseableHttpResponse httpResponse = getHttpClient().execute(delete)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
httpStatusCode = httpResponse.getStatusLine().getStatusCode();
if (httpStatusCode != HttpStatus.SC_OK) {
String message = httpResponse.getStatusLine().getReasonPhrase();
log.info("Delete request fail, url={}, statusCode={}, errorReason={}", url, statusCode, message);
throw new InternalException(String.format("url=%s,statusCode=%s" +
",message=%s", url, statusCode, message), ErrorCode.API_ERROR);
log.info("Delete request fail, url={}, httpStatusCode={}, errorReason={}", url, httpStatusCode,
message);
throw new InternalException(String.format("url=%s,httpStatusCode=%s" +
",message=%s", url, httpStatusCode, message), ErrorCode.API_ERROR);
}
HttpEntity entity = httpResponse.getEntity();
byte[] respBytes = EntityUtils.toByteArray(entity);
if (respBytes == null) {
return null;
}
return Pair.of(statusCode, new String(respBytes, CHARSET));
respStr = new String(respBytes, CHARSET);
return Pair.of(httpStatusCode, respStr);
} catch (IOException e) {
log.error("Delete request fail", e);
throw new InternalException(e, ErrorCode.API_ERROR);
} finally {
delete.releaseConnection();
if (log.isDebugEnabled()) {
log.debug(
"delete:url={},headers={},body={},httpStatusCode={},respStr={}",
url,
headers,
content,
httpStatusCode,
respStr
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InternalException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHeader;

Expand Down Expand Up @@ -138,7 +140,7 @@ public String get(boolean keepAlive, String url, Header[] header) {
return httpHelper.get(keepAlive, url, header).getRight();
}

public CloseableHttpResponse getRawResp(boolean keepAlive, String url, Header[] header) {
public Pair<HttpRequestBase, CloseableHttpResponse> getRawResp(boolean keepAlive, String url, Header[] header) {
return httpHelper.getRawResp(keepAlive, url, header);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;

public interface HttpHelper {

CloseableHttpResponse getRawResp(boolean keepAlive, String url, Header[] header);
Pair<HttpRequestBase, CloseableHttpResponse> getRawResp(boolean keepAlive, String url, Header[] header);

Pair<Integer, String> get(boolean keepAlive, String url, Header[] header);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;

import java.util.AbstractList;
import java.util.concurrent.TimeUnit;
Expand All @@ -49,7 +50,7 @@ public WatchableHttpHelper(HttpHelper httpHelper, MeterRegistry meterRegistry) {
}

@Override
public CloseableHttpResponse getRawResp(boolean keepAlive, String url, Header[] header) {
public Pair<HttpRequestBase, CloseableHttpResponse> getRawResp(boolean keepAlive, String url, Header[] header) {
return httpHelper.getRawResp(keepAlive, url, header);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.methods.HttpRequestBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -197,7 +198,7 @@ private Pair<Long, StreamingResponseBody> getFileSizeAndStreamFromArtifactory(St
throw new InternalException(ErrorCode.FAIL_TO_GET_NODE_INFO_FROM_ARTIFACTORY);
}
try {
Pair<InputStream, Long> pair = artifactoryClient.getFileInputStream(
Pair<InputStream, HttpRequestBase> pair = artifactoryClient.getFileInputStream(
artifactoryConfig.getArtifactoryJobProject(),
backupStorageConfig.getBackupRepo(),
fileName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.methods.HttpRequestBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -201,7 +202,7 @@ private void processImportJob(String jobId) {
// 下载文件
if (!importFileDirectory.exists()) {
log.debug("begin to download from artifactory:{}", importJob.getFileName());
Pair<InputStream, Long> pair = artifactoryClient.getFileInputStream(
Pair<InputStream, HttpRequestBase> pair = artifactoryClient.getFileInputStream(
artifactoryConfig.getArtifactoryJobProject(),
backupStorageConfig.getBackupRepo(),
importJob.getFileName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.methods.HttpRequestBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -222,7 +223,7 @@ private Pair<Long, StreamingResponseBody> getFileSizeAndStreamFromArtifactory(
}
try {
log.debug("get {} fileInputStream from artifactory", exportInfo.getZipFileName());
Pair<InputStream, Long> pair = artifactoryClient.getFileInputStream(
Pair<InputStream, HttpRequestBase> pair = artifactoryClient.getFileInputStream(
artifactoryConfig.getArtifactoryJobProject(),
logExportConfig.getLogExportRepo(),
exportInfo.getZipFileName()
Expand Down
Loading