You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/10/01 03:56:02 UTC

[GitHub] [arrow] thamht4190 commented on a change in pull request #8023: ARROW-9318: [C++] Parquet encryption key management

thamht4190 commented on a change in pull request #8023:
URL: https://github.com/apache/arrow/pull/8023#discussion_r497969857



##########
File path: cpp/src/parquet/encryption/two_level_cache_with_expiration_test.cc
##########
@@ -0,0 +1,207 @@
+// 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.
+
+#include <chrono>
+#include <thread>
+
+#include <gtest/gtest.h>
+
+#include "arrow/util/concurrent_map.h"
+
+#include "parquet/encryption/two_level_cache_with_expiration.h"
+
+namespace parquet {
+namespace encryption {
+namespace test {
+
+class TwoLevelCacheWithExpirationTest : public ::testing::Test {
+ public:
+  void SetUp() {
+    // lifetime is 1s
+    std::shared_ptr<ConcurrentMap<int>> lifetime_1s =
+        cache_.GetOrCreateInternalCache("lifetime1s", 1000);
+    lifetime_1s->Insert("item1", 1);
+    lifetime_1s->Insert("item2", 2);
+
+    // lifetime is 3s
+    std::shared_ptr<ConcurrentMap<int>> lifetime_3s =
+        cache_.GetOrCreateInternalCache("lifetime3s", 3000);
+    lifetime_3s->Insert("item21", 21);
+    lifetime_3s->Insert("item22", 22);
+  }
+
+ protected:
+  void TaskInsert(int thread_no) {
+    for (int i = 0; i < 20; i++) {
+      std::string token = i % 2 == 0 ? "lifetime1s" : "lifetime3s";
+      uint64_t lifetime_ms = i % 2 == 0 ? 1000 : 3000;
+      std::shared_ptr<ConcurrentMap<int>> internal_cache =
+          cache_.GetOrCreateInternalCache(token, lifetime_ms);
+      std::stringstream ss;
+      ss << "item_" << thread_no << "_" << i;
+      internal_cache->Insert(ss.str(), i);
+      std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    }
+  }
+
+  void TaskClean() {
+    for (int i = 0; i < 20; i++) {
+      cache_.Clear();
+      std::this_thread::sleep_for(std::chrono::milliseconds(20));
+    }
+  }
+
+  TwoLevelCacheWithExpiration<int> cache_;
+};
+
+TEST_F(TwoLevelCacheWithExpirationTest, RemoveExpiration) {
+  std::shared_ptr<ConcurrentMap<int>> lifetime_1s_before_expiration =
+      cache_.GetOrCreateInternalCache("lifetime1s", 1000);
+  ASSERT_EQ(lifetime_1s_before_expiration->size(), 2);
+
+  // wait for 2s, we expect:
+  // lifetime_1s will be expired
+  // lifetime_3s will not be expired
+  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+  // now clear expired items from the cache
+  cache_.RemoveExpiredEntriesFromCache();
+
+  // lifetime_1s (with 2 items) is expired and has been removed from the cache.
+  // Now the cache create a new object which has no item.
+  std::shared_ptr<ConcurrentMap<int>> lifetime_1s =
+      cache_.GetOrCreateInternalCache("lifetime1s", 1000);
+  ASSERT_EQ(lifetime_1s->size(), 0);
+
+  // However, lifetime_1s_before_expiration can still access normally and independently
+  // from the one in cache
+  lifetime_1s_before_expiration->Insert("item3", 3);
+  ASSERT_EQ(lifetime_1s_before_expiration->size(), 3);
+  ASSERT_EQ(lifetime_1s->size(), 0);
+
+  // lifetime_3s is not expired and still contains 2 items.
+  std::shared_ptr<ConcurrentMap<int>> lifetime_3s =
+      cache_.GetOrCreateInternalCache("lifetime3s", 3000);
+  ASSERT_EQ(lifetime_3s->size(), 2);
+}
+
+TEST_F(TwoLevelCacheWithExpirationTest, CleanupPeriodTooBig) {
+  // wait for 2s, now:
+  // lifetime_1s is expired
+  // lifetime_3s isn't expired
+  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
+
+  // if cleanup_period is too big (10s), the expired items may not be removed from cache.
+  cache_.CheckCacheForExpiredTokens(10000);
+
+  // lifetime_1s (with 2 items) is expired but not removed from the cache, still contains

Review comment:
       I think you're right about "what's being tested is useful" which is not. In real use case, developers should pass a small enough value for `cleanup_period` param. I will delete this test.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org