You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "Jackie-Jiang (via GitHub)" <gi...@apache.org> on 2023/02/28 23:05:22 UTC

[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #10191: [Index SPI] IndexType

Jackie-Jiang commented on code in PR #10191:
URL: https://github.com/apache/pinot/pull/10191#discussion_r1120876913


##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/FieldIndexConfigs.java:
##########
@@ -0,0 +1,121 @@
+/**
+ * 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;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.config.table.IndexConfig;
+
+
+/**
+ * FieldIndexConfigs are a map like structure that relates index types with their configuration, providing a type safe
+ * interface.
+ *
+ * This class can be serialized into a JSON object whose keys are the index type ids using Jackson, but cannot be
+ * serialized back. A custom Jackson deserializer could be provided if needed.
+ */
+public class FieldIndexConfigs {
+
+  public static final FieldIndexConfigs EMPTY = new FieldIndexConfigs(new HashMap<>());
+
+  private final Map<IndexType, IndexConfig> _configMap;
+
+  private FieldIndexConfigs(Map<IndexType, IndexConfig> configMap) {
+    _configMap = Collections.unmodifiableMap(configMap);
+  }
+
+  /**
+   * Returns the configuration associated with the given index type, which will be null if there is no configuration for
+   * that index type.
+   */
+  @JsonIgnore
+  public <C extends IndexConfig, I extends IndexType<C, ?, ?>> C getConfig(I indexType) {
+    IndexConfig config = _configMap.get(indexType);
+    if (config == null) {
+      return indexType.getDefaultConfig();
+    }
+    return (C) config;
+  }
+
+  /*
+  This is used by Jackson when this object is serialized. Each entry of the map will be directly contained in the
+  JSON object, with the key name as the key in the JSON object and the result of serializing the key value as the value
+  in the JSON object.
+   */
+  @JsonAnyGetter
+  public Map<String, JsonNode> unwrapIndexes() {
+    Function<Map.Entry<IndexType, IndexConfig>, JsonNode> serializer =
+        entry -> entry.getKey().serialize(entry.getValue());
+    return _configMap.entrySet().stream()
+        .filter(e -> e.getValue() != null)
+        .collect(Collectors.toMap(entry -> entry.getKey().getId(), serializer));
+  }
+
+  public static class Builder {
+    private final Map<IndexType, IndexConfig> _configMap;
+
+    public Builder() {
+      _configMap = new HashMap<>();
+    }
+
+    public Builder(FieldIndexConfigs other) {
+      _configMap = new HashMap<>(other._configMap);
+    }
+
+    public <C extends IndexConfig, I extends IndexType<C, ?, ?>> Builder add(I indexType, @Nullable C config) {

Review Comment:
   Do we allow adding `null` config?



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexCreator.java:
##########
@@ -0,0 +1,62 @@
+/**
+ * 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;
+
+import java.io.Closeable;
+import java.io.IOException;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+
+/**
+ * The interface used to create indexes.
+ *
+ * The lifecycle for an IndexCreator is to be created, receive one or more calls to either
+ * {@link #addSingleValueCell(Object, int)} or {@link #addMultiValueCell(Object[], int[])} (but not
+ * mix them),
+ * a call to {@link #seal()} and finally be closed. Calls to add cell methods must be done in document id order,
+ * starting from the first document id.
+ */
+public interface IndexCreator extends Closeable {
+  /**
+   * Adds the given single value cell to the index.
+   *
+   * Rows will be added in docId order, starting with the one with docId 0.
+   *
+   * @param value The nonnull value of the cell. In case the cell was actually null, a default value is received instead
+   * @param dictId An optional dictionary value of the cell. If there is no dictionary, -1 is received
+   */
+  void addSingleValueCell(@Nonnull Object value, int dictId)

Review Comment:
   Suggest renaming it to `add()` which is consistent with the existing index creators. Whether it is SV or MV is implicit from the argument type. Currently we don't have the term `cell` but use `entry`



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/FieldIndexConfigs.java:
##########
@@ -0,0 +1,121 @@
+/**
+ * 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;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.config.table.IndexConfig;
+
+
+/**
+ * FieldIndexConfigs are a map like structure that relates index types with their configuration, providing a type safe
+ * interface.
+ *
+ * This class can be serialized into a JSON object whose keys are the index type ids using Jackson, but cannot be
+ * serialized back. A custom Jackson deserializer could be provided if needed.
+ */
+public class FieldIndexConfigs {
+
+  public static final FieldIndexConfigs EMPTY = new FieldIndexConfigs(new HashMap<>());
+
+  private final Map<IndexType, IndexConfig> _configMap;
+
+  private FieldIndexConfigs(Map<IndexType, IndexConfig> configMap) {
+    _configMap = Collections.unmodifiableMap(configMap);
+  }
+
+  /**
+   * Returns the configuration associated with the given index type, which will be null if there is no configuration for
+   * that index type.
+   */
+  @JsonIgnore
+  public <C extends IndexConfig, I extends IndexType<C, ?, ?>> C getConfig(I indexType) {
+    IndexConfig config = _configMap.get(indexType);
+    if (config == null) {
+      return indexType.getDefaultConfig();
+    }
+    return (C) config;
+  }
+
+  /*
+  This is used by Jackson when this object is serialized. Each entry of the map will be directly contained in the
+  JSON object, with the key name as the key in the JSON object and the result of serializing the key value as the value
+  in the JSON object.
+   */
+  @JsonAnyGetter

Review Comment:
   I'm a little bit confused here. Do we ever need to serialize this class into a JSON? I feel this class should act just as a holder for all the deserialized configs, where the deserialization happens outside of this class



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexType.java:
##########
@@ -0,0 +1,171 @@
+/**
+ * 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;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.ColumnMetadata;
+import org.apache.pinot.segment.spi.creator.IndexCreationContext;
+import org.apache.pinot.segment.spi.index.column.ColumnIndexContainer;
+import org.apache.pinot.segment.spi.store.SegmentDirectory;
+import org.apache.pinot.spi.config.table.FieldConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.utils.JsonUtils;
+
+
+/**
+ * TODO: implement mutable indexes.
+ * @param <C> the class that represents how this object is configured.
+ * @param <IR> the {@link IndexReader} subclass that should be used to read indexes of this type.
+ * @param <IC> the {@link IndexCreator} subclass that should be used to create indexes of this type.
+ */
+public interface IndexType<C, IR extends IndexReader, IC extends IndexCreator> {
+
+  /**
+   * The unique id that identifies this index type.
+   * In case there is more than one implementation for a given index, then all should share the same id in order to be
+   * correctly registered in the {@link IndexService}.
+   * This is also the value being used as the default toString implementation and the one used as keys when config is
+   * specified.
+   *
+   * <p>Therefore the returned value for each index should be constant across different Pinot versions.</p>
+   */
+  String getId();

Review Comment:
   But in `ColumnIndexType`, the `_indexName` is always the lower case of the enum value. Can we always use `IndexName`? That can avoid a lot of confusion. If you really think we need 2 different names, we can call the internal one `getInternalName()`



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexReaderFactory.java:
##########
@@ -0,0 +1,37 @@
+/**
+ * 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;
+
+import java.io.IOException;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.ColumnMetadata;
+import org.apache.pinot.segment.spi.store.SegmentDirectory;
+
+
+public interface IndexReaderFactory<R extends IndexReader> {
+
+  /**
+   * @throws IndexReaderConstraintException if the constraints of the index reader are not matched. For example, some
+   * indexes may require the column to be dictionary based.
+   */
+  @Nullable
+  R read(SegmentDirectory.Reader segmentReader, FieldIndexConfigs fieldIndexConfigs, ColumnMetadata metadata)

Review Comment:
   Suggest renaming to `getIndexReader()`



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexType.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.ColumnMetadata;
+import org.apache.pinot.segment.spi.creator.IndexCreationContext;
+import org.apache.pinot.segment.spi.index.column.ColumnIndexContainer;
+import org.apache.pinot.segment.spi.store.SegmentDirectory;
+import org.apache.pinot.spi.config.table.IndexConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.utils.JsonUtils;
+
+
+/**
+ * TODO: implement mutable indexes.
+ * @param <C> the class that represents how this object is configured.
+ * @param <IR> the {@link IndexReader} subclass that should be used to read indexes of this type.
+ * @param <IC> the {@link IndexCreator} subclass that should be used to create indexes of this type.
+ */
+public interface IndexType<C extends IndexConfig, IR extends IndexReader, IC extends IndexCreator> {
+
+  /**
+   * The unique id that identifies this index type.
+   * In case there is more than one implementation for a given index, then all should share the same id in order to be
+   * correctly registered in the {@link IndexService}.
+   * This is also the value being used as the default toString implementation and the one used as keys when config is
+   * specified.
+   *
+   * <p>Therefore the returned value for each index should be constant across different Pinot versions.</p>
+   */
+  String getId();
+
+  /**
+   * Returns an internal name used in some parts of the code (mainly in format v1 and metadata) that is persisted on
+   * disk.
+   *
+   * <p>Therefore the returned value for each index should be constant across different Pinot versions.</p>
+   */
+  String getIndexName();
+
+  Class<C> getIndexConfigClass();
+
+  /**
+   * The default config when it is not explicitly defined by the user.
+   */
+  C getDefaultConfig();
+
+  C deserialize(TableConfig tableConfig, Schema schema);

Review Comment:
   This is not really deserialize. Suggest renaming it to `C getConfig(TableConfig tableConfig, Schema schema);



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/IndexConfig.java:
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.spi.config.table;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.pinot.spi.config.BaseJsonConfig;
+
+
+/**
+ * This is the base class used to configure indexes.
+ *
+ * The common logic between all indexes is that they can be enabled or disabled.
+ *
+ * Indexes that do not require extra configuration can directly use this class.
+ */
+public class IndexConfig extends BaseJsonConfig {
+  public static final IndexConfig ENABLED = new IndexConfig(true);
+  public static final IndexConfig DISABLED = new IndexConfig(false);
+  private final boolean _enabled;

Review Comment:
   Probably make it `_disabled` because in most cases the index should be enabled when configured.



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexType.java:
##########
@@ -0,0 +1,134 @@
+/**
+ * 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;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.ColumnMetadata;
+import org.apache.pinot.segment.spi.creator.IndexCreationContext;
+import org.apache.pinot.segment.spi.index.column.ColumnIndexContainer;
+import org.apache.pinot.segment.spi.store.SegmentDirectory;
+import org.apache.pinot.spi.config.table.IndexConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.utils.JsonUtils;
+
+
+/**
+ * TODO: implement mutable indexes.
+ * @param <C> the class that represents how this object is configured.
+ * @param <IR> the {@link IndexReader} subclass that should be used to read indexes of this type.
+ * @param <IC> the {@link IndexCreator} subclass that should be used to create indexes of this type.
+ */
+public interface IndexType<C extends IndexConfig, IR extends IndexReader, IC extends IndexCreator> {
+
+  /**
+   * The unique id that identifies this index type.
+   * In case there is more than one implementation for a given index, then all should share the same id in order to be
+   * correctly registered in the {@link IndexService}.

Review Comment:
   There are some links no longer apply in this class. Please update them accordingly



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/IndexHandler.java:
##########
@@ -0,0 +1,48 @@
+/**
+ * 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;
+
+import org.apache.pinot.segment.spi.store.SegmentDirectory;
+
+
+/**
+ * Interface for index handlers, which update the corresponding type of indices,
+ * like adding, removing or converting the format.
+ */
+public interface IndexHandler {
+  /**
+   * Adds new indices and removes obsolete indices.
+   */
+  void updateIndices(SegmentDirectory.Writer segmentWriter)
+      throws Exception;
+
+  /**
+   * Check if there is a need to add new indices or removes obsolete indices.
+   * @return true if there is a need to update.
+   */
+  boolean needUpdateIndices(SegmentDirectory.Reader segmentReader)

Review Comment:
   Just realize this is copy-pasted from the class in segment local, but the original `IndexHandler` is modified recently..
   Let's directly move the class to segment spi since there are very few usages of it



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org