Skip to content

[Bugfix][Elasticsearch] jobmanager memory leak (#9160) #9161

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

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -20,10 +20,19 @@
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.api.source.SourceSplitEnumerator;
import org.apache.seatunnel.common.exception.CommonErrorCode;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.client.EsRestClient;
import org.apache.seatunnel.common.utils.JsonUtils;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.config.ElasticsearchBaseOptions;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.config.ElasticsearchConfig;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.dto.source.IndexDocsCount;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.exception.ElasticsearchConnectorException;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.util.HttpClientUtil;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -46,8 +55,6 @@ public class ElasticsearchSourceSplitEnumerator

private final ReadonlyConfig connConfig;

private EsRestClient esRestClient;

private final Object stateLock = new Object();

private Map<Integer, List<ElasticsearchSourceSplit>> pendingSplit;
Expand Down Expand Up @@ -80,9 +87,7 @@ public ElasticsearchSourceSplitEnumerator(
}

@Override
public void open() {
esRestClient = EsRestClient.createInstance(connConfig);
}
Comment on lines -83 to -85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we can not create client in open method? Could you share more details?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing, I found that it is related to the CloseableHttpAsyncClient client in RestClient because this can cause the issue where although the file is deleted, the thread still holds an open handle to the temporary file.
in jobmanager

ls -l /proc/1/fd/ | grep deleted
lr-x------ 1 flink flink 64 Apr 14 05:44 381 -> /tmp/jm_00b72ebe540b0440ed4510730bb35a8b/blobStorage/job_fae4e51b34120030fbda6a238628bd3a/blob_p-3cfee1e57d60be6705eb5ca6458a32f526d30a3a-9b7914da78fe1a3c0389cdbbf9d1e481 (deleted)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean CloseableHttpAsyncClient has bug so that not close properly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a factual perspective, yes. After replacing it with CloseableHttpClient, the #9160 was resolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should raise a issue on ElasticSearch community or upgrade ElasticSearch dependency version to verify it fixed or not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other projects, it's difficult to reproduce this issue—so far, it has only been reproduced in Flink.

If we wait for an official fix, it may take a long time or even yield no results.

Given these two points, I think we can proceed with this workaround for now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hisoka-X Can it be merged now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need more discuss. @hailin0 @corgy-w

public void open() {}

@Override
public void run() {
Expand Down Expand Up @@ -142,7 +147,7 @@ private List<ElasticsearchSourceSplit> getElasticsearchSplit() {
for (ElasticsearchConfig elasticsearchConfig : elasticsearchConfigs) {

String index = elasticsearchConfig.getIndex();
List<IndexDocsCount> indexDocsCounts = esRestClient.getIndexDocsCount(index);
List<IndexDocsCount> indexDocsCounts = getIndexDocsCount(index);
indexDocsCounts =
indexDocsCounts.stream()
.filter(x -> x.getDocsCount() != null && x.getDocsCount() > 0)
Expand All @@ -160,9 +165,7 @@ private List<ElasticsearchSourceSplit> getElasticsearchSplit() {
}

@Override
public void close() throws IOException {
esRestClient.close();
}
public void close() throws IOException {}

@Override
public void addSplitsBack(List<ElasticsearchSourceSplit> splits, int subtaskId) {
Expand Down Expand Up @@ -201,4 +204,30 @@ public ElasticsearchSourceState snapshotState(long checkpointId) throws Exceptio

@Override
public void notifyCheckpointComplete(long checkpointId) throws Exception {}

public List<IndexDocsCount> getIndexDocsCount(String index) {
List<String> hosts = connConfig.get(ElasticsearchBaseOptions.HOSTS);
String endpoint =
String.format(
"%s/_cat/indices/%s?h=index,docsCount&format=json", hosts.get(0), index);
HttpClientUtil httpClientUtil = new HttpClientUtil();
CloseableHttpClient httpClient = httpClientUtil.getHttpClient(connConfig);
HttpGet httpGet = new HttpGet(endpoint);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String entityStr = EntityUtils.toString(entity);
return JsonUtils.toList(entityStr, IndexDocsCount.class);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.connectors.seatunnel.elasticsearch.util;

import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.connectors.seatunnel.elasticsearch.config.ElasticsearchBaseOptions;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;

import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.SSLContext;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Optional;

@Slf4j
public class HttpClientUtil {

public CloseableHttpClient getHttpClient(ReadonlyConfig config) {
// 1. Initialize credentials provider
CredentialsProvider credsProvider = new BasicCredentialsProvider();
Optional<String> username = config.getOptional(ElasticsearchBaseOptions.USERNAME);
Optional<String> password = config.getOptional(ElasticsearchBaseOptions.PASSWORD);

// 2. Configure SSL
boolean tlsVerifyCertificate = config.get(ElasticsearchBaseOptions.TLS_VERIFY_CERTIFICATE);
boolean tlsVerifyHostnames = config.get(ElasticsearchBaseOptions.TLS_VERIFY_HOSTNAME);

SSLContext sslContext = buildSSLContext(config, tlsVerifyCertificate);

// 3. Set credentials if provided
username.ifPresent(
u ->
password.ifPresent(
p ->
credsProvider.setCredentials(
new AuthScope(
AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(u, p))));

// 4. Build HttpClient
HttpClientBuilder httpClientBuilder =
HttpClients.custom().setDefaultCredentialsProvider(credsProvider);

if (sslContext != null) {
httpClientBuilder.setSSLContext(sslContext);
}

if (!tlsVerifyHostnames) {
httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
}

return httpClientBuilder.build();
}

private SSLContext buildSSLContext(ReadonlyConfig config, boolean tlsVerifyCertificate) {
try {
if (tlsVerifyCertificate) {
Optional<String> keystorePath =
config.getOptional(ElasticsearchBaseOptions.TLS_KEY_STORE_PATH);
Optional<String> keystorePassword =
config.getOptional(ElasticsearchBaseOptions.TLS_KEY_STORE_PASSWORD);
Optional<String> truststorePath =
config.getOptional(ElasticsearchBaseOptions.TLS_TRUST_STORE_PATH);
Optional<String> truststorePassword =
config.getOptional(ElasticsearchBaseOptions.TLS_TRUST_STORE_PASSWORD);

return SSLUtils.buildSSLContext(
keystorePath, keystorePassword, truststorePath, truststorePassword)
.orElseThrow(() -> new RuntimeException("Failed to build SSLContext"));
} else {
log.warn(
"TLS certificate verification is disabled. This is not secure for production environments.");
return SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
}
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException("Failed to initialize SSLContext", e);
}
}
}
Loading