dclim commented on a change in pull request #7428: Add errors and state to 
stream supervisor status API endpoint
URL: https://github.com/apache/incubator-druid/pull/7428#discussion_r278351993
 
 

 ##########
 File path: 
indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/SeekableStreamSupervisorStateManager.java
 ##########
 @@ -0,0 +1,386 @@
+/*
+ * 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.druid.indexing.seekablestream.supervisor;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import org.apache.druid.indexer.TaskState;
+import org.apache.druid.indexing.seekablestream.SeekableStreamSupervisorConfig;
+import 
org.apache.druid.indexing.seekablestream.exceptions.NonTransientStreamException;
+import 
org.apache.druid.indexing.seekablestream.exceptions.PossiblyTransientStreamException;
+import 
org.apache.druid.indexing.seekablestream.exceptions.TransientStreamException;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.utils.CircularBuffer;
+import org.codehaus.plexus.util.ExceptionUtils;
+import org.joda.time.DateTime;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+public class SeekableStreamSupervisorStateManager
+{
+  public enum State
+  {
+    // Error states are ordered from high to low priority
+    UNHEALTHY_SUPERVISOR(1),
+    UNHEALTHY_TASKS(2),
+    UNABLE_TO_CONNECT_TO_STREAM(3),
+    LOST_CONTACT_WITH_STREAM(4),
+    // Non-error states are equal priority
+    WAITING_TO_RUN(5),
+    CONNECTING_TO_STREAM(5),
+    DISCOVERING_INITIAL_TASKS(5),
+    CREATING_TASKS(5),
+    RUNNING(5),
+    SUSPENDED(5),
+    SHUTTING_DOWN(5);
+
+    // Lower priority number means higher priority and vice versa
+    private final int priority;
+
+    State(int priority)
+    {
+      this.priority = priority;
+    }
+
+    // We only want to set these if the supervisor hasn't had a successful 
iteration yet
+    public boolean isOnlySetWhenNoSuccessfulRunYet()
+    {
+      Set<State> firstRunStates = ImmutableSet.of(
+          WAITING_TO_RUN,
+          CONNECTING_TO_STREAM,
+          DISCOVERING_INITIAL_TASKS,
+          CREATING_TASKS,
+          RUNNING
+      );
+      return firstRunStates.contains(this);
+    }
+
+    public boolean isHealthy()
+    {
+      Set<State> unhealthyStates = ImmutableSet.of(
+          UNHEALTHY_SUPERVISOR,
+          UNHEALTHY_TASKS,
+          UNABLE_TO_CONNECT_TO_STREAM,
+          LOST_CONTACT_WITH_STREAM
+      );
+      return !unhealthyStates.contains(this);
+    }
+  }
+
+  public enum StreamErrorTransience
+  {
+    TRANSIENT,
+    POSSIBLY_TRANSIENT,
+    NON_TRANSIENT,
+    NON_STREAM_ERROR
+  }
+
+  private State supervisorState;
+  private final int healthinessThreshold;
+  private final int unhealthinessThreshold;
+  private final int healthinessTaskThreshold;
+  private final int unhealthinessTaskThreshold;
+
+  private boolean atLeastOneSuccessfulRun = false;
+  private boolean currentRunSuccessful = true;
+  private final CircularBuffer<TaskState> completedTaskHistory;
+  private final CircularBuffer<State> supervisorStateHistory; // From previous 
runs
+  private final ExceptionEventStore eventStore;
+
+  public SeekableStreamSupervisorStateManager(
+      State initialState,
+      SeekableStreamSupervisorConfig supervisorConfig
+  )
+  {
+    this.supervisorState = initialState;
+    this.healthinessThreshold = supervisorConfig.getHealthinessThreshold();
+    this.unhealthinessThreshold = supervisorConfig.getUnhealthinessThreshold();
+    this.healthinessTaskThreshold = 
supervisorConfig.getTaskHealthinessThreshold();
+    this.unhealthinessTaskThreshold = 
supervisorConfig.getTaskUnhealthinessThreshold();
+    Preconditions.checkArgument(
+        supervisorConfig.getMaxStoredExceptionEvents() >=
+        Math.max(healthinessThreshold, unhealthinessThreshold),
+        "numExceptionEventsToStore must be greater than or equal to both "
+        + "healthinessThreshold and unhealthinessThreshold"
+    );
+    this.eventStore = new ExceptionEventStore(
+        supervisorConfig.getMaxStoredExceptionEvents(),
+        supervisorConfig.isStoringStackTraces()
+    );
+    this.completedTaskHistory = new 
CircularBuffer<>(Math.max(healthinessTaskThreshold, 
unhealthinessTaskThreshold));
+    this.supervisorStateHistory = new 
CircularBuffer<>(Math.max(healthinessThreshold, unhealthinessThreshold));
+  }
+
+  /**
+   * Certain supervisor states can only be set if the supervisor hasn't had a 
successful iteration yet.  This function
+   * checks if there's been at least one successful iteration if needed and 
sets supervisor state to an appropriate
+   * new state.
+   */
+  public Optional<State> setState(State newState)
+  {
+    if (newState.isOnlySetWhenNoSuccessfulRunYet()) {
+      if (!atLeastOneSuccessfulRun) {
+        supervisorState = newState;
+        return Optional.of(newState);
+      } else {
+        return Optional.absent();
+      }
+    } else if (newState.equals(State.SUSPENDED)) {
+      atLeastOneSuccessfulRun = false; // We want the startup states again 
after being suspended
+    }
+    this.supervisorState = newState;
+    return Optional.of(newState);
+  }
+
+  public void storeThrowableEvent(Throwable t)
+  {
+    if (t instanceof PossiblyTransientStreamException && 
atLeastOneSuccessfulRun) {
+      t = new TransientStreamException(t);
 
 Review comment:
   How about wrap the underlying exception instead of wrapping 
`PossiblyTransientStreamException` in a `TransientStreamException`?

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to