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

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

commit 4bda059ecada7c4c08066bc63a4fc80a49dcfe0d
Author: Jangho Seo <[email protected]>
AuthorDate: Sun Oct 20 06:27:24 2019 +0900

    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
---
 .../pass/compiletime/annotating/ResourceSitePass.java  |  2 +-
 .../nemo/examples/beam/NetworkTraceAnalysis.java       |  7 +++----
 .../runtime/common/plan/PhysicalPlanGenerator.java     | 10 ++++++----
 .../executor/bytetransfer/ByteInputContext.java        | 11 ++++++-----
 .../executor/bytetransfer/ByteOutputContext.java       |  7 ++++---
 .../executor/bytetransfer/ByteTransferContext.java     |  2 +-
 .../apache/nemo/runtime/executor/data/DataUtil.java    | 18 ++++++++++--------
 7 files changed, 31 insertions(+), 26 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..b6fcf00 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
@@ -44,7 +44,8 @@ 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 volatile boolean hasActiveInputStream = false;
+  private ByteBufInputStream currentByteBufInputStream = null;
 
   private final Iterator<InputStream> inputStreams = new 
Iterator<InputStream>() {
     @Override
@@ -107,7 +108,7 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * Called when a punctuation for sub-stream incarnation is detected.
    */
   void onNewStream() {
-    if (currentByteBufInputStream != null) {
+    if (hasActiveInputStream) {
       currentByteBufInputStream.byteBufQueue.close();
     }
     currentByteBufInputStream = new ByteBufInputStream();
@@ -120,7 +121,7 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * @param byteBuf the {@link ByteBuf} to supply
    */
   void onByteBuf(final ByteBuf byteBuf) {
-    if (currentByteBufInputStream == null) {
+    if (!hasActiveInputStream) {
       throw new RuntimeException("Cannot accept ByteBuf: No sub-stream is 
opened.");
     }
     if (byteBuf.readableBytes() > 0) {
@@ -135,7 +136,7 @@ public final class ByteInputContext extends 
ByteTransferContext {
    * Called when {@link #onByteBuf(ByteBuf)} event is no longer expected.
    */
   void onContextClose() {
-    if (currentByteBufInputStream != null) {
+    if (hasActiveInputStream) {
       currentByteBufInputStream.byteBufQueue.close();
     }
     byteBufInputStreams.close();
@@ -147,7 +148,7 @@ public final class ByteInputContext extends 
ByteTransferContext {
   public void onChannelError(@Nullable final Throwable cause) {
     setChannelError(cause);
 
-    if (currentByteBufInputStream != null) {
+    if (hasActiveInputStream) {
       currentByteBufInputStream.byteBufQueue.closeExceptionally(cause);
     }
     byteBufInputStreams.closeExceptionally(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..b719597 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
@@ -53,7 +53,8 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
 
   private final Channel channel;
 
-  private volatile ByteOutputStream currentByteOutputStream = null;
+  private volatile boolean hasActiveOutputStream = false;
+  private ByteOutputStream currentByteOutputStream = null;
   private volatile boolean closed = false;
 
   /**
@@ -83,7 +84,7 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
     if (closed) {
       throw new IOException("Context already closed.");
     }
-    if (currentByteOutputStream != null) {
+    if (hasActiveOutputStream) {
       currentByteOutputStream.close();
     }
     currentByteOutputStream = new ByteOutputStream();
@@ -101,7 +102,7 @@ public final class ByteOutputContext extends 
ByteTransferContext implements Auto
     if (closed) {
       return;
     }
-    if (currentByteOutputStream != null) {
+    if (hasActiveOutputStream) {
       currentByteOutputStream.close();
     }
     
channel.writeAndFlush(DataFrameEncoder.DataFrame.newInstance(getContextId()))
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..35b497f 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
@@ -41,7 +41,7 @@ public abstract class ByteTransferContext {
   private final ContextManager contextManager;
 
   private volatile boolean hasException = false;
-  private volatile Throwable exception = null;
+  private Throwable exception = null;
 
   /**
    * Creates a transfer context.
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