dschneider-pivotal commented on a change in pull request #6862: URL: https://github.com/apache/geode/pull/6862#discussion_r710400966
########## 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(); Review comment: if lock throws an exception then this method's finally block will still call unlock. That doesn't seem correct. ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/RegionProvider.java ########## @@ -124,11 +125,13 @@ public RegionProvider(InternalCache cache, StripedCoordinator stripedCoordinator dataRegion = redisDataRegionFactory.create(REDIS_DATA_REGION); partitionedRegion = (PartitionedRegion) dataRegion; - stringCommands = new RedisStringCommandsFunctionExecutor(this); - setCommands = new RedisSetCommandsFunctionExecutor(this); - hashCommands = new RedisHashCommandsFunctionExecutor(this); - sortedSetCommands = new RedisSortedSetCommandsFunctionExecutor(this); - keyCommands = new RedisKeyCommandsFunctionExecutor(this); + CacheTransactionManager txManager = cache.getCacheTransactionManager(); + + stringCommands = new RedisStringCommandsFunctionExecutor(this, txManager); Review comment: Instead of passing geode's CacheTransactionManager to all the executors, what do you think of adding a method on the RegionProvider "doAtomically(Runnable)"? This would let you capture in one place our current way of doing a sequence of commands atomically using a geode transaction that does begin, commit and handles rollback. And it does seem related to the RegionProvider because it involves doing region ops atomically. Feel free to name this method something else "doInTransaction?". ########## 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: once again if lock throws an exception we should not call unlock in finally. Also we might have only gotten through part of this lock array when an exception is thrown and then we should only unlock the subset of locks we locked. -- 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]
