nemo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-nemo] wynot12 commented on a change in pull request #203: [NEMO-360] Implementing an 'XGBoostPolicy'
Date Wed, 20 Mar 2019 09:23:47 GMT
wynot12 commented on a change in pull request #203: [NEMO-360] Implementing an 'XGBoostPolicy'
URL: https://github.com/apache/incubator-nemo/pull/203#discussion_r267239034
 
 

 ##########
 File path: common/src/main/java/org/apache/nemo/common/MetricUtils.java
 ##########
 @@ -0,0 +1,503 @@
+/*
+ * 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.nemo.common;
+
+import com.google.common.collect.HashBiMap;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.nemo.common.coder.DecoderFactory;
+import org.apache.nemo.common.coder.EncoderFactory;
+import org.apache.nemo.common.dag.Edge;
+import org.apache.nemo.common.dag.Vertex;
+import org.apache.nemo.common.exception.DeprecationException;
+import org.apache.nemo.common.exception.MetricException;
+import org.apache.nemo.common.exception.UnsupportedMethodException;
+import org.apache.nemo.common.ir.IRDAG;
+import org.apache.nemo.common.ir.executionproperty.ExecutionProperty;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.sql.*;
+import java.util.Enumeration;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.Supplier;
+import java.util.stream.IntStream;
+
+/**
+ * Utility class for metrics.
+ */
+public final class MetricUtils {
+  private static final Logger LOG = LoggerFactory.getLogger(MetricUtils.class.getName());
+
+  private static final CountDownLatch METADATA_LOADED = new CountDownLatch(1);
+  private static final CountDownLatch MUST_UPDATE_EP_KEY_METADATA = new CountDownLatch(1);
+  private static final CountDownLatch MUST_UPDATE_EP_METADATA = new CountDownLatch(1);
+
+  private static final Pair<HashBiMap<Integer, Pair<Class<? extends ExecutionProperty>,
Class<? extends Serializable>>>,
+      HashBiMap<Pair<Integer, Integer>, ExecutionProperty<?>>> METADATA
= loadMetaData();
+  // BiMap of (1) INDEX and (2) the Execution Property class and the value type class.
+  private static final HashBiMap<Integer, Pair<Class<? extends ExecutionProperty>,
Class<? extends Serializable>>>
+    EP_KEY_METADATA = METADATA.left();
+  // BiMap of (1) the Execution Property class INDEX and the value INDEX pair and (2) the
Execution Property.
+  private static final HashBiMap<Pair<Integer, Integer>, ExecutionProperty<?>>
+    EP_METADATA = METADATA.right();
+
+  private static final int VERTEX = 1;
+  private static final int EDGE = 2;
+
+  public static final String SQLITE_DB_NAME =
+    "jdbc:sqlite:" + Util.fetchProjectRootPath() + "/optimization_db.sqlite3";
+  public static final String POSTGRESQL_METADATA_DB_NAME =
+    "jdbc:postgresql://nemo-optimization.cabbufr3evny.us-west-2.rds.amazonaws.com:5432/nemo_optimization";
+  private static final String METADATA_TABLE_NAME = "nemo_optimization_meta";
+
+  /**
+   * Private constructor.
+   */
+  private MetricUtils() {
+  }
+
+  /**
+   * Load the BiMaps (lightweight) Metadata from the DB.
+   * @return the loaded BiMaps, or initialized ones.
+   */
+  private static Pair<HashBiMap<Integer, Pair<Class<? extends ExecutionProperty>,
Class<? extends Serializable>>>,
+    HashBiMap<Pair<Integer, Integer>, ExecutionProperty<?>>> loadMetaData()
{
+    deregisterBeamDriver();
+    try (final Connection c = DriverManager.getConnection(MetricUtils.POSTGRESQL_METADATA_DB_NAME,
+      "postgres", "fake_password")) {
+      try (final Statement statement = c.createStatement()) {
+        statement.setQueryTimeout(30);  // set timeout to 30 sec.
+
+        statement.executeUpdate(
+          "CREATE TABLE IF NOT EXISTS " + METADATA_TABLE_NAME
+          + " (key TEXT NOT NULL UNIQUE, data BYTEA NOT NULL);");
+
+        final ResultSet rsl = statement.executeQuery(
+          "SELECT * FROM " + METADATA_TABLE_NAME + " WHERE key='EP_KEY_METADATA';");
+        LOG.info("Metadata can be successfully loaded.");
+        if (rsl.next()) {
+          final HashBiMap<Integer, Pair<Class<? extends ExecutionProperty>, Class<?
extends Serializable>>>
+            indexEpKeyBiMap = SerializationUtils.deserialize(rsl.getBytes("Data"));
+          rsl.close();
+
+          final ResultSet rsr = statement.executeQuery(
+            "SELECT * FROM " + METADATA_TABLE_NAME + " WHERE key='EP_METADATA';");
+          if (rsr.next()) {
+            final HashBiMap<Pair<Integer, Integer>, ExecutionProperty<?>>
indexEpBiMap =
+              SerializationUtils.deserialize(rsr.getBytes("Data"));
+            rsr.close();
+
+            METADATA_LOADED.countDown();
+            LOG.info("Metadata successfully loaded from DB.");
+            return Pair.of(indexEpKeyBiMap, indexEpBiMap);
+          } else {
+            METADATA_LOADED.countDown();
+            LOG.info("No initial metadata for EP.");
+            return Pair.of(indexEpKeyBiMap, HashBiMap.create());
+          }
+        } else {
+          METADATA_LOADED.countDown();
+          LOG.info("No initial metadata.");
+          return Pair.of(HashBiMap.create(), HashBiMap.create());
+        }
+      } catch (Exception e) {
+        LOG.warn("Loading metadata from DB failed: ", e);
+        return Pair.of(HashBiMap.create(), HashBiMap.create());
+      }
+    } catch (Exception e) {
+      LOG.warn("Loading metadata from DB failed : ", e);
+      return Pair.of(HashBiMap.create(), HashBiMap.create());
+    }
+  }
+
+  public static Boolean metaDataLoaded() {
+    return METADATA_LOADED.getCount() == 0;
+  }
+
+  /**
+   * Save the BiMaps to DB if changes are necessary (rarely executed).
+   */
+  private static void updateMetaData() {
+    if (!metaDataLoaded()
+      || (MUST_UPDATE_EP_METADATA.getCount() + MUST_UPDATE_EP_KEY_METADATA.getCount() ==
2)) {
+      // no need to update
+      LOG.info("Not saving Metadata: metadata loaded: {}, Index-EP data: {}, Index-EP Key
data: {}",
+        metaDataLoaded(), MUST_UPDATE_EP_METADATA.getCount() == 0, MUST_UPDATE_EP_KEY_METADATA.getCount()
== 0);
+      return;
+    }
+    LOG.info("Saving Metadata..");
+
+    deregisterBeamDriver();
+    try (final Connection c = DriverManager.getConnection(MetricUtils.POSTGRESQL_METADATA_DB_NAME,
+      "postgres", "fake_password")) {
+      try (final Statement statement = c.createStatement()) {
+        statement.setQueryTimeout(30);  // set timeout to 30 sec.
+
+        if (MUST_UPDATE_EP_KEY_METADATA.getCount() == 0) {
+          insertOrUpdateMetadata(c, "EP_KEY_METADATA", EP_KEY_METADATA);
+          LOG.info("EP Key Metadata saved to DB.");
+        }
+
+        if (MUST_UPDATE_EP_METADATA.getCount() == 0) {
+          insertOrUpdateMetadata(c, "EP_METADATA", EP_METADATA);
+          LOG.info("EP Metadata saved to DB.");
+        }
+      }
+    } catch (SQLException e) {
+      LOG.warn("Saving of Metadata to DB failed: ", e);
+    }
+  }
+
+  /**
+   * Utility method to save key, value to the metadata table.
+   * @param c the connection to the DB.
+   * @param key the key to write to the DB metadata table.
+   * @param value the value to write to the DB metadata table.
+   * @throws SQLException SQLException on the way.
+   */
+  public static void insertOrUpdateMetadata(final Connection c, final String key, final Serializable
value)
+    throws SQLException {
+    try (final PreparedStatement pstmt = c.prepareStatement(
+      "INSERT INTO " + METADATA_TABLE_NAME + " (key, data) "
+        + "VALUES ('" + key + "', ?) ON CONFLICT (key) DO UPDATE SET data = excluded.data;"))
{
+      pstmt.setBinaryStream(1,
+        new ByteArrayInputStream(SerializationUtils.serialize(value)));
+      pstmt.executeUpdate();
+    }
+  }
+
+  /**
+   * Stringify execution properties of an IR DAG.
+   * @param irdag IR DAG to observe.
+   * @return the pair of stringified execution properties. Left is for vertices, right is
for edges.
+   */
+  public static Pair<String, String> stringifyIRDAGProperties(final IRDAG irdag) {
+    final StringBuilder vStringBuilder = new StringBuilder();
+    final StringBuilder eStringBuilder = new StringBuilder();
+
+    irdag.getVertices().forEach(v ->
+      v.getExecutionProperties().forEachProperties(ep ->
+        epFormatter(vStringBuilder, VERTEX, v.getNumericId(), ep)));
+
+    irdag.getVertices().forEach(v ->
+      irdag.getIncomingEdgesOf(v).forEach(e ->
+        e.getExecutionProperties().forEachProperties(ep ->
+          epFormatter(eStringBuilder, EDGE, e.getNumericId(), ep))));
+
+    // Update the metric metadata if new execution property key / values have been discovered
and updates are required.
+    updateMetaData();
+    return Pair.of(vStringBuilder.toString().trim(), eStringBuilder.toString().trim());
+  }
+
+  /**
+   * Formatter for execution properties. It updates the metadata for the metrics if new EP
key / values are discovered.
+   * @param builder string builder to append the metrics to.
+   * @param idx index specifying whether it's a vertex or an edge. This should be one digit.
+   * @param numericId numeric ID of the vertex or the edge.
+   * @param ep the execution property.
+   */
+  private static void epFormatter(final StringBuilder builder, final int idx,
 
 Review comment:
   I saw the similar code before and now it's gone and comes with this new PR.
   What happens??

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message