You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by "chengyouling (via GitHub)" <gi...@apache.org> on 2025/06/09 07:56:11 UTC

[PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

chengyouling opened a new pull request, #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2139122454


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_RECORDER_MAX_SIZE = 5000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private final Object lock = new Object();
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    synchronized (lock) {
+      StreamOperateEvent oldestEvent = getOldestOperateEvent(streamWrapperRecorder.values());
+      LOGGER.warn("reached recorder maxSize [{}] of file stream, delete oldest stream, operate time [{}], stackTrace {}",
+          maxSize, oldestEvent.getOpenStreamTimestamp(), oldestEvent.getInvokeStackTrace());
+      closeStreamWrapper(oldestEvent.getInputStreamWrapper());

Review Comment:
   add eventType for distinguish



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling merged PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137076746


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";

Review Comment:
   Please refer to those current existing upload related property keys:
   
   https://github.com/apache/servicecomb-java-chassis/blob/38cc4ebba11a77e2bf215a154d71c427bf3f6a73/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java#L68
   
   They all start with `servicecomb.uploads.`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137319060


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));

Review Comment:
   change to upload-file-stream-check



##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));
+    startCheckOpenStream();
+  }
+
+  private void startCheckOpenStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkOpenInputStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    wrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int upperLimit = getStreamOpenUpperLimit();
+    if (wrapperRecorder.size() < upperLimit) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(wrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());
+    StreamOperateEvent deleteEvent = sortEvents.get(0);
+    LOGGER.warn("reached upper limit [{}] of open stream, delete oldest stream, operate time [{}], stackTrace {}",
+        upperLimit, deleteEvent.getOpenStreamTimestamp(), deleteEvent.getInvokeStackTrace());
+    closeStreamWrapper(deleteEvent.getInputStreamWrapper());
+  }
+
+  public void clearRecorder(InputStreamWrapper inputStreamWrapper) {
+    wrapperRecorder.remove(inputStreamWrapper);
+  }
+
+  private void checkOpenInputStream() {
+    if (wrapperRecorder.isEmpty()) {
+      return;
+    }
+    List<InputStreamWrapper> overdueStreams = new ArrayList<>();
+    for (Map.Entry<InputStreamWrapper, StreamOperateEvent> entry : wrapperRecorder.entrySet()) {
+      StreamOperateEvent event = entry.getValue();
+      long streamOperateTime = event.getOpenStreamTimestamp();
+      long notifyTime = getStreamCheckInterval();
+
+      // If the check time exceeds three times, close the open stream.
+      if (System.currentTimeMillis() - streamOperateTime >= 3 * notifyTime) {
+        overdueStreams.add(entry.getKey());
+        continue;
+      }
+      if (System.currentTimeMillis() - streamOperateTime >= notifyTime) {
+        LOGGER.warn("there have input stream not closed, operate time [{}], operate stackTrace {}",
+            event.getOpenStreamTimestamp(), event.getInvokeStackTrace());
+        eventBus.post(event);
+      }
+    }
+    for (InputStreamWrapper wrapper : overdueStreams) {
+      closeStreamWrapper(wrapper);
+      LOGGER.warn("closed notify three times stream, operate time [{}], operate stackTrace {}",
+          wrapperRecorder.get(wrapper).getOpenStreamTimestamp(), wrapperRecorder.get(wrapper).getInvokeStackTrace());
+    }
+  }
+
+  private void closeStreamWrapper(InputStreamWrapper wrapper) {
+    try {
+      wrapper.close();
+    } catch (IOException e) {
+      LOGGER.error("closed input stream failed!", e);
+    }
+  }
+
+  private int getStreamOpenUpperLimit() {
+    return DynamicPropertyFactory.getInstance()
+        .getIntProperty(STREAM_OPEN_UPPER_LIMIT, DEFAULT_STREAM_OPEN_UPPER_LIMIT).get();
+  }
+
+  private boolean getStackTraceEnabled() {
+    return DynamicPropertyFactory.getInstance().getBooleanProperty(STREAM_STACKTRACE_ENABLED, true).get();

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137339631


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));
+    startCheckOpenStream();
+  }
+
+  private void startCheckOpenStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkOpenInputStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    wrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {

Review Comment:
   According to the current `checkAndRemoveOldestStream` logic, some cornor cases may not be handled well.
   
   For example,
   - if the business thread pool is large enough,
   - or the controller is developed in the reactive mode, opening file stream and scheduling the stream into a backend thread pool to process (I mean the response `CompletableFuture` is completed when the backend task is finished),
   
   in these two cases, it's possible that more than 1000 file streams are opened in a short period, and `checkAndRemoveOldestStream` method closes the previously opened stream, and the business logic is affected. From the perspective of business developers, these are normal usecases but we break their business logic.
   
   To solve the problem perfectly, it seems necessary to subscribe the `InvocationFinishEvent` and establish the mapping relationships between `Invocation` and `StreamOperateEvent`, which increases the complexity.
   Or as a simpler solution, can we just set the default value of the max recorders size bigger ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2138059014


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_RECORDER_MAX_SIZE = 5000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private final Object lock = new Object();
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    synchronized (lock) {
+      StreamOperateEvent oldestEvent = getOldestOperateEvent(streamWrapperRecorder.values());
+      LOGGER.warn("reached recorder maxSize [{}] of file stream, delete oldest stream, operate time [{}], stackTrace {}",
+          maxSize, oldestEvent.getOpenStreamTimestamp(), oldestEvent.getInvokeStackTrace());
+      closeStreamWrapper(oldestEvent.getInputStreamWrapper());

Review Comment:
   `streamWrapperRecorder` is oversize. This is abnormal case, and better to post an event to make users be aware of it.
   In addition, it's better to add a field to indicate this StreamOperateEvent is evicted by the map oversizing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2138990369


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));
+    startCheckOpenStream();
+  }
+
+  private void startCheckOpenStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkOpenInputStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    wrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {

Review Comment:
   change to 5000



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137317768


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/InputStreamWrapper.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class InputStreamWrapper extends InputStream {
+  private final InputStream inputStream;
+
+  public InputStreamWrapper(InputStream inputStream) {
+    this.inputStream = inputStream;
+  }
+
+  public InputStream getInputStream() {
+    return inputStream;
+  }
+
+  @Override
+  public int read() throws IOException {
+    return inputStream.read();
+  }
+
+  @Override
+  public int read(byte[] b) throws IOException {
+    return inputStream.read(b);
+  }
+
+  @Override
+  public int read(byte[] b, int off, int len) throws IOException {
+    return inputStream.read(b, off, len);
+  }
+
+  @Override
+  public long skip(long n) throws IOException {
+    return inputStream.skip(n);
+  }
+
+  @Override
+  public int available() throws IOException {
+    return inputStream.available();
+  }
+
+  @Override
+  public boolean markSupported() {
+    return inputStream.markSupported();
+  }
+
+  @Override
+  public synchronized void mark(int readlimit) {
+    inputStream.mark(readlimit);
+  }
+
+  @Override
+  public void close() throws IOException {
+    FileUploadStreamRecorder.getInstance().clearRecorder(this);
+    inputStream.close();
+  }
+
+  @Override
+  public synchronized void reset() throws IOException {

Review Comment:
   remove



##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java:
##########
@@ -36,7 +36,10 @@ public FileUploadPart(FileUpload fileUpload) {
 
   @Override
   public InputStream getInputStream() throws IOException {
-    return Files.newInputStream(new File(fileUpload.uploadedFileName()).toPath());
+    final InputStream inputStream = Files.newInputStream(new File(fileUpload.uploadedFileName()).toPath());
+    final InputStreamWrapper inputStreamWrapper = new InputStreamWrapper(inputStream);
+    FileUploadStreamRecorder.getInstance().recordOpenStream(inputStreamWrapper);

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137318183


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2138989368


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(streamWrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());
+    StreamOperateEvent deleteEvent = sortEvents.get(0);
+    LOGGER.warn("reached recorder maxSize [{}] of file stream, delete oldest stream, operate time [{}], stackTrace {}",
+        maxSize, deleteEvent.getOpenStreamTimestamp(), deleteEvent.getInvokeStackTrace());
+    closeStreamWrapper(deleteEvent.getInputStreamWrapper());

Review Comment:
   add synchronized



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137057134


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));

Review Comment:
   Maybe the thread name can be more specific, like "upload-file-stream-operate-check"?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137174719


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> wrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1, (t) -> new Thread(t, "stream-operate-check"));
+    startCheckOpenStream();
+  }
+
+  private void startCheckOpenStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkOpenInputStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    wrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int upperLimit = getStreamOpenUpperLimit();
+    if (wrapperRecorder.size() < upperLimit) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(wrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());
+    StreamOperateEvent deleteEvent = sortEvents.get(0);
+    LOGGER.warn("reached upper limit [{}] of open stream, delete oldest stream, operate time [{}], stackTrace {}",
+        upperLimit, deleteEvent.getOpenStreamTimestamp(), deleteEvent.getInvokeStackTrace());
+    closeStreamWrapper(deleteEvent.getInputStreamWrapper());
+  }
+
+  public void clearRecorder(InputStreamWrapper inputStreamWrapper) {
+    wrapperRecorder.remove(inputStreamWrapper);
+  }
+
+  private void checkOpenInputStream() {
+    if (wrapperRecorder.isEmpty()) {
+      return;
+    }
+    List<InputStreamWrapper> overdueStreams = new ArrayList<>();
+    for (Map.Entry<InputStreamWrapper, StreamOperateEvent> entry : wrapperRecorder.entrySet()) {
+      StreamOperateEvent event = entry.getValue();
+      long streamOperateTime = event.getOpenStreamTimestamp();
+      long notifyTime = getStreamCheckInterval();
+
+      // If the check time exceeds three times, close the open stream.
+      if (System.currentTimeMillis() - streamOperateTime >= 3 * notifyTime) {
+        overdueStreams.add(entry.getKey());
+        continue;
+      }
+      if (System.currentTimeMillis() - streamOperateTime >= notifyTime) {
+        LOGGER.warn("there have input stream not closed, operate time [{}], operate stackTrace {}",
+            event.getOpenStreamTimestamp(), event.getInvokeStackTrace());
+        eventBus.post(event);
+      }
+    }
+    for (InputStreamWrapper wrapper : overdueStreams) {
+      closeStreamWrapper(wrapper);
+      LOGGER.warn("closed notify three times stream, operate time [{}], operate stackTrace {}",
+          wrapperRecorder.get(wrapper).getOpenStreamTimestamp(), wrapperRecorder.get(wrapper).getInvokeStackTrace());
+    }
+  }
+
+  private void closeStreamWrapper(InputStreamWrapper wrapper) {
+    try {
+      wrapper.close();
+    } catch (IOException e) {
+      LOGGER.error("closed input stream failed!", e);
+    }
+  }
+
+  private int getStreamOpenUpperLimit() {
+    return DynamicPropertyFactory.getInstance()
+        .getIntProperty(STREAM_OPEN_UPPER_LIMIT, DEFAULT_STREAM_OPEN_UPPER_LIMIT).get();
+  }
+
+  private boolean getStackTraceEnabled() {
+    return DynamicPropertyFactory.getInstance().getBooleanProperty(STREAM_STACKTRACE_ENABLED, true).get();

Review Comment:
   Better to make it false by default, to avoid performance impact.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "chengyouling (via GitHub)" <gi...@apache.org>.
chengyouling commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137771802


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(streamWrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137054382


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_OPEN_UPPER_LIMIT = "file.upload.stream.operate.upper-limit";
+
+  private static final String STREAM_STACKTRACE_ENABLED = "file.upload.stream.operate.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "file.upload.stream.operate.check-interval";

Review Comment:
   To keep consistent with other Java-Chassis properties, please make these property keys start with `servicecomb.` prefix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137411892


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(streamWrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());

Review Comment:
   It costs too much to sort the all events. Better to traversal the events and pickup the oldest one.



##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadStreamRecorder.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+
+public class FileUploadStreamRecorder {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadStreamRecorder.class);
+
+  private static final FileUploadStreamRecorder RECORDER = new FileUploadStreamRecorder();
+
+  private static final String STREAM_RECORDER_MAX_SIZE = "servicecomb.uploads.file.stream-recorder.max-size";
+
+  private static final String STREAM_STACKTRACE_ENABLED
+      = "servicecomb.uploads.file.stream-recorder.stack-trace-enabled";
+
+  private static final String STREAM_CHECK_INTERVAL = "servicecomb.uploads.file.stream-recorder.check-interval";
+
+  private static final int DEFAULT_STREAM_OPEN_UPPER_LIMIT = 1000;
+
+  private static final long DEFAULT_STREAM_CHECK_INTERVAL = 30000L;
+
+  private final Map<InputStreamWrapper, StreamOperateEvent> streamWrapperRecorder = new ConcurrentHashMap<>();
+
+  private final EventBus eventBus;
+
+  private final ScheduledExecutorService streamCheckExecutor;
+
+  private FileUploadStreamRecorder() {
+    eventBus = EventManager.getEventBus();
+    streamCheckExecutor = Executors.newScheduledThreadPool(1,
+        (t) -> new Thread(t, "upload-file-stream-check"));
+    startCheckRecordFileStream();
+  }
+
+  private void startCheckRecordFileStream() {
+    streamCheckExecutor.scheduleWithFixedDelay(this::checkRecordFileStream, DEFAULT_STREAM_CHECK_INTERVAL,
+        getStreamCheckInterval(), TimeUnit.MILLISECONDS);
+  }
+
+  public static FileUploadStreamRecorder getInstance() {
+    return RECORDER;
+  }
+
+  public void recordOpenStream(final InputStreamWrapper wrapper) {
+    checkAndRemoveOldestStream();
+    streamWrapperRecorder.put(wrapper, new StreamOperateEvent(wrapper));
+  }
+
+  private void checkAndRemoveOldestStream() {
+    int maxSize = getStreamRecorderMaxSize();
+    if (streamWrapperRecorder.size() < maxSize) {
+      return;
+    }
+    List<StreamOperateEvent> operateEvents = new ArrayList<>(streamWrapperRecorder.values());
+    List<StreamOperateEvent> sortEvents = operateEvents.stream()
+        .sorted(Comparator.comparingLong(StreamOperateEvent::getOpenStreamTimestamp)).collect(Collectors.toList());
+    StreamOperateEvent deleteEvent = sortEvents.get(0);
+    LOGGER.warn("reached recorder maxSize [{}] of file stream, delete oldest stream, operate time [{}], stackTrace {}",
+        maxSize, deleteEvent.getOpenStreamTimestamp(), deleteEvent.getInvokeStackTrace());
+    closeStreamWrapper(deleteEvent.getInputStreamWrapper());

Review Comment:
   The code block from line 86 to the end is not atomic. In concurrency case, there may be duplicated removal of the same oldest `StreamOpearteEvent`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2136996598


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/InputStreamWrapper.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.servicecomb.foundation.vertx.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class InputStreamWrapper extends InputStream {
+  private final InputStream inputStream;
+
+  public InputStreamWrapper(InputStream inputStream) {
+    this.inputStream = inputStream;
+  }
+
+  public InputStream getInputStream() {
+    return inputStream;
+  }
+
+  @Override
+  public int read() throws IOException {
+    return inputStream.read();
+  }
+
+  @Override
+  public int read(byte[] b) throws IOException {
+    return inputStream.read(b);
+  }
+
+  @Override
+  public int read(byte[] b, int off, int len) throws IOException {
+    return inputStream.read(b, off, len);
+  }
+
+  @Override
+  public long skip(long n) throws IOException {
+    return inputStream.skip(n);
+  }
+
+  @Override
+  public int available() throws IOException {
+    return inputStream.available();
+  }
+
+  @Override
+  public boolean markSupported() {
+    return inputStream.markSupported();
+  }
+
+  @Override
+  public synchronized void mark(int readlimit) {
+    inputStream.mark(readlimit);
+  }
+
+  @Override
+  public void close() throws IOException {
+    FileUploadStreamRecorder.getInstance().clearRecorder(this);
+    inputStream.close();
+  }
+
+  @Override
+  public synchronized void reset() throws IOException {

Review Comment:
   `synchronized` on this method and `mark(int readlimit)` seems not necessary, please check whether these `synchronized` words can be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#4835] Fixed the fileUpload stream not closed problem [servicecomb-java-chassis]

Posted by "yhs0092 (via GitHub)" <gi...@apache.org>.
yhs0092 commented on code in PR #4837:
URL: https://github.com/apache/servicecomb-java-chassis/pull/4837#discussion_r2137016160


##########
foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/FileUploadPart.java:
##########
@@ -36,7 +36,10 @@ public FileUploadPart(FileUpload fileUpload) {
 
   @Override
   public InputStream getInputStream() throws IOException {
-    return Files.newInputStream(new File(fileUpload.uploadedFileName()).toPath());
+    final InputStream inputStream = Files.newInputStream(new File(fileUpload.uploadedFileName()).toPath());
+    final InputStreamWrapper inputStreamWrapper = new InputStreamWrapper(inputStream);
+    FileUploadStreamRecorder.getInstance().recordOpenStream(inputStreamWrapper);

Review Comment:
   How about moving this line into the constructor of `InputStreamWrapper`?
   It's not about functionality, but since the `clearRecorder` method is invoked in the `InputStreamWrapper.close()` method, maybe it's better to move the `recordOpenStream` method invocation into the `InputStreamWrapper`, too, so that the `FileUploadStreamRecord` can be encapsulated better.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org