jdeppe-pivotal commented on a change in pull request #6862: URL: https://github.com/apache/geode/pull/6862#discussion_r710474622
########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/services/LockingStripedCoordinator.java ########## @@ -0,0 +1,103 @@ +/* + * 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.geode.redis.internal.services; + +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +import org.apache.geode.redis.internal.data.RedisKey; + +/** + * Implements {@link StripedCoordinator} by using {@link ReentrantLock}s synchronization. The thread + * that calls execute will also be the thread that does the work. But it will do it under + * synchronization. The hashCode of the stripeId is used to associate the id with a stripe. + */ +public class LockingStripedCoordinator implements StripedCoordinator { + private static final int DEFAULT_CONCURRENCY_LEVEL = 4093; // use a prime + private final ReentrantLock[] locks; + + public LockingStripedCoordinator() { + this(DEFAULT_CONCURRENCY_LEVEL); + } + + public LockingStripedCoordinator(int concurrencyLevel) { + locks = new ReentrantLock[concurrencyLevel]; + for (int i = 0; i < concurrencyLevel; i++) { + locks[i] = new ReentrantLock(); + } + } + + @Override + public <T> T execute(RedisKey stripeId, Callable<T> callable) { + Lock lock = getLock(stripeId); + try { + lock.lock(); + return callable.call(); + } catch (RuntimeException re) { + throw re; + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + lock.unlock(); + } + } + + @Override + public <T> T execute(List<RedisKey> stripeIds, Callable<T> callable) { + Lock[] locks = getLocks(stripeIds); + try { + for (int i = 0; i < locks.length; i++) { + locks[i].lock(); Review comment: I did wonder about that - since `ReentrantLock.lock()` isn't supposed to throw anything. But better to be safer. -- 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]
