This is an automated email from the ASF dual-hosted git repository.

sanha pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nemo.git


The following commit(s) were added to refs/heads/master by this push:
     new f7d9139  [NEMO-422] Address code quality issues for ResourceSitePass, 
NetworkTraceAnalysis, PhysicalPlanGenerator, and bytetransfer (#244)
f7d9139 is described below

commit f7d9139b3df81b3b9dd72dfc2831d30cc543e64f
Author: Jangho Seo <[email protected]>
AuthorDate: Tue Oct 22 21:19:45 2019 +0900

    [NEMO-422] Address code quality issues for ResourceSitePass, 
NetworkTraceAnalysis, PhysicalPlanGenerator, and bytetransfer (#244)
    
    JIRA: [NEMO-422: SonarCloud 
issues](https://issues.apache.org/jira/projects/NEMO/issues/NEMO-422)
    
    **Major changes:**
    - None
    
    **Minor changes to note:**
    - ResourceSitePass: Fixed an operand in arithmetic expression to express 
its type explicitly.
    - example/NetworkTraceAnalysis: Replaced at-most-one-iteration loop with 
if-else.
    - PhysicalPlanGenerator: Use isPresent instead of orElseThrow for sanity 
checks.
    - bytetransfer: Removed 'volatile' for object types.
    
    **Tests for the changes:**
    - Existing tests should be sufficient.
    
    **Other comments:**
    - None
---
 .../compiletime/annotating/ResourceSitePass.java   |  2 +-
 .../nemo/examples/beam/NetworkTraceAnalysis.java   |  7 +++----
 .../runtime/common/plan/PhysicalPlanGenerator.java | 10 ++++++----
 .../executor/bytetransfer/ByteInputContext.java    | 23 +++++++++++-----------
 .../executor/bytetransfer/ByteOutputContext.java   | 15 +++++++-------
 .../executor/bytetransfer/ByteTransferContext.java | 14 ++++++-------
 .../nemo/runtime/executor/data/DataUtil.java       | 18 +++++++++--------
 7 files changed, 46 insertions(+), 43 deletions(-)

diff --git 
a/compiler/optimizer/src/main/java/org/apache/nemo/compiler/optimizer/pass/compiletime/annotating/ResourceSitePass.java
 
b/compiler/optimizer/src/main/java/org/apache/nemo/compiler/optimizer/pass/compiletime/annotating/ResourceSitePass.java
index bb97bc7..56db7e2 100644
--- 
a/compiler/optimizer/src/main/java/org/apache/nemo/compiler/optimizer/pass/compiletime/annotating/ResourceSitePass.java
+++ 
b/compiler/optimizer/src/main/java/org/apache/nemo/compiler/optimizer/pass/compiletime/annotating/ResourceSitePass.java
@@ -197,7 +197,7 @@ public final class ResourceSitePass extends AnnotatingPass {
       // Download bandwidth
       final double[] downloadCoefficientVector = new 
double[coefficientVectorSize];
       downloadCoefficientVector[OBJECTIVE_COEFFICIENT_INDEX] = 
bandwidthSpecification.down(nodeName);
-      downloadCoefficientVector[nodeCoefficientIndex] = 
parentParallelismOnThisLocation - parentParallelism;
+      downloadCoefficientVector[nodeCoefficientIndex] = (double) 
parentParallelismOnThisLocation - parentParallelism;
       constraints.add(new LinearConstraint(downloadCoefficientVector, 
Relationship.GEQ, 0));
 
       // The coefficient is non-negative
diff --git 
a/examples/beam/src/main/java/org/apache/nemo/examples/beam/NetworkTraceAnalysis.java
 
b/examples/beam/src/main/java/org/apache/nemo/examples/beam/NetworkTraceAnalysis.java
index f4a9ff3..aa56246 100644
--- 
a/examples/beam/src/main/java/org/apache/nemo/examples/beam/NetworkTraceAnalysis.java
+++ 
b/examples/beam/src/main/java/org/apache/nemo/examples/beam/NetworkTraceAnalysis.java
@@ -33,6 +33,7 @@ import org.apache.beam.sdk.values.TupleTag;
 import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -128,10 +129,8 @@ public final class NetworkTraceAnalysis {
    * @return extracted long typed data
    */
   private static long getLong(final Iterable<Long> data) {
-    for (final long datum : data) {
-      return datum;
-    }
-    return 0;
+    final Iterator<Long> iterator = data.iterator();
+    return iterator.hasNext() ? iterator.next() : 0;
   }
 
   /**
diff --git 
a/runtime/common/src/main/java/org/apache/nemo/runtime/common/plan/PhysicalPlanGenerator.java
 
b/runtime/common/src/main/java/org/apache/nemo/runtime/common/plan/PhysicalPlanGenerator.java
index b74dbd4..83728e4 100644
--- 
a/runtime/common/src/main/java/org/apache/nemo/runtime/common/plan/PhysicalPlanGenerator.java
+++ 
b/runtime/common/src/main/java/org/apache/nemo/runtime/common/plan/PhysicalPlanGenerator.java
@@ -284,10 +284,12 @@ public final class PhysicalPlanGenerator implements 
Function<IRDAG, DAG<Stage, S
    * @param stage to check for
    */
   private void integrityCheck(final Stage stage) {
-    stage.getPropertyValue(ParallelismProperty.class)
-      .orElseThrow(() -> new RuntimeException("Parallelism property must be 
set for Stage"));
-    stage.getPropertyValue(ScheduleGroupProperty.class)
-      .orElseThrow(() -> new RuntimeException("ScheduleGroup property must be 
set for Stage"));
+    if (!stage.getPropertyValue(ParallelismProperty.class).isPresent()) {
+      throw new RuntimeException("Parallelism property must be set for Stage");
+    }
+    if (!stage.getPropertyValue(ScheduleGroupProperty.class).isPresent()) {
+      throw new RuntimeException("ScheduleGroup property must be set for 
Stage");
+    }
 
     stage.getIRDAG().getVertices().forEach(irVertex -> {
       // Check vertex type.
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteInputContext.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteInputContext.java
index 30c7304..0559f60 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteInputContext.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteInputContext.java
@@ -23,6 +23,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.annotation.Nullable;
+import java.util.concurrent.atomic.AtomicReference;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Iterator;
@@ -44,7 +45,7 @@ public final class ByteInputContext extends 
ByteTransferContext {
 
   private final CompletableFuture<Iterator<InputStream>> completedFuture = new 
CompletableFuture<>();
   private final ClosableBlockingQueue<ByteBufInputStream> byteBufInputStreams 
= new ClosableBlockingQueue<>();
-  private volatile ByteBufInputStream currentByteBufInputStream = null;
+  private final AtomicReference<ByteBufInputStream> currentByteBufInputStream 
= new AtomicReference<>();
 
   private final Iterator<InputStream> inputStreams = new 
Iterator<InputStream>() {
     @Override
@@ -107,11 +108,11 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * Called when a punctuation for sub-stream incarnation is detected.
    */
   void onNewStream() {
-    if (currentByteBufInputStream != null) {
-      currentByteBufInputStream.byteBufQueue.close();
+    if (currentByteBufInputStream.get() != null) {
+      currentByteBufInputStream.get().byteBufQueue.close();
     }
-    currentByteBufInputStream = new ByteBufInputStream();
-    byteBufInputStreams.put(currentByteBufInputStream);
+    currentByteBufInputStream.set(new ByteBufInputStream());
+    byteBufInputStreams.put(currentByteBufInputStream.get());
   }
 
   /**
@@ -120,11 +121,11 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * @param byteBuf the {@link ByteBuf} to supply
    */
   void onByteBuf(final ByteBuf byteBuf) {
-    if (currentByteBufInputStream == null) {
+    if (currentByteBufInputStream.get() == null) {
       throw new RuntimeException("Cannot accept ByteBuf: No sub-stream is 
opened.");
     }
     if (byteBuf.readableBytes() > 0) {
-      currentByteBufInputStream.byteBufQueue.put(byteBuf);
+      currentByteBufInputStream.get().byteBufQueue.put(byteBuf);
     } else {
       // ignore empty data frames
       byteBuf.release();
@@ -135,8 +136,8 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * Called when {@link #onByteBuf(ByteBuf)} event is no longer expected.
    */
   void onContextClose() {
-    if (currentByteBufInputStream != null) {
-      currentByteBufInputStream.byteBufQueue.close();
+    if (currentByteBufInputStream.get() != null) {
+      currentByteBufInputStream.get().byteBufQueue.close();
     }
     byteBufInputStreams.close();
     completedFuture.complete(inputStreams);
@@ -147,8 +148,8 @@ public final class ByteInputContext extends 
ByteTransferContext {
   public void onChannelError(@Nullable final Throwable cause) {
     setChannelError(cause);
 
-    if (currentByteBufInputStream != null) {
-      currentByteBufInputStream.byteBufQueue.closeExceptionally(cause);
+    if (currentByteBufInputStream.get() != null) {
+      currentByteBufInputStream.get().byteBufQueue.closeExceptionally(cause);
     }
     byteBufInputStreams.closeExceptionally(cause);
     completedFuture.completeExceptionally(cause);
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteOutputContext.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteOutputContext.java
index fade5b0..4970456 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteOutputContext.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteOutputContext.java
@@ -36,6 +36,7 @@ import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -53,7 +54,7 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
 
   private final Channel channel;
 
-  private volatile ByteOutputStream currentByteOutputStream = null;
+  private final AtomicReference<ByteOutputStream> currentByteOutputStream = 
new AtomicReference<>();
   private volatile boolean closed = false;
 
   /**
@@ -83,11 +84,11 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
     if (closed) {
       throw new IOException("Context already closed.");
     }
-    if (currentByteOutputStream != null) {
-      currentByteOutputStream.close();
+    if (currentByteOutputStream.get() != null) {
+      currentByteOutputStream.get().close();
     }
-    currentByteOutputStream = new ByteOutputStream();
-    return currentByteOutputStream;
+    currentByteOutputStream.set(new ByteOutputStream());
+    return currentByteOutputStream.get();
   }
 
   /**
@@ -101,8 +102,8 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
     if (closed) {
       return;
     }
-    if (currentByteOutputStream != null) {
-      currentByteOutputStream.close();
+    if (currentByteOutputStream.get() != null) {
+      currentByteOutputStream.get().close();
     }
     
channel.writeAndFlush(DataFrameEncoder.DataFrame.newInstance(getContextId()))
       .addListener(getChannelWriteListener());
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteTransferContext.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteTransferContext.java
index 9d1f605..3838565 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteTransferContext.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/bytetransfer/ByteTransferContext.java
@@ -25,6 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.annotation.Nullable;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.Objects;
 
 /**
@@ -39,9 +40,7 @@ public abstract class ByteTransferContext {
   private final byte[] contextDescriptor;
   private final ChannelWriteFutureListener channelWriteFutureListener = new 
ChannelWriteFutureListener();
   private final ContextManager contextManager;
-
-  private volatile boolean hasException = false;
-  private volatile Throwable exception = null;
+  private final AtomicReference<Throwable> exception = new AtomicReference<>();
 
   /**
    * Creates a transfer context.
@@ -86,14 +85,14 @@ public abstract class ByteTransferContext {
    * @return Whether this context has exception or not.
    */
   public final boolean hasException() {
-    return hasException;
+    return exception.get() != null;
   }
 
   /**
    * @return The exception involved with this context, or {@code null}.
    */
   public final Throwable getException() {
-    return exception;
+    return exception.get();
   }
 
   @Override
@@ -121,12 +120,11 @@ public abstract class ByteTransferContext {
    * @param cause the exception to set
    */
   protected final void setChannelError(@Nullable final Throwable cause) {
-    if (hasException) {
+    if (hasException()) {
       return;
     }
-    hasException = true;
     LOG.error(String.format("A channel exception set on %s", toString())); // 
Not logging throwable, which isn't useful
-    exception = cause;
+    exception.set(cause);
   }
 
   /**
diff --git 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
index 16df0b6..a0e7fcf 100644
--- 
a/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
+++ 
b/runtime/executor/src/main/java/org/apache/nemo/runtime/executor/data/DataUtil.java
@@ -30,6 +30,7 @@ import 
org.apache.nemo.runtime.executor.data.streamchainer.Serializer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.annotation.concurrent.NotThreadSafe;
 import java.io.*;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -209,19 +210,20 @@ public final class DataUtil {
    *
    * @param <T> The type of elements.
    */
+  @NotThreadSafe
   public static final class InputStreamIterator<T> implements 
IteratorWithNumBytes<T> {
 
     private final Iterator<InputStream> inputStreams;
     private final Serializer<?, T> serializer;
 
-    private volatile CountingInputStream serializedCountingStream = null;
-    private volatile CountingInputStream encodedCountingStream = null;
-    private volatile boolean hasNext = false;
-    private volatile T next;
-    private volatile boolean cannotContinueDecoding = false;
-    private volatile DecoderFactory.Decoder<T> decoder = null;
-    private volatile long numSerializedBytes = 0;
-    private volatile long numEncodedBytes = 0;
+    private CountingInputStream serializedCountingStream = null;
+    private CountingInputStream encodedCountingStream = null;
+    private boolean hasNext = false;
+    private T next;
+    private boolean cannotContinueDecoding = false;
+    private DecoderFactory.Decoder<T> decoder = null;
+    private long numSerializedBytes = 0;
+    private long numEncodedBytes = 0;
 
     /**
      * Construct {@link Iterator} from {@link InputStream} and {@link 
DecoderFactory}.

Reply via email to