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


##########
server/src/main/java/org/apache/druid/metadata/segment/CachedSegmentMetadataTransaction.java:
##########
@@ -0,0 +1,353 @@
+/*
+ * 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.metadata.segment;
+
+import org.apache.druid.discovery.DruidLeaderSelector;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.InternalServerError;
+import org.apache.druid.metadata.PendingSegmentRecord;
+import org.apache.druid.metadata.segment.cache.DatasourceSegmentCache;
+import org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec;
+import org.apache.druid.server.http.DataSegmentPlus;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.SegmentId;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+import org.skife.jdbi.v2.Handle;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+/**
+ * A {@link SegmentMetadataTransaction} that reads only from the cache and 
sends
+ * writes to the metadata store. If the transaction succeeds, all the writes
+ * made to the metadata store are also committed to the cache in {@link 
#close()}.
+ * The cache is not updated right away in case the transaction needs to be
+ * rolled back. This is okay since we assume that a transaction does not read
+ * what it writes.
+ */
+class CachedSegmentMetadataTransaction implements SegmentMetadataTransaction
+{
+  private final SegmentMetadataTransaction delegate;
+  private final DatasourceSegmentCache metadataCache;
+  private final DruidLeaderSelector leaderSelector;
+
+  private final int startTerm;
+
+  private final AtomicBoolean isRollingBack = new AtomicBoolean(false);
+  private final AtomicBoolean isClosed = new AtomicBoolean(false);
+
+  private final List<Consumer<DatasourceSegmentMetadataWriter>> 
pendingCacheWrites = new ArrayList<>();
+
+  CachedSegmentMetadataTransaction(
+      SegmentMetadataTransaction delegate,
+      DatasourceSegmentCache metadataCache,
+      DruidLeaderSelector leaderSelector
+  )
+  {
+    this.delegate = delegate;
+    this.metadataCache = metadataCache;
+    this.leaderSelector = leaderSelector;
+
+    if (leaderSelector.isLeader()) {
+      this.startTerm = leaderSelector.localTerm();
+    } else {
+      throw InternalServerError.exception("Not leader anymore. Cannot start 
transaction.");
+    }
+  }
+
+  private void verifyStillLeaderWithSameTerm()
+  {
+    if (!isLeaderWithSameTerm()) {
+      throw InternalServerError.exception("Not leader anymore. Failing 
transaction.");
+    }
+  }
+
+  private boolean isLeaderWithSameTerm()
+  {
+    return leaderSelector.isLeader() && startTerm == 
leaderSelector.localTerm();
+  }
+
+  @Override
+  public Handle getHandle()
+  {
+    return delegate.getHandle();
+  }
+
+  @Override
+  public void setRollbackOnly()
+  {
+    isRollingBack.set(true);
+    delegate.setRollbackOnly();
+  }
+
+  @Override
+  public void close()
+  {
+    if (isClosed.get()) {
+      return;
+    } else if (isRollingBack.get()) {
+      isClosed.set(true);
+      return;
+    }
+
+    // Commit the changes to the cache
+    try {
+      pendingCacheWrites.forEach(action -> {
+        if (isLeaderWithSameTerm()) {
+          action.accept(metadataCache);
+        } else {
+          // Leadership has been lost, cache would have been stopped and 
invalidated

Review Comment:
   This is to retain current behaviour: if leadership is lost while a SQL 
transaction is in progress, the transaction proceeds unaffected.
   So here in the cached transaction, we choose to commit the SQL transaction 
but just don't update the cache.



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