This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1212950 - EAP 6 start operation causes agent to run out of memory…
… due to storing console output in an unused buffer Now process output is captured (if captured) up to 2MB size, once output exceeds this limit, it is ignored - so we don't run out of memory (unless agent does not start plenty of verbose processes). Default limit can be changed via rhq.process-execution.captured-output.limit system property. This commit also gives more power to plugin writers about capturing process outputs. ProcessExecution#setCaptureOutput is now deprecated in favor of new CaptureMode setting. CaptureMode can capture to memory and/or forward to agent.log as well as setting captured limit. x
- Loading branch information
Libor Zoubek
committed
May 11, 2015
1 parent
10f72b3
commit 13439fe
Showing
3 changed files
with
244 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...es/core/native-system/src/main/java/org/rhq/core/system/ProcessExecutionOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* RHQ Management Platform | ||
* Copyright (C) 2005-2015 Red Hat, Inc. | ||
* All rights reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License, version 2, as | ||
* published by the Free Software Foundation, and/or the GNU Lesser | ||
* General Public License, version 2.1, also as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License and the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* and the GNU Lesser General Public License along with this program; | ||
* if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.rhq.core.system; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
/** | ||
* in-memory output stream with a limited buffer size. Once limit is reached it starts to ignore | ||
* {@link #write(int)} and {@link #write(byte[], int, int)} calls. Default limit is 2MB - this default can be | ||
* set using rhq.process-execution.captured-output.limit system property (in Bytes) or via {@link #ProcessExecutionOutputStream(int, boolean)}. | ||
* Optionally written output can be forwarded to logging subsystem. | ||
* @author lzoubek@redhat.com | ||
* | ||
*/ | ||
public class ProcessExecutionOutputStream extends ByteArrayOutputStream { | ||
|
||
private static final Log LOG = LogFactory.getLog(ProcessExecutionOutputStream.class); | ||
|
||
private int limit = Integer.getInteger("rhq.process-execution.captured-output.limit", 2 * 1024 * 1024); // default 2MB | ||
|
||
private boolean writeToLog; | ||
|
||
/** | ||
* Creates output stream with specified limit size | ||
* @param limit (in Bytes) maximum size of stream buffer, once reached, stream silently ignores any writes | ||
* @param writeToLog true to forward all messages to logger | ||
*/ | ||
public ProcessExecutionOutputStream(int limit, boolean writeToLog) { | ||
super(); | ||
this.limit = limit; | ||
this.writeToLog = writeToLog; | ||
} | ||
|
||
/** | ||
* Creates output stream with default stream buffer limit (2MB) | ||
* @param writeToLog true to forward all messages to logger | ||
*/ | ||
public ProcessExecutionOutputStream(boolean writeToLog) { | ||
super(); | ||
this.writeToLog = writeToLog; | ||
} | ||
|
||
@Override | ||
public synchronized void write(int b) { | ||
if (this.count < this.limit) { | ||
super.write(b); | ||
} | ||
|
||
if (this.writeToLog) { | ||
LOG.info(String.valueOf(b)); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void write(byte[] b, int off, int len) { | ||
if (this.count < this.limit) { | ||
if (this.count + len > this.limit) { | ||
len = this.limit - this.count; | ||
} | ||
super.write(b, off, len); | ||
} | ||
|
||
if (this.writeToLog && len > 0) { | ||
LOG.info(new String(this.buf, this.count - len, len)); | ||
} | ||
} | ||
} |