You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/04/16 01:30:52 UTC

[GitHub] [incubator-druid] justinborromeo commented on a change in pull request #7428: Add errors and state to stream supervisor status API endpoint

justinborromeo 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_r275601091
 
 

 ##########
 File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/SeekableStreamSupervisorStateManager.java
 ##########
 @@ -0,0 +1,335 @@
+/*
+ * 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;
+    }
+
+    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);
+    }
+  }
+
+  private State supervisorState;
+  // Remove all throwableEvents that aren't in this set at the end of each run (transient)
+  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> stateHistory; // 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.stateHistory = new CircularBuffer<>(Math.max(healthinessThreshold, unhealthinessThreshold));
+  }
+
+  public Optional<State> setStateIfNoSuccessfulRunYet(State newState)
+  {
+    if (!atLeastOneSuccessfulRun) {
+      return Optional.of(setState(newState));
+    }
+    return Optional.absent();
+  }
+
+  public State setState(State newState)
+  {
+    if (newState.equals(State.SUSPENDED)) {
+      atLeastOneSuccessfulRun = false; // We want the startup states again after being suspended
+    }
+    this.supervisorState = newState;
+    return newState;
+  }
+
+  public void storeThrowableEvent(Throwable t)
+  {
+    if (t instanceof PossiblyTransientStreamException && atLeastOneSuccessfulRun) {
+      t = new TransientStreamException(t);
+    }
+
+    eventStore.storeThrowable(t);
+    currentRunSuccessful = false;
+  }
+
+  public void storeCompletedTaskState(TaskState state)
+  {
+    completedTaskHistory.add(state);
+  }
+
+  public void markRunFinishedAndEvaluateHealth()
+  {
+    if (currentRunSuccessful) {
+      atLeastOneSuccessfulRun = true;
+    }
+
+    State currentRunState = State.RUNNING;
+
+    for (Map.Entry<Class, Queue<ExceptionEvent>> events : eventStore.getNonTransientRecentEvents().entrySet()) {
+      if (events.getValue().size() >= unhealthinessThreshold) {
+        if (events.getKey().equals(NonTransientStreamException.class)) {
+          currentRunState = getHigherPriorityState(currentRunState, State.UNABLE_TO_CONNECT_TO_STREAM);
+        } else if (events.getKey().equals(TransientStreamException.class) ||
+                   events.getKey().equals(PossiblyTransientStreamException.class)) {
+          currentRunState = getHigherPriorityState(currentRunState, State.LOST_CONTACT_WITH_STREAM);
+        } else {
+          currentRunState = getHigherPriorityState(currentRunState, State.UNHEALTHY_SUPERVISOR);
+        }
+      }
+    }
+
+    // Evaluate task health
+    if (supervisorState == State.UNHEALTHY_TASKS) {
+      boolean tasksHealthy = completedTaskHistory.size() >= healthinessTaskThreshold;
+      for (int i = 0; i < Math.min(healthinessTaskThreshold, completedTaskHistory.size()); i++) {
+        if (completedTaskHistory.getLatest(i) != TaskState.SUCCESS) {
+          tasksHealthy = false;
+        }
+      }
+      if (tasksHealthy && currentRunState == State.UNHEALTHY_TASKS) {
 
 Review comment:
   You're right.  That should have been `supervisorState` but that's an irrelevant check because it's already checked on line 173.

----------------------------------------------------------------
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

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org