xiangfu0 commented on code in PR #19998:
URL: https://github.com/apache/pinot/pull/19998#discussion_r4356720987


##########
pinot-server/src/test/java/org/apache/pinot/server/api/TablesResourceTest.java:
##########
@@ -534,8 +535,18 @@ private void downloadAndVerifySegmentContent(String 
tableNameWithType, IndexSegm
       throws IOException, ConfigurationException {
     String segmentPath = "/segments/" + tableNameWithType + "/" + 
segment.getSegmentName();
 
+    File segmentDir = new 
File(_tableDataManagerMap.get(tableNameWithType).getTableDataDir(), 
segment.getSegmentName());
+    assertTrue(new File(segmentDir, "segment_metadata.bin").isFile());
+    File temporarySidecar = new File(segmentDir, ".segment_metadata.test.tmp");
+    Files.writeString(temporarySidecar.toPath(), "in progress");
+
     // Download the segment and save to a temp local file.

Review Comment:
   This assertion fails at HEAD. `BaseResourceTest.setUpSegment()` loads 
segments via `ImmutableSegmentLoader.load(dir, ReadMode.mmap)`, whose no-arg 
`IndexLoadingConfig` has `isMappedSegmentMetadataEnabled() == false`, so no 
`segment_metadata.bin` is ever written for these segments. Running the class 
gives `testDownloadSegments ... expected [true] but found [false]` right here. 
Either load the test segments with mapping enabled, or drop this pre-condition 
so the test still exercises the exclusion without requiring the sidecar to 
exist.



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/metadata/MappedColumnMetadata.java:
##########
@@ -0,0 +1,398 @@
+/**
+ * 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.pinot.segment.spi.index.metadata;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.ColumnMetadata;
+import org.apache.pinot.segment.spi.compression.ChunkCompressionType;
+import org.apache.pinot.segment.spi.index.IndexService;
+import org.apache.pinot.segment.spi.index.IndexType;
+import org.apache.pinot.segment.spi.partition.PartitionFunction;
+import org.apache.pinot.segment.spi.partition.PartitionFunctionFactory;
+import org.apache.pinot.spi.config.table.FieldConfig.EncodingType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.utils.JsonUtils;
+
+/// A small view of one fixed record in [MappedSegmentMetadataStore]. No 
per-column values are copied into this view.
+/// Data-source metadata delegates to this live view. Serving callers must 
hold the owning segment's acquired
+/// reference for the full read; the segment manager's reference count delays 
destruction and unmapping until readers
+/// release it. Standalone callers must keep the owning mapped metadata open. 
Reads after close are unsafe.
+public final class MappedColumnMetadata implements ColumnMetadata {
+  private final MappedSegmentMetadataStore _store;
+  private final int _ordinal;
+  @Nullable
+  private volatile PartitionFunction _partitionFunction;
+  @Nullable
+  private volatile Set<Integer> _partitions;
+  @Nullable
+  private volatile Bounds _bounds;
+  @Nullable
+  private volatile IndexSize[] _indexes;
+
+  private static final class Bounds {
+    @Nullable
+    private final Comparable<?> _minimum;
+    @Nullable
+    private final Comparable<?> _maximum;
+
+    private Bounds(@Nullable Comparable<?> minimum, @Nullable Comparable<?> 
maximum) {
+      _minimum = minimum;
+      _maximum = maximum;
+    }
+  }
+
+  MappedColumnMetadata(MappedSegmentMetadataStore store, int ordinal) {
+    _store = store;
+    _ordinal = ordinal;
+  }
+
+  @Override
+  public FieldSpec getFieldSpec() {
+    // Field specs are shared across mapped segments; callers must treat the 
returned spec as read-only.
+    return _store.fieldSpec(_ordinal);
+  }
+
+  @Override
+  public int getTotalDocs() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.TOTAL_DOCS);
+  }
+
+  @Override
+  public int getCardinality() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.CARDINALITY);
+  }
+
+  @Override
+  public boolean hasDictionary() {
+    return _store.flag(_ordinal, MappedSegmentMetadataStore.HAS_DICTIONARY);
+  }
+
+  @Nullable
+  @Override
+  public EncodingType getForwardIndexEncoding() {
+    switch (_store.getInt(_ordinal, MappedSegmentMetadataStore.ENCODING)) {
+      case 0:
+        return null;
+      case 1:
+        return EncodingType.RAW;
+      case 2:
+        return EncodingType.DICTIONARY;
+      default:
+        throw new IllegalStateException("Invalid forward-index encoding");
+    }
+  }
+
+  @Override
+  public boolean isSorted() {
+    return _store.flag(_ordinal, MappedSegmentMetadataStore.SORTED);
+  }
+
+  @Override
+  public boolean isNonNull() {
+    return _store.flag(_ordinal, MappedSegmentMetadataStore.NON_NULL);
+  }
+
+  @Nullable
+  @Override
+  public Comparable<?> getMinValue() {
+    return bounds()._minimum;
+  }
+
+  @Nullable
+  @Override
+  public Comparable<?> getMaxValue() {
+    return bounds()._maximum;
+  }
+
+  private Bounds bounds() {
+    Bounds bounds = _bounds;
+    if (bounds == null) {
+      synchronized (this) {
+        bounds = _bounds;
+        if (bounds == null) {
+          bounds = new Bounds(_store.value(_ordinal, true), 
_store.value(_ordinal, false));
+          _bounds = bounds;
+        }
+      }
+    }
+    return bounds;
+  }
+
+  @Override
+  public boolean isMinMaxValueInvalid() {
+    return _store.flag(_ordinal, MappedSegmentMetadataStore.INVALID_MIN_MAX);
+  }
+
+  @Override
+  public int getLengthOfShortestElement() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.SHORTEST);
+  }
+
+  @Override
+  public int getLengthOfLongestElement() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.LONGEST);
+  }
+
+  @Override
+  public boolean isAscii() {
+    return _store.flag(_ordinal, MappedSegmentMetadataStore.ASCII);
+  }
+
+  @Override
+  public int getBitsPerElement() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.BITS);
+  }
+
+  @Override
+  public int getTotalNumberOfEntries() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.TOTAL_ENTRIES);
+  }
+
+  @Override
+  public int getMaxNumberOfMultiValues() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.MAX_MV);
+  }
+
+  @Override
+  public int getMaxRowLengthInBytes() {
+    return _store.getInt(_ordinal, MappedSegmentMetadataStore.MAX_ROW);
+  }
+
+  @Nullable
+  @Override
+  public PartitionFunction getPartitionFunction() {
+    PartitionFunction cached = _partitionFunction;

Review Comment:
   Minor: when a column legitimately has no partition info these decode to 
null, and since the cache field stays null every call re-enters the 
synchronized block and re-decodes. `_indexes` avoids this; a small sentinel for 
the null case here would match it.



##########
pinot-server/src/main/java/org/apache/pinot/server/api/resources/TablesResource.java:
##########
@@ -1101,11 +1108,24 @@ private File createSegmentTarFile(TableDataManager 
tableDataManager, String segm
     File segmentTarFile = 
org.apache.pinot.common.utils.FileUtils.concatAndValidateFile(segmentTarUploadDir,
         tableNameWithType + "_" + segmentName + "_" + UUID.randomUUID() + 
TarCompressionUtils.TAR_GZ_FILE_EXTENSION,
         "Invalid table / segment name: %s, %s", tableNameWithType, 
segmentName);
-    TarCompressionUtils.createCompressedTarFile(new 
File(tableDataManager.getTableDataDir(), segmentName),
-        segmentTarFile);
+    createSegmentTransferTarFile(new File(tableDataManager.getTableDataDir(), 
segmentName), segmentTarFile);
     return segmentTarFile;
   }
 
+  private static void createSegmentTransferTarFile(File segmentDir, File 
segmentTarFile)
+      throws IOException {
+    TarCompressionUtils.createFilteredCompressedTarFile(segmentDir, 
segmentTarFile, file -> {
+      if (!segmentDir.equals(file.getParentFile())) {
+        return true;
+      }
+      // Keep these names in sync with MappedSegmentMetadataStore.FILE_NAME 
and writeAtomically()'s temp-file pattern.
+      // Both are local serving artifacts, rebuilt from metadata.properties on 
the receiving server.
+      String name = file.getName();
+      return !name.equals("segment_metadata.bin")

Review Comment:
   These literals mirror the package-private 
`MappedSegmentMetadataStore.FILE_NAME` and its temp prefix/suffix, kept in sync 
only by the comment above. If that prefix ever changes, segments would silently 
start shipping sidecar temp files. Worth exposing a shared constant so there's 
one source of truth.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/store/SegmentLocalFSDirectory.java:
##########
@@ -141,11 +141,35 @@ public SegmentMetadataImpl getSegmentMetadata() {
     return _segmentMetadata;
   }
 
+  /// Called only during preprocessing, before the segment is queryable; 
replacing and closing metadata is not
+  /// synchronized with serving readers.
   @Override
   public void reloadMetadata()
       throws Exception {
-    _segmentMetadata = new SegmentMetadataImpl(_indexDir);
-    _columnIndexDirectory.setSegmentMetadata(_segmentMetadata);
+    SegmentMetadataImpl metadata = new SegmentMetadataImpl(_indexDir);
+    _columnIndexDirectory.setSegmentMetadata(metadata);
+    SegmentMetadataImpl previous = _segmentMetadata;
+    _segmentMetadata = metadata;
+    previous.close();
+  }
+
+  /// Replaces mutable metadata with the mapped representation after 
preprocessing and before the segment is queryable.
+  public void loadMappedMetadata()
+      throws IOException, ConfigurationException {

Review Comment:
   Nit: the failure is now caught and logged internally and never rethrown, so 
`throws IOException, ConfigurationException` here is dead. Fine to keep for API 
stability, but the signature now reads as if it can propagate when it can't.



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