capistrant commented on code in PR #18402:
URL: https://github.com/apache/druid/pull/18402#discussion_r2475534268


##########
indexing-service/src/main/java/org/apache/druid/indexing/compact/MSQCompactionJobTemplate.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.compact;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.indexing.input.DruidInputSource;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.granularity.Granularity;
+import org.apache.druid.query.http.ClientSqlQuery;
+import org.apache.druid.server.compaction.CompactionCandidate;
+import org.apache.druid.server.compaction.CompactionSlotManager;
+import org.apache.druid.server.compaction.DataSourceCompactibleSegmentIterator;
+import org.apache.druid.server.coordinator.duty.CompactSegments;
+import org.joda.time.Interval;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Compaction template that creates MSQ SQL jobs using a templatized SQL with
+ * variables of the format {@code ${variableName}} for fields such as 
datasource
+ * name and start timestamp.
+ * <p>
+ * Compaction is triggered for an interval only if the current compaction state
+ * of the underlying segments DOES NOT match with the {@link #targetState}.
+ */
+public class MSQCompactionJobTemplate implements CompactionJobTemplate
+{
+  public static final String TYPE = "compactMsq";
+
+  public static final String VAR_DATASOURCE = "${dataSource}";
+  public static final String VAR_START_TIMESTAMP = "${startTimestamp}";
+  public static final String VAR_END_TIMESTAMP = "${endTimestamp}";
+
+  private static final DateTimeFormatter TIMESTAMP_FORMATTER =
+      DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
+
+  private final ClientSqlQuery sqlTemplate;
+  private final CompactionStateMatcher targetState;
+
+  @JsonCreator
+  public MSQCompactionJobTemplate(
+      @JsonProperty("sqlTemplate") ClientSqlQuery sqlTemplate,
+      @JsonProperty("targetState") CompactionStateMatcher targetState
+  )
+  {
+    this.sqlTemplate = sqlTemplate;
+    this.targetState = targetState;
+  }
+
+  @JsonProperty
+  public ClientSqlQuery getSqlTemplate()
+  {
+    return sqlTemplate;
+  }
+
+  @JsonProperty
+  public CompactionStateMatcher getTargetState()
+  {
+    return targetState;
+  }
+
+  @Nullable
+  @Override
+  public Granularity getSegmentGranularity()
+  {
+    return targetState.getSegmentGranularity();
+  }
+
+  @Override
+  public List<CompactionJob> createCompactionJobs(
+      DruidInputSource source,
+      CompactionJobParams jobParams
+  )
+  {
+    final String dataSource = source.getDataSource();
+
+    // Identify the compactible candidate segments
+    final CompactionConfigBasedJobTemplate delegate =
+        CompactionConfigBasedJobTemplate.create(dataSource, targetState);
+    final DataSourceCompactibleSegmentIterator candidateIterator =
+        delegate.getCompactibleCandidates(source, jobParams);
+
+    // Create MSQ jobs for each candidate by interpolating the template 
variables
+    final List<CompactionJob> jobs = new ArrayList<>();
+    while (candidateIterator.hasNext()) {
+      final CompactionCandidate candidate = candidateIterator.next();
+      jobs.add(
+          new CompactionJob(
+              createQueryForJob(dataSource, candidate.getCompactionInterval()),
+              candidate,
+              
CompactionSlotManager.getMaxTaskSlotsForMSQCompactionTask(sqlTemplate.getContext())
+          )
+      );
+    }
+
+    return jobs;
+  }
+
+  private ClientSqlQuery createQueryForJob(String dataSource, Interval 
compactionInterval)

Review Comment:
   I guess since this is just inline, the filter could be defined inline. But 
if someone changed the query after it existed for some time that filter 
wouldn't be re-applied to already compacted segments since it is not persisted 
in the target state.



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