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


##########
server/src/main/java/org/apache/druid/segment/metadata/KillUnreferencedSegmentSchemas.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.google.inject.Inject;
+import org.apache.druid.guice.LazySingleton;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.metadata.SegmentsMetadataManager;
+
+import java.util.List;
+
+/**
+ * This class deals with cleaning schema which is not referenced by any used 
segment.
+ * <p>
+ * <ol>
+ * <li>If a schema is not referenced, UPDATE schemas SET used = false, 
used_status_last_updated = now</li>
+ * <li>DELETE FROM schemas WHERE used = false AND used_status_last_updated < 6 
hours ago</li>
+ * <li>When creating a new segment, try to find schema for the fingerprint of 
the segment.</li>
+ *    <ol type="a">
+ *    <li> If no record found, create a new one.</li>
+ *    <li> If record found which has used = true, reuse this schema_id.</li>
+ *    <li> If record found which has used = false, UPDATE SET used = true, 
used_status_last_updated = now</li>
+ *    </ol>
+ * </ol>
+ * </p>
+ * <p>
+ * Possible race conditions:
+ *    <ol type="a">
+ *    <li> Between ops 1 and 3b: In other words, we might end up with a 
segment that points to a schema that has just been marked as unused. This can 
be repaired by the coordinator duty. </li>
+ *    <li> Between 2 and 3c: This can be handled. Either 2 will fail to update 
any rows (good case) or 3c will fail to update any rows and thus return 0 (bad 
case). In the bad case, we need to recreate the schema, same as step 3a. </li>
+ *    </ol>
+ * </p>
+ */
+@LazySingleton
+public class KillUnreferencedSegmentSchemas
+{
+  private static final EmittingLogger log = new 
EmittingLogger(KillUnreferencedSegmentSchemas.class);
+  private final SegmentSchemaManager segmentSchemaManager;
+  private final SegmentsMetadataManager metadataManager;
+
+  @Inject
+  public KillUnreferencedSegmentSchemas(
+      SegmentSchemaManager segmentSchemaManager,
+      SegmentsMetadataManager metadataManager
+  )
+  {
+    this.segmentSchemaManager = segmentSchemaManager;
+    this.metadataManager = metadataManager;
+  }
+
+  public int cleanup(long timestamp)
+  {
+    // 1: Identify unreferenced schema and mark them as unused. These will get 
deleted after a fixed period.
+    int unused = segmentSchemaManager.identifyAndMarkSchemaUnused();
+    log.info("Identified [%s] unreferenced schema. Marking them as unused.", 
unused);

Review Comment:
   At this point, you have already marked the schema as unused. It is also 
possible that the method above returned 0. So we should log only if the 
numUpdated > 0.



##########
server/src/main/java/org/apache/druid/segment/metadata/KillUnreferencedSegmentSchemas.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.google.inject.Inject;
+import org.apache.druid.guice.LazySingleton;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.metadata.SegmentsMetadataManager;
+
+import java.util.List;
+
+/**
+ * This class deals with cleaning schema which is not referenced by any used 
segment.
+ * <p>
+ * <ol>
+ * <li>If a schema is not referenced, UPDATE schemas SET used = false, 
used_status_last_updated = now</li>
+ * <li>DELETE FROM schemas WHERE used = false AND used_status_last_updated < 6 
hours ago</li>
+ * <li>When creating a new segment, try to find schema for the fingerprint of 
the segment.</li>
+ *    <ol type="a">
+ *    <li> If no record found, create a new one.</li>
+ *    <li> If record found which has used = true, reuse this schema_id.</li>
+ *    <li> If record found which has used = false, UPDATE SET used = true, 
used_status_last_updated = now</li>
+ *    </ol>
+ * </ol>
+ * </p>
+ * <p>
+ * Possible race conditions:
+ *    <ol type="a">
+ *    <li> Between ops 1 and 3b: In other words, we might end up with a 
segment that points to a schema that has just been marked as unused. This can 
be repaired by the coordinator duty. </li>
+ *    <li> Between 2 and 3c: This can be handled. Either 2 will fail to update 
any rows (good case) or 3c will fail to update any rows and thus return 0 (bad 
case). In the bad case, we need to recreate the schema, same as step 3a. </li>
+ *    </ol>
+ * </p>
+ */
+@LazySingleton
+public class KillUnreferencedSegmentSchemas
+{
+  private static final EmittingLogger log = new 
EmittingLogger(KillUnreferencedSegmentSchemas.class);
+  private final SegmentSchemaManager segmentSchemaManager;
+  private final SegmentsMetadataManager metadataManager;
+
+  @Inject
+  public KillUnreferencedSegmentSchemas(
+      SegmentSchemaManager segmentSchemaManager,
+      SegmentsMetadataManager metadataManager
+  )
+  {
+    this.segmentSchemaManager = segmentSchemaManager;
+    this.metadataManager = metadataManager;
+  }
+
+  public int cleanup(long timestamp)
+  {
+    // 1: Identify unreferenced schema and mark them as unused. These will get 
deleted after a fixed period.
+    int unused = segmentSchemaManager.identifyAndMarkSchemaUnused();
+    log.info("Identified [%s] unreferenced schema. Marking them as unused.", 
unused);

Review Comment:
   ```suggestion
       int numSchemasMarkedAsUnused = 
segmentSchemaManager.identifyAndMarkSchemaUnused();
       if (numSchemasMarkedAsUnused > 0)
           log.info("Marked [%d] unreferenced schemas as unused.", 
numSchemasMarkedAsUnused);
   ```



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