kfaraz commented on code in PR #17653: URL: https://github.com/apache/druid/pull/17653#discussion_r1947547358
########## server/src/main/java/org/apache/druid/metadata/segment/SqlSegmentMetadataTransactionFactory.java: ########## @@ -0,0 +1,162 @@ +/* + * 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 com.fasterxml.jackson.databind.ObjectMapper; +import com.google.inject.Inject; +import org.apache.druid.client.indexing.IndexingService; +import org.apache.druid.discovery.DruidLeaderSelector; +import org.apache.druid.metadata.MetadataStorageTablesConfig; +import org.apache.druid.metadata.SQLMetadataConnector; +import org.apache.druid.metadata.segment.cache.DatasourceSegmentCache; +import org.apache.druid.metadata.segment.cache.SegmentMetadataCache; +import org.skife.jdbi.v2.Handle; +import org.skife.jdbi.v2.TransactionStatus; + +/** + * Factory for {@link SegmentMetadataTransaction}s. If the + * {@link SegmentMetadataCache} is enabled and ready, the transaction may + * read/write from the cache as applicable. + * <p> + * This class serves as a wrapper over the {@link SQLMetadataConnector} to + * perform transactions specific to segment metadata. + */ +public class SqlSegmentMetadataTransactionFactory implements SegmentMetadataTransactionFactory +{ + private static final int QUIET_RETRIES = 3; + private static final int MAX_RETRIES = 10; + + private final ObjectMapper jsonMapper; + private final MetadataStorageTablesConfig tablesConfig; + private final SQLMetadataConnector connector; + private final DruidLeaderSelector leaderSelector; + private final SegmentMetadataCache segmentMetadataCache; + + @Inject + public SqlSegmentMetadataTransactionFactory( + ObjectMapper jsonMapper, + MetadataStorageTablesConfig tablesConfig, + SQLMetadataConnector connector, + @IndexingService DruidLeaderSelector leaderSelector, + SegmentMetadataCache segmentMetadataCache + ) + { + this.jsonMapper = jsonMapper; + this.tablesConfig = tablesConfig; + this.connector = connector; + this.leaderSelector = leaderSelector; + this.segmentMetadataCache = segmentMetadataCache; + } + + public int getMaxRetries() + { + return MAX_RETRIES; + } + + @Override + public <T> T inReadOnlyDatasourceTransaction( + String dataSource, + SegmentMetadataReadTransaction.Callback<T> callback + ) + { + return connector.inReadOnlyTransaction((handle, status) -> { + final SegmentMetadataTransaction sqlTransaction + = createSqlTransaction(dataSource, handle, status); + + if (segmentMetadataCache.isEnabled()) { + final DatasourceSegmentCache datasourceCache + = segmentMetadataCache.getDatasource(dataSource); + final SegmentMetadataReadTransaction cachedTransaction + = new CachedSegmentMetadataTransaction(sqlTransaction, datasourceCache, leaderSelector); + + return datasourceCache.read(() -> executeReadAndClose(cachedTransaction, callback)); + } else { + return executeReadAndClose(createSqlTransaction(dataSource, handle, status), callback); + } + }); + } + + @Override + public <T> T retryDatasourceTransaction( + String dataSource, + SegmentMetadataTransaction.Callback<T> callback + ) + { + return connector.retryTransaction( + (handle, status) -> { + final SegmentMetadataTransaction sqlTransaction + = createSqlTransaction(dataSource, handle, status); + + if (segmentMetadataCache.isEnabled()) { + final DatasourceSegmentCache datasourceCache + = segmentMetadataCache.getDatasource(dataSource); + final SegmentMetadataTransaction cachedTransaction + = new CachedSegmentMetadataTransaction(sqlTransaction, datasourceCache, leaderSelector); + + return datasourceCache.write(() -> executeWriteAndClose(cachedTransaction, callback)); + } else { + return executeWriteAndClose(sqlTransaction, callback); + } + }, + QUIET_RETRIES, + getMaxRetries() + ); + } + + private SegmentMetadataTransaction createSqlTransaction( + String dataSource, + Handle handle, + TransactionStatus transactionStatus + ) + { + return new SqlSegmentMetadataTransaction( + dataSource, + handle, transactionStatus, connector, tablesConfig, jsonMapper + ); + } + + private <T> T executeWriteAndClose( + SegmentMetadataTransaction transaction, + SegmentMetadataTransaction.Callback<T> callback + ) throws Exception + { + try { + return callback.inTransaction(transaction); + } + catch (Exception e) { Review Comment: done. ########## 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."); Review Comment: updated. -- 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]
