You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by aledsage <gi...@git.apache.org> on 2017/06/12 01:20:06 UTC

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

GitHub user aledsage opened a pull request:

    https://github.com/apache/brooklyn-server/pull/731

    BROOKLYN-440: ssh not use StreamGobbler for logging stdout

    Improves thread usage when executing ssh remote commands. Simplifies the code to use a new `LoggingOutputStream`, rather than using the `StreamGobbler` thread.
    
    @neykov I'd appreciate your thoughts. It only does the first part of https://issues.apache.org/jira/browse/BROOKLYN-440; it doesn't do your suggestion of having the single thread polling to see if the streams have any bytes available.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/aledsage/brooklyn-server BROOKLYN-440

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/brooklyn-server/pull/731.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #731
    
----
commit e45a28fd1ce54c9229f65af2ea7685edb9fff7a2
Author: Aled Sage <al...@gmail.com>
Date:   2017-06-10T23:19:22Z

    BROOKLYN-440: ssh not use StreamGobbler for logging stdout

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    @neykov @geomacy thanks for comments - I've added another commit to address those. Could you review again please?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    LGTM


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/brooklyn-server/pull/731


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r121662464
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    --- End diff --
    
    the `b >= 0` here, plus the use of `StringBuilder` and `lineSoFar.append((char)c);` in `onChar`, will make things work wrongly for unicode, e.g. 
    
    ```
        @Test
        public void testLogsUnicode() throws Exception {
            LoggingOutputStream out = LoggingOutputStream.builder().logger(mockLogger).build();
            String test = "Лорем.";
            out.write("Лорем.\n".getBytes(StandardCharsets.UTF_8));
            out.flush();
    
            assertEquals(logs, ImmutableList.of(test));
        }
    ```
    fails with `java.lang.AssertionError: Lists differ at element [0]: Лорем. != . expected [Лорем.] but found [.]
    `.
    
    We could drop the `b >= 0` and use a list of bytes for `lineSoFar` in order to support Unicode if you felt that was valuable.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    retest this please


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    Failure unrelated?
    
    Retest this please.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r122729269
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    +        out.write(b);
    +    }
    +
    +    @Override
    +    public void flush() throws IOException {
    +        try {
    +            if (lineSoFar.length() > 0) {
    +                onLine(lineSoFar.toString());
    +                lineSoFar.setLength(0);
    --- End diff --
    
    > Is your concern about leaking that the lineSoFar might now have a very large internal array in StringBuilder.value, which won't be GC'ed until the LoggingOutputStream is GC'ed?
    
    Yes, that's what I meant.
    
    > Maybe I'll compromise and say that if the size was >= 1024 then I'll trimToSize.
    
    Sounds like a good balance. Alternatively could re-create the `StringBuilder` object with size 16 if you feel that's a better metric.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r121618034
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    +        out.write(b);
    +    }
    +
    +    @Override
    +    public void flush() throws IOException {
    +        try {
    +            if (lineSoFar.length() > 0) {
    +                onLine(lineSoFar.toString());
    +                lineSoFar.setLength(0);
    +            }
    +        } finally {
    +            super.flush();
    +        }
    +    }
    +    
    +    // Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior:
    +    // it silently ignores any exception thrown by flush(). Instead, just close the delegate stream.
    +    // It should flush itself if necessary.
    +    @Override
    +    public void close() throws IOException {
    +        try {
    +            onLine(lineSoFar.toString());
    +            lineSoFar.setLength(0);
    +        } finally {
    +            out.close();
    +            running.set(false);
    +        }
    +    }
    +    
    +    public void onChar(int c) {
    +        if (c=='\n' || c=='\r') {
    +            if (lineSoFar.length()>0)
    +                //suppress blank lines, so that we can treat either newline char as a line separator
    +                //(eg to show curl updates frequently)
    +                onLine(lineSoFar.toString());
    +            lineSoFar.setLength(0);
    +        } else {
    +            lineSoFar.append((char)c);
    +        }
    +    }
    +    
    +    public void onLine(String line) {
    +        //right trim, in case there is \r or other funnies
    +        while (line.length()>0 && Character.isWhitespace(line.charAt(line.length()-1)))
    +            line = line.substring(0, line.length()-1);
    +        //right trim, in case there is \r or other funnies
    --- End diff --
    
    `left trim` here; not sure I see the need for this check, since `onChar` never appends `\n` or `\r` to `lineSoFar`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r122727924
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    +        out.write(b);
    +    }
    +
    +    @Override
    +    public void flush() throws IOException {
    +        try {
    +            if (lineSoFar.length() > 0) {
    +                onLine(lineSoFar.toString());
    +                lineSoFar.setLength(0);
    --- End diff --
    
    Is your concern about leaking that the `lineSoFar` might now have a very large internal array in `StringBuilder.value`, which won't be GC'ed until the `LoggingOutputStream` is GC'ed?
    
    I could add a call to `StringBuilder.trimToSize()` as well (which would set it to size 0, rather than our initial size of 16). That would be less efficient (causing array-copy to grow again), but probably not noticeably so. Maybe I'll compromise and say that if the size was `>= 1024` then I'll `trimToSize`.
    
    Note that this code was copy-pasted from `StreamGobbler`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r122135363
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    --- End diff --
    
    Nice catch


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    @neykov @geomacy Do you think I need to worry about multi-threaded calls to this new `LoggingOutputStream` stream's `write`, `flush` and `close` methods?!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r122135751
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null) {
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    +        out.write(b);
    +    }
    +
    +    @Override
    +    public void flush() throws IOException {
    +        try {
    +            if (lineSoFar.length() > 0) {
    +                onLine(lineSoFar.toString());
    +                lineSoFar.setLength(0);
    --- End diff --
    
    This could potentially leak a big buffer - when logging a very long line once, `StringBuffer` will keep the allocated memory.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #731: BROOKLYN-440: ssh not use StreamGobbler for logg...

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:

    https://github.com/apache/brooklyn-server/pull/731
  
    @aledsage I'm fine with not updating the code to handle multi-threaded calls. Would be nice if you mark it explicitly as not supported it in the docs. It's been used just fine as is and we'll keep using it in the same context as before where it's single threaded.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---