kfaraz commented on code in PR #18844:
URL: https://github.com/apache/druid/pull/18844#discussion_r2697108197


##########
server/src/main/java/org/apache/druid/segment/metadata/SqlIndexingStateStorage.java:
##########
@@ -0,0 +1,408 @@
+/*
+ * 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.segment.metadata;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.inject.Inject;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.InternalServerError;
+import org.apache.druid.guice.LazySingleton;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.metadata.MetadataStorageTablesConfig;
+import org.apache.druid.metadata.SQLMetadataConnector;
+import org.apache.druid.timeline.CompactionState;
+import org.joda.time.DateTime;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.SQLStatement;
+import org.skife.jdbi.v2.Update;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.validation.constraints.NotEmpty;
+import java.util.List;
+
+/**
+ * Database-backed implementation of {@link IndexingStateStorage}.
+ * <p>
+ * Manages the persistence and retrieval of {@link CompactionState} (AKA 
IndexingState) objects in the metadata storage.
+ * Indexing states are uniquely identified by their fingerprints, which are 
SHA-256 hashes of their content.
+ * </p>
+ * <p>
+ * This implementation is designed to be called from a single thread and 
relies on
+ * database constraints and the retry mechanism to handle any conflicts. 
Operations are idempotent - concurrent
+ * upserts for the same fingerprint will either succeed or fail with a 
constraint violation that is safely ignored.
+ * </p>
+ */
+@LazySingleton
+public class SqlIndexingStateStorage implements IndexingStateStorage
+{
+  private static final EmittingLogger log = new 
EmittingLogger(SqlIndexingStateStorage.class);
+
+  private final MetadataStorageTablesConfig dbTables;
+  private final ObjectMapper jsonMapper;
+  private final SQLMetadataConnector connector;
+
+  @Inject
+  public SqlIndexingStateStorage(
+      @Nonnull MetadataStorageTablesConfig dbTables,
+      @Nonnull ObjectMapper jsonMapper,
+      @Nonnull SQLMetadataConnector connector
+  )
+  {
+    this.dbTables = dbTables;
+    this.jsonMapper = jsonMapper;
+    this.connector = connector;
+  }
+
+  @Override
+  public void upsertIndexingState(
+      @NotEmpty final String dataSource,
+      @NotEmpty final String fingerprint,
+      @Nonnull final CompactionState indexingState,
+      @Nonnull final DateTime updateTime
+  )
+  {
+    // Strictly sanitize inputs to avoid writing junk data to the rdbms
+    StringBuilder errors = new StringBuilder();
+    if (dataSource == null || dataSource.isEmpty()) {
+      errors.append("dataSource cannot be empty; ");
+    }
+    if (fingerprint == null || fingerprint.isEmpty()) {
+      errors.append("fingerprint cannot be empty; ");
+    }
+    if (indexingState == null) {
+      errors.append("indexingState cannot be null; ");
+    }
+    if (updateTime == null) {
+      errors.append("updateTime cannot be null; ");
+    }
+    if (errors.length() > 0) {
+      throw DruidException.forPersona(DruidException.Persona.DEVELOPER)
+                          .ofCategory(DruidException.Category.INVALID_INPUT)
+                          .build(errors.toString().trim());
+    }
+
+    try {
+      connector.retryWithHandle(handle -> {
+        // Check if the fingerprint already exists and its used status
+        final FingerprintState state = getFingerprintState(handle, 
fingerprint);
+        final String now = updateTime.toString();
+
+        switch (state) {
+          case EXISTS_AND_USED:
+            // Fingerprint exists and is already marked as used - no operation 
needed
+            log.debug(
+                "Indexing state for fingerprint[%s] in dataSource[%s] already 
exists and is marked as used. Skipping update.",
+                fingerprint,
+                dataSource
+            );
+            break;
+
+          case EXISTS_AND_UNUSED:
+            // Fingerprint exists but is marked as unused - update the used 
flag
+            log.info(

Review Comment:
   Can we move the branches into separate methods?
   - `markIndexingStateAsUsed`
   - `insertIndexingState`.



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