Skip to content

Commit

Permalink
GDV-31:[Java][C++]Fixed concurrency issue in cache. (apache#84)
Browse files Browse the repository at this point in the history
* GDV-31:[Java][C++]Fixed concurrency issue in cache.

Modifications were happening in get without a mutex.
Wrote a test to verify and prevent regression.
  • Loading branch information
praveenbingo committed Aug 23, 2018
1 parent fd55d9f commit cb74b04
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
8 changes: 2 additions & 6 deletions cpp/src/gandiva/codegen/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ template <class KeyType, typename ValueType>
class Cache {
public:
Cache(size_t capacity = CACHE_SIZE) : cache_(capacity) {}
ValueType GetCachedModule(KeyType cache_key) {
ValueType GetModule(KeyType cache_key) {
boost::optional<ValueType> result;
result = cache_.get(cache_key);
if (result != boost::none) {
return result.value();
}
mtx_.lock();
result = cache_.get(cache_key);
mtx_.unlock();
return result != boost::none ? result.value() : nullptr;
}

void CacheModule(KeyType cache_key, ValueType module) {
void PutModule(KeyType cache_key, ValueType module) {
mtx_.lock();
cache_.insert(cache_key, module);
mtx_.unlock();
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/codegen/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,
Status::Invalid("configuration cannot be null"));
static Cache<FilterCacheKey, std::shared_ptr<Filter>> cache;
FilterCacheKey cacheKey(schema, configuration, *(condition.get()));
std::shared_ptr<Filter> cachedFilter = cache.GetCachedModule(cacheKey);
std::shared_ptr<Filter> cachedFilter = cache.GetModule(cacheKey);
if (cachedFilter != nullptr) {
*filter = cachedFilter;
return Status::OK();
Expand All @@ -67,7 +67,7 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition,

// Instantiate the filter with the completely built llvm generator
*filter = std::make_shared<Filter>(std::move(llvm_gen), schema, configuration);
cache.CacheModule(cacheKey, *filter);
cache.PutModule(cacheKey, *filter);
return Status::OK();
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/codegen/projector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector &exprs,
// see if equivalent projector was already built
static Cache<ProjectorCacheKey, std::shared_ptr<Projector>> cache;
ProjectorCacheKey cache_key(schema, configuration, exprs);
std::shared_ptr<Projector> cached_projector = cache.GetCachedModule(cache_key);
std::shared_ptr<Projector> cached_projector = cache.GetModule(cache_key);
if (cached_projector != nullptr) {
*projector = cached_projector;
return Status::OK();
Expand Down Expand Up @@ -84,7 +84,7 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector &exprs,
// Instantiate the projector with the completely built llvm generator
*projector = std::shared_ptr<Projector>(
new Projector(std::move(llvm_gen), schema, output_fields, configuration));
cache.CacheModule(cache_key, *projector);
cache.PutModule(cache_key, *projector);
return Status::OK();
}

Expand Down

0 comments on commit cb74b04

Please sign in to comment.