dimas-b commented on code in PR #1339: URL: https://github.com/apache/polaris/pull/1339#discussion_r2035428776
########## jcstress-tests/src/jcstress/java/org/apache/polaris/core/persistence/cache/FakeMetaStoreManager.java: ########## @@ -0,0 +1,309 @@ +/* + * 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.polaris.core.persistence.cache; + +import static java.util.Collections.emptyList; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.entity.*; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.dao.entity.*; +import org.apache.polaris.core.storage.PolarisStorageActions; + +/** + * Build a fake meta store manager that returns a single catalog entity. Every time the catalog is + * returned, an incremented entity version id is used. this is in order to disambiguate the entity + * returned by the cache and verify whether the cache is thread safe. + * + * <p>Think of it as a poor-man's Mockito. It is not a real mock, but it is good enough for this + * test. More importantly, it is a lot faster to instantiate than Mockito. It allows the stress test + * to run up to 100x more iterations in the same amount of time. + * + * <p>The only implemented methods are `loadResolvedEntityById` and `loadResolvedEntityByName`. Any + * time they are invoked, regardless of their parameters, the same catalog is returned but with an + * increased entity version id. + */ +public class FakeMetaStoreManager implements PolarisMetaStoreManager { + public static final int CATALOG_ID = 42; + private final Supplier<ResolvedEntityResult> catalogSupplier; + + public FakeMetaStoreManager() { + final AtomicInteger versionCounter = new AtomicInteger(1); + this.catalogSupplier = + () -> { + int version = versionCounter.getAndIncrement(); + CatalogEntity catalog = + new CatalogEntity.Builder() + .setId(CATALOG_ID) + .setInternalProperties(Map.of()) + .setProperties(Map.of()) + .setName("test") + .setParentId(PolarisEntityConstants.getRootEntityId()) + .setEntityVersion(version) + .build(); + return new ResolvedEntityResult(catalog, version, emptyList()); + }; + } + + /** + * Utility method that ensures that catalogs creation is thread safe. + * + * @return a catalog entity with an incremented version id + */ + private synchronized ResolvedEntityResult nextResult() { Review Comment: I'm not sure it adds correctness even to the old cache test. How the cache works should not be a reason for synchronizing or not on the Meta Store impl (in principle). The `synchronized` block will probably make it extremely likely for the entity that increments the version counter first to also be returned first, but it is not guaranteed, I think :thinking: ... at least the caller may not be able to process the returned values in the same order. -- 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]
