You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/03/29 20:48:26 UTC

[GitHub] [kafka] dhruvilshah3 commented on a change in pull request #10401: KAFKA-12552: Introduce LogSegments class abstracting the segments map

dhruvilshah3 commented on a change in pull request #10401:
URL: https://github.com/apache/kafka/pull/10401#discussion_r603597075



##########
File path: core/src/main/scala/kafka/log/LogSegments.scala
##########
@@ -0,0 +1,220 @@
+/**
+ * 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 kafka.log
+
+import java.io.File
+import java.lang.{Long => JLong}
+import java.util.Map
+import java.util.concurrent.{ConcurrentNavigableMap, ConcurrentSkipListMap}
+
+import kafka.utils.threadsafe
+import org.apache.kafka.common.TopicPartition
+
+import scala.jdk.CollectionConverters._
+
+/**
+ * This class encapsulates a thread-safe navigable map of LogSegment instances and provides the
+ * required read and write behavior on the map.
+ *
+ * @param topicPartition the TopicPartition associated with the segments
+ *                        (useful for logging purposes)
+ */
+class LogSegments(topicPartition: TopicPartition) {
+
+  /* the segments of the log with key being LogSegment base offset and value being a LogSegment */
+  private val segments: ConcurrentNavigableMap[java.lang.Long, LogSegment] = new ConcurrentSkipListMap[java.lang.Long, LogSegment]
+
+  /**
+   * @return true if the segments are empty, false otherwise.
+   */
+  @threadsafe
+  def isEmpty: Boolean = segments.isEmpty
+
+  /**
+   * @return true if the segments are non-empty, false otherwise.
+   */
+  @threadsafe
+  def nonEmpty: Boolean = !isEmpty
+
+  /**
+   * Add the given segment, or replace an existing entry.
+   *
+   * @param segment the segment to add
+   */
+  @threadsafe
+  def add(segment: LogSegment): LogSegment = this.segments.put(segment.baseOffset, segment)
+
+  /**
+   * Remove the segment at the provided offset.
+   *
+   * @param offset the offset to be removed
+   */
+  @threadsafe
+  def remove(offset: Long): Unit = segments.remove(offset)
+
+  /**
+   * Clears all entries.
+   */
+  @threadsafe
+  def clear(): Unit = segments.clear()
+
+  /**
+   * Close all segments.
+   */
+  def close(): Unit = values.foreach(_.close())
+
+  /**
+   * Close the handlers for all segments.
+   */
+  def closeHandlers(): Unit = values.foreach(_.closeHandlers())
+
+  /**
+   * Update the directory reference for the log and indices of all segments.
+   *
+   * @param dir the renamed directory
+   */
+  def updateParentDir(dir: File): Unit = values.foreach(_.updateParentDir(dir))
+
+  /**
+   * Take care! this is an O(n) operation, where n is the number of segments.
+   *
+   * @return The number of segments.
+   *
+   */
+  @threadsafe
+  def numberOfSegments: Int = segments.size
+
+  /**
+   * @return the base offsets of all segments
+   */
+  def baseOffsets: Seq[Long] = segments.values().asScala.map(_.baseOffset).toSeq

Review comment:
       nit: should this return an `Iterable` instead of `Seq`?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org