Github user ben-manes commented on a diff in the pull request:

    https://github.com/apache/storm/pull/2218#discussion_r129211231
  
    --- Diff: 
storm-client/src/jvm/org/apache/storm/topology/SimpleWindowPartitionCache.java 
---
    @@ -0,0 +1,182 @@
    +/**
    + * 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.storm.topology;
    +
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.Map;
    +import java.util.concurrent.ConcurrentMap;
    +import java.util.concurrent.ConcurrentSkipListMap;
    +import java.util.concurrent.locks.ReentrantLock;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * A simple implementation that evicts the largest un-pinned entry from 
the cache. This works well
    + * for caching window partitions since the access pattern is mostly 
sequential scans.
    + */
    +public class SimpleWindowPartitionCache<K, V> implements 
WindowPartitionCache<K, V> {
    +    private static final Logger LOG = 
LoggerFactory.getLogger(SimpleWindowPartitionCache.class);
    +
    +    private final ConcurrentSkipListMap<K, V> map = new 
ConcurrentSkipListMap<>();
    +    private final Map<K, Long> pinned = new HashMap<>();
    +    private final long maximumSize;
    +    private final RemovalListener<K, V> removalListener;
    +    private final CacheLoader<K, V> cacheLoader;
    +    private final ReentrantLock lock = new ReentrantLock(true);
    +    private int size;
    +
    +    @Override
    +    public V get(K key) {
    +        return getOrLoad(key, false);
    +    }
    +
    +    @Override
    +    public V getPinned(K key) {
    +        return getOrLoad(key, true);
    +    }
    +
    +    @Override
    +    public boolean unpin(K key) {
    +        LOG.debug("unpin '{}'", key);
    +        boolean res = false;
    +        try {
    +            lock.lock();
    +            if (pinned.compute(key, (k, v) -> v == null ? 0 : v - 1) <= 0) 
{
    +                pinned.remove(key);
    +                res = true;
    +            }
    +        } finally {
    +            lock.unlock();
    +        }
    +        LOG.debug("pinned '{}'", pinned);
    +        return res;
    +    }
    +
    +    @Override
    +    public ConcurrentMap<K, V> asMap() {
    +        return map;
    +    }
    +
    +    @Override
    +    public void invalidate(K key) {
    +        if (isPinned(key)) {
    +            LOG.debug("Entry '{}' is pinned, skipping invalidation", key);
    +        } else {
    +            LOG.debug("Invalidating entry '{}'", key);
    +            V val = map.remove(key);
    +            try {
    +                lock.lock();
    +                --size;
    --- End diff --
    
    if the key isn't in the cache (e.g. concurrent invalidations) then this 
will decrease incorrectly. I think you need to check `val` and skip the locking 
code if not present.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to