You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ol...@apache.org on 2016/09/07 23:37:57 UTC

[07/50] [abbrv] ambari git commit: AMBARI-18077. Clean up Log Search Appender and improve speed (Miklos Gergely via oleewere)

AMBARI-18077. Clean up Log Search Appender and improve speed (Miklos Gergely via oleewere)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/dff48f0a
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/dff48f0a
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/dff48f0a

Branch: refs/heads/branch-dev-logsearch
Commit: dff48f0a2297fa8f2c5c0207e4af4504a2deffbf
Parents: ff4beca
Author: Miklos Gergely <mg...@hortonworks.com>
Authored: Thu Aug 18 13:22:00 2016 +0200
Committer: oleewere <ol...@gmail.com>
Committed: Thu Sep 8 01:33:58 2016 +0200

----------------------------------------------------------------------
 .../ambari-logsearch-appender/pom.xml           |  1 -
 .../logsearch/appender/LogsearchConversion.java | 57 ++++++------
 .../ambari/logsearch/appender/Output.java       | 91 ++++++++++++++++++++
 .../apache/ambari/logsearch/appender/VBase.java | 63 --------------
 .../ambari/logsearch/appender/VOutput.java      | 88 -------------------
 .../commands/AbstractStateFileZkCommand.java    |  2 +-
 6 files changed, 117 insertions(+), 185 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-appender/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-appender/pom.xml b/ambari-logsearch/ambari-logsearch-appender/pom.xml
index 39f250a..e14d576 100644
--- a/ambari-logsearch/ambari-logsearch-appender/pom.xml
+++ b/ambari-logsearch/ambari-logsearch-appender/pom.xml
@@ -67,7 +67,6 @@
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
-      <version>1.2.17</version>
     </dependency>
     <dependency>
       <groupId>com.google.code.gson</groupId>

http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/LogsearchConversion.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/LogsearchConversion.java b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/LogsearchConversion.java
index dbdfe6c..877fa24 100644
--- a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/LogsearchConversion.java
+++ b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/LogsearchConversion.java
@@ -26,54 +26,47 @@ import org.apache.log4j.EnhancedPatternLayout;
 import org.apache.log4j.spi.LoggingEvent;
 
 public class LogsearchConversion extends EnhancedPatternLayout {
-  //
-  protected final int BUF_SIZE = 256;
-  protected final int MAX_CAPACITY = 1024;
 
-  private StringBuffer sbuf = new StringBuffer(BUF_SIZE);
-
-  private String newLine = System.getProperty("line.separator");
+  private static final String NEW_LINE = System.getProperty("line.separator");
 
   public LogsearchConversion() {
   }
 
   public String format(LoggingEvent event) {
-    if (sbuf.capacity() > MAX_CAPACITY) {
-      sbuf = new StringBuffer(BUF_SIZE);
-    } else {
-      sbuf.setLength(0);
-    }
     String outputStr = createOutput(event);
-    sbuf.append(outputStr + newLine);
-    return sbuf.toString();
+    return outputStr + NEW_LINE;
   }
 
   public String createOutput(LoggingEvent event) {
-    VOutput vOutput = new VOutput();
-    vOutput.setLevel(event.getLevel().toString());
-    vOutput.setFile(event.getLocationInformation().getFileName());
-    vOutput.setLine_number(Integer.parseInt(event.getLocationInformation().getLineNumber()));
-    String logmsg = event.getMessage() != null ? event.getMessage().toString() : "";
+    Output output = new Output();
+    
+    output.setLevel(event.getLevel().toString());
+    output.setFile(event.getLocationInformation().getFileName());
+    output.setLineNumber(Integer.parseInt(event.getLocationInformation().getLineNumber()));
+    output.setLogtime(Long.toString(event.getTimeStamp()));
+    output.setLoggerName(event.getLoggerName());
+    output.setThreadName(event.getThreadName());
+    output.setLogMessage(getLogMessage(event));
+    
+    return output.toJson();
+  }
+
+  public String getLogMessage(LoggingEvent event) {
+    String logMessage = event.getMessage() != null ? event.getMessage().toString() : "";
+
     if (event.getThrowableInformation() != null && event.getThrowableInformation().getThrowable() != null) {
-      logmsg += newLine + stackTraceToString(event.getThrowableInformation().getThrowable());
+      logMessage += NEW_LINE;
+      StringWriter sw = new StringWriter();
+      PrintWriter pw = new PrintWriter(sw);
+      event.getThrowableInformation().getThrowable().printStackTrace(pw);
+      logMessage += sw.toString();
     }
-    vOutput.setLog_message(logmsg);
-    vOutput.setLogtime("" + event.getTimeStamp());
-    vOutput.setLogger_name("" + event.getLoggerName());
-    vOutput.setThread_name(event.getThreadName());
-    return vOutput.toJson();
-  }
 
-  public String stackTraceToString(Throwable e) {
-    StringWriter sw = new StringWriter();
-    PrintWriter pw = new PrintWriter(sw);
-    e.printStackTrace(pw);
-    return sw.toString();
+    return logMessage;
   }
-  
+
   @Override
   public boolean ignoresThrowable() {
-    //set false to ignore exception stacktrace
     return false;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/Output.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/Output.java b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/Output.java
new file mode 100644
index 0000000..8001054
--- /dev/null
+++ b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/Output.java
@@ -0,0 +1,91 @@
+/*
+ * 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.ambari.logsearch.appender;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import com.google.gson.stream.JsonWriter;
+
+class Output {
+
+  private String level;
+  private String file;
+  private String threadName;
+  private int lineNumber;
+  private String loggerName;
+  private String logtime;
+  private String logMessage;
+
+  void setLevel(String level) {
+    this.level = level;
+  }
+
+  void setFile(String file) {
+    this.file = file;
+  }
+
+  void setThreadName(String threadName) {
+    this.threadName = threadName;
+  }
+
+  void setLineNumber(int lineNumber) {
+    this.lineNumber = lineNumber;
+  }
+
+  void setLoggerName(String loggerName) {
+    this.loggerName = loggerName;
+  }
+
+  void setLogtime(String logtime) {
+    this.logtime = logtime;
+  }
+
+  void setLogMessage(String logMessage) {
+    this.logMessage = logMessage;
+  }
+
+  public String toJson() {
+    StringWriter stringWriter = new StringWriter();
+    
+    try (JsonWriter writer = new JsonWriter(stringWriter)) {
+      writer.beginObject();
+      
+      if (level != null) writer.name("level").value(level);
+      if (file != null) writer.name("file").value(file);
+      if (threadName != null) writer.name("thread_name").value(threadName);
+      writer.name("line_number").value(lineNumber);
+      if (loggerName != null) writer.name("logger_name").value(loggerName);
+      if (logtime != null) writer.name("logtime").value(logtime);
+      if (logMessage != null) writer.name("log_message").value(logMessage);
+      
+      writer.endObject();
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+    
+    return stringWriter.toString();
+  }
+  
+  @Override
+  public String toString() {
+    return toJson();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VBase.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VBase.java b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VBase.java
deleted file mode 100644
index 4e91ccc..0000000
--- a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VBase.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * 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.ambari.logsearch.appender;
-
-import java.lang.reflect.Field;
-
-import org.apache.log4j.Logger;
-
-import com.google.gson.Gson;
-
-public class VBase {
-  private static Logger logger = Logger.getLogger(VBase.class);
-
-  /**
-   *
-   */
-  @Override
-  public String toString() {
-    @SuppressWarnings("rawtypes")
-    Class klass = this.getClass();
-    Field[] fields = klass.getDeclaredFields();
-    StringBuilder builder = new StringBuilder(klass.getSimpleName() + "={");
-    for (Field field : fields) {
-      try {
-        field.setAccessible(true);
-        Object fieldValue = field.get(this);
-        String fieldName = field.getName();
-        if (!fieldName.equalsIgnoreCase("serialVersionUID")) {
-          builder.append(fieldName + "={" + fieldValue + "} ");
-        }
-
-      } catch (Exception e) {
-        logger.error(e.getLocalizedMessage(), e);
-      }
-    }
-    builder.append("}");
-
-    return builder.toString();
-  }
-
-  public String toJson() {
-    Gson gson = new Gson();
-    String json = gson.toJson(this);
-    return json;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VOutput.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VOutput.java b/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VOutput.java
deleted file mode 100644
index 61da1da..0000000
--- a/ambari-logsearch/ambari-logsearch-appender/src/main/java/org/apache/ambari/logsearch/appender/VOutput.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * 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.ambari.logsearch.appender;
-
-public class VOutput extends VBase {
-
-  private String level;
-  private String file;
-  private String thread_name;
-  private int line_number;
-  private String log_message;
-  private String logger_name;
-  private String logtime;
-
-  public String getLevel() {
-    return level;
-  }
-
-  public void setLevel(String level) {
-    this.level = level;
-  }
-
-  public String getFile() {
-    return file;
-  }
-
-  public void setFile(String file) {
-    this.file = file;
-  }
-
-  public String getThread_name() {
-    return thread_name;
-  }
-
-  public void setThread_name(String thread_name) {
-    this.thread_name = thread_name;
-  }
-
-  public int getLine_number() {
-    return line_number;
-  }
-
-  public void setLine_number(int line_number) {
-    this.line_number = line_number;
-  }
-
-  public String getLog_message() {
-    return log_message;
-  }
-
-  public void setLog_message(String log_message) {
-    this.log_message = log_message;
-  }
-
-  public String getLogger_name() {
-    return logger_name;
-  }
-
-  public void setLogger_name(String logger_name) {
-    this.logger_name = logger_name;
-  }
-
-  public String getLogtime() {
-    return logtime;
-  }
-
-  public void setLogtime(String logtime) {
-    this.logtime = logtime;
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dff48f0a/ambari-logsearch/ambari-logsearch-solr-client/src/main/java/org/apache/ambari/logsearch/solr/commands/AbstractStateFileZkCommand.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-solr-client/src/main/java/org/apache/ambari/logsearch/solr/commands/AbstractStateFileZkCommand.java b/ambari-logsearch/ambari-logsearch-solr-client/src/main/java/org/apache/ambari/logsearch/solr/commands/AbstractStateFileZkCommand.java
index 664cd88..d351589 100644
--- a/ambari-logsearch/ambari-logsearch-solr-client/src/main/java/org/apache/ambari/logsearch/solr/commands/AbstractStateFileZkCommand.java
+++ b/ambari-logsearch/ambari-logsearch-solr-client/src/main/java/org/apache/ambari/logsearch/solr/commands/AbstractStateFileZkCommand.java
@@ -19,7 +19,7 @@
 package org.apache.ambari.logsearch.solr.commands;
 
 import org.apache.ambari.logsearch.solr.AmbariSolrCloudClient;
-import org.apache.ambari.logsearch.solr.domain.AmbariSolrState;;
+import org.apache.ambari.logsearch.solr.domain.AmbariSolrState;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;