github-advanced-security[bot] commented on code in PR #17353:
URL: https://github.com/apache/druid/pull/17353#discussion_r1802349798


##########
indexing-service/src/main/java/org/apache/druid/indexing/batch/ScheduledBatchSupervisorSpec.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.batch;
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.apache.druid.common.config.Configs;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.InvalidInput;
+import org.apache.druid.indexing.overlord.supervisor.SupervisorSpec;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.sql.calcite.planner.ExplainAttributes;
+import org.apache.druid.sql.client.BrokerClient;
+import org.apache.druid.sql.http.ExplainPlanResponse;
+import org.apache.druid.sql.http.SqlQuery;
+
+import javax.annotation.Nullable;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+
+public class ScheduledBatchSupervisorSpec implements SupervisorSpec
+{
+  public static final String TYPE = "scheduled_batch";
+  public static final String ID_PREFIX = "scheduled_batch__";
+
+  private static final Logger log = new Logger(ScheduledBatchSupervisor.class);
+
+  @JsonProperty
+  private final SqlQuery spec;
+  @JsonProperty
+  private final boolean suspended;
+  @JsonProperty
+  private final CronSchedulerConfig schedulerConfig;
+
+  /**
+   * Note that both {@link #dataSource} and {@link #id} are optional JSON 
fields present in the spec.
+   * They are only used internally because we use and persist the user-facing 
spec in the metadata store. So these
+   * additional fields are required for jackson deserialization.
+   * It would be better to have separate user-facing and domain-specific DTOs 
for this purpose and map them, but
+   * that'll entail a larger change.
+   */
+  @JsonProperty
+  private final String dataSource;
+  @JsonProperty
+  private final String id;
+
+  private final ObjectMapper objectMapper;
+  private final ScheduledBatchScheduler batchScheduler;
+  private final BrokerClient brokerClient;
+
+  @JsonCreator
+  public ScheduledBatchSupervisorSpec(
+      @JsonProperty("spec") final SqlQuery spec,
+      @JsonProperty("schedulerConfig") final CronSchedulerConfig 
schedulerConfig,
+      @JsonProperty("suspended") @Nullable Boolean suspended,
+      @JsonProperty("id") @Nullable final String id,
+      @JsonProperty("dataSource") @Nullable final String dataSource,
+      @JacksonInject ObjectMapper objectMapper,
+      @JacksonInject ScheduledBatchScheduler batchScheduler,
+      @JacksonInject BrokerClient brokerClient
+  )
+  {
+    this.spec = spec;
+    this.schedulerConfig = schedulerConfig;
+    this.suspended = Configs.valueOrDefault(suspended, false);
+    this.objectMapper = objectMapper;
+    this.batchScheduler = batchScheduler;
+    this.brokerClient = brokerClient;
+
+    this.dataSource = dataSource != null ? dataSource : 
getDatasourceFromQuery();
+    this.id = id != null ? id : ID_PREFIX + this.dataSource + "__" + 
UUID.randomUUID();
+  }
+
+  private String getDatasourceFromQuery()
+  {
+    final List<ExplainPlanResponse> explainPlanResponses;
+    final ListenableFuture<List<ExplainPlanResponse>> explainPlanFuture = 
brokerClient.explainPlanFor(spec);
+    try {
+      explainPlanResponses = explainPlanFuture.get();
+    }
+    catch (Exception e) {
+      throw InvalidInput.exception("Error getting datasource from query[%s]: 
[%s]", spec, e);
+    }
+
+    if (explainPlanResponses.size() != 1) {
+      throw DruidException.defensive(
+          "Received an invalid EXPLAIN PLAN response for query[%s]. Expected a 
single plan, but got[%d]: [%s].",
+          spec.getQuery(), explainPlanResponses.size(), explainPlanResponses
+      );
+    }
+
+    final ExplainPlanResponse explainPlanResponse = 
explainPlanResponses.get(0);
+    final ExplainAttributes explainAttributes;
+    try {
+      explainAttributes = objectMapper.readValue(
+          explainPlanResponse.getAttributes(),
+          ExplainAttributes.class
+      );
+    }
+    catch (JsonProcessingException e) {
+      throw DruidException.defensive(
+          "Error unmarshaling EXPLAIN PLAN attributes for query[%s] from 
response[%s]: [%s]",
+          spec.getQuery(), explainPlanResponse, e

Review Comment:
   ## Use of default toString()
   
   Default toString(): ExplainPlanResponse inherits toString() from Object, and 
so is not suitable for printing.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/8407)



##########
indexing-service/src/main/java/org/apache/druid/indexing/batch/ScheduledBatchSupervisor.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.batch;
+
+import org.apache.druid.indexing.overlord.DataSourceMetadata;
+import org.apache.druid.indexing.overlord.supervisor.Supervisor;
+import org.apache.druid.indexing.overlord.supervisor.SupervisorReport;
+import org.apache.druid.indexing.overlord.supervisor.SupervisorStateManager;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.logger.Logger;
+
+import javax.annotation.Nullable;
+
+public class ScheduledBatchSupervisor implements Supervisor
+{
+  private static final Logger log = new Logger(ScheduledBatchSupervisor.class);
+  private final ScheduledBatchSupervisorSpec supervisorSpec;
+  private final ScheduledBatchScheduler scheduler;
+
+  public ScheduledBatchSupervisor(
+      final ScheduledBatchSupervisorSpec supervisorSpec,
+      final ScheduledBatchScheduler scheduler
+  )
+  {
+    this.supervisorSpec = supervisorSpec;
+    this.scheduler = scheduler;
+  }
+
+  @Override
+  public void start()
+  {
+    if (supervisorSpec.isSuspended()) {
+      log.info("Suspending the scheduled batch supervisor[%s].", 
supervisorSpec.getId());
+      scheduler.stopScheduledIngestion(supervisorSpec.getId());
+    } else {
+      scheduler.startScheduledIngestion(
+          supervisorSpec.getId(),
+          supervisorSpec.getSchedulerConfig(),
+          supervisorSpec.getSpec()
+      );
+      log.info("Starting the scheduled batch supervisor[%s].", 
supervisorSpec.getId());
+    }
+  }
+
+  @Override
+  public void stop(boolean stopGracefully)
+  {
+    log.info("Stopping the scheduled batch supervisor[%s]", 
supervisorSpec.getId());
+    scheduler.stopScheduledIngestion(supervisorSpec.getId());
+  }
+
+  @Override
+  public SupervisorReport<ScheduledBatchSupervisorSnapshot> getStatus()
+  {
+    return new SupervisorReport<>(
+        supervisorSpec.getId(),
+        DateTimes.nowUtc(),
+        scheduler.getSchedulerSnapshot(supervisorSpec.getId())
+    );
+  }
+
+  @Override
+  public SupervisorStateManager.State getState()
+  {
+    if (supervisorSpec.isSuspended()) {
+      return State.SUSPENDED;
+    } else {
+      return State.RUNNING;
+    }
+  }
+
+  @Override
+  public void reset(@Nullable DataSourceMetadata dataSourceMetadata)
+  {
+    // do nothing
+  }
+
+  public enum State implements SupervisorStateManager.State

Review Comment:
   ## Class has same name as super class
   
   State has the same name as its supertype 
[org.apache.druid.indexing.overlord.supervisor.SupervisorStateManager$State](1).
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/8406)



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to