ptupitsyn commented on code in PR #3146: URL: https://github.com/apache/ignite-3/pull/3146#discussion_r1479902880
########## modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/ReflectionMarshallersProvider.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.ignite.internal.marshaller; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Arrays; +import java.util.Objects; +import java.util.function.Function; +import org.apache.ignite.table.mapper.Mapper; + +/** Implementation of {@link MarshallersProvider}. */ +public class ReflectionMarshallersProvider implements MarshallersProvider { + /** Marshaller cache size per type. */ + private static final int CACHE_SIZE = 1024; + + /** Marshaller for key columns of a particular schema. Cached by schema version. */ + private final MarshallerCache keysMarshallerCache; + + /** Marshaller for value columns of a particular schema. Cached by schema version. */ + private final MarshallerCache valuesMarshallerCache; + + /** Marshaller for all columns of a particular schema. Cached by schema version. */ + private final MarshallerCache rowMarshallerCache; + + /** Marshaller for an arbitrary columns. Cached by columns. */ + private final MarshallerCache projectionMarshallerCache; + + /** Constructor. */ + public ReflectionMarshallersProvider() { + this.keysMarshallerCache = new MarshallerCache(CACHE_SIZE); + this.valuesMarshallerCache = new MarshallerCache(CACHE_SIZE); + this.rowMarshallerCache = new MarshallerCache(CACHE_SIZE); + this.projectionMarshallerCache = new MarshallerCache(CACHE_SIZE); + } + + /** {@inheritDoc} */ + @Override + public Marshaller getKeysMarshaller( + MarshallerSchema schema, + Mapper<?> mapper, + boolean requireAllFields, + boolean allowUnmappedFields) { + + MarshallerCacheKey key = new MarshallerCacheKey(schema.schemaVersion(), mapper, requireAllFields, allowUnmappedFields); + + return keysMarshallerCache.put(key, k -> { + return Marshaller.createMarshaller(schema.keys(), key.mapper, key.requireAllFields, key.allowUnmappedFields); + }); + } + + /** {@inheritDoc} */ + @Override + public Marshaller getValuesMarshaller( + MarshallerSchema schema, + Mapper<?> mapper, + boolean requireAllFields, + boolean allowUnmappedFields) { + + MarshallerCacheKey key = new MarshallerCacheKey(schema.schemaVersion(), mapper, requireAllFields, allowUnmappedFields); + + return valuesMarshallerCache.put(key, k -> { + return Marshaller.createMarshaller(schema.values(), key.mapper, key.requireAllFields, key.allowUnmappedFields); + }); + } + + /** {@inheritDoc} */ + @Override + public Marshaller getRowMarshaller( + MarshallerSchema schema, + Mapper<?> mapper, + boolean requireAllFields, + boolean allowUnmappedFields) { + + MarshallerCacheKey key = new MarshallerCacheKey(schema.schemaVersion(), mapper, requireAllFields, allowUnmappedFields); + + return rowMarshallerCache.put(key, k -> { + return Marshaller.createMarshaller(schema.row(), key.mapper, key.requireAllFields, key.allowUnmappedFields); + }); + } + + /** {@inheritDoc} */ + @Override + public Marshaller getMarshaller( + MarshallerColumn[] columns, + Mapper<?> mapper, + boolean requireAllFields, + boolean allowUnmappedFields) { + + MarshallerCacheKey key = new MarshallerCacheKey(columns, mapper, requireAllFields, allowUnmappedFields); + + return projectionMarshallerCache.put(key, k -> { + return Marshaller.createMarshaller(k.columns, k.mapper, k.requireAllFields, k.allowUnmappedFields); + }); + } + + private static class MarshallerCache { + + private final Cache<MarshallerCacheKey, Marshaller> cache; + + MarshallerCache(int maximumSize) { + cache = Caffeine.newBuilder() + .maximumSize(maximumSize) + .build(); + } + + Marshaller put(MarshallerCacheKey key, Function<MarshallerCacheKey, Marshaller> func) { Review Comment: ```suggestion Marshaller getOrAdd(MarshallerCacheKey key, Function<MarshallerCacheKey, Marshaller> func) { ``` `put` is quite confusing here. ########## modules/marshaller-common/src/main/java/org/apache/ignite/internal/marshaller/ReflectionMarshallersProvider.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.ignite.internal.marshaller; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Arrays; +import java.util.Objects; +import java.util.function.Function; +import org.apache.ignite.table.mapper.Mapper; + +/** Implementation of {@link MarshallersProvider}. */ +public class ReflectionMarshallersProvider implements MarshallersProvider { + /** Marshaller cache size per type. */ + private static final int CACHE_SIZE = 1024; Review Comment: I think 1024 per type is too many. Normally we only use the latest schema, maybe 2 or 3 previous during schema migration. It does not make sense to cache more than ~5 schemas per type. ########## modules/client/src/main/java/org/apache/ignite/internal/client/table/ClientSchema.java: ########## @@ -231,4 +260,55 @@ private static BinaryMode mode(ColumnType dataType) { throw new IgniteException(Client.PROTOCOL_ERR, "Unknown client data type: " + dataType); } } + + private MarshallerSchema marshallerSchema() { + if (marshallerSchema == null) { + marshallerSchema = new ClientSideMarshallerSchema(this); + } + return marshallerSchema; + } + + private static class ClientSideMarshallerSchema implements MarshallerSchema { Review Comment: ```suggestion private static class ClientMarshallerSchema implements MarshallerSchema { ``` ########## modules/runner/src/integrationTest/java/org/apache/ignite/internal/benchmark/SelectBenchmark.java: ########## @@ -78,7 +78,7 @@ public class SelectBenchmark extends AbstractMultiNodeBenchmark { private KeyValueView<Tuple, Tuple> keyValueView; - @Param({"1", "2", "3"}) + @Param({"1"})//, "2", "3"}) Review Comment: Revert? -- 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]
