alex-plekhanov commented on a change in pull request #7941:
URL: https://github.com/apache/ignite/pull/7941#discussion_r494843840



##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/BasicRateLimiterTest.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.ignite.internal.util;
+
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Rate limiter tests.
+ */
+public class BasicRateLimiterTest {

Review comment:
       Not included into any test suite

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/managers/encryption/GridEncryptionManager.java
##########
@@ -627,22 +720,104 @@ public void groupKey(int grpId, byte[] encGrpKey) {
         return withMasterKeyChangeReadLock(() -> getSpi().getMasterKeyName());
     }
 
+    /** {@inheritDoc} */
+    @Override public IgniteFuture<Void> changeCacheGroupKey(Collection<String> 
cacheOrGrpNames) {
+        A.notEmpty(cacheOrGrpNames, "cacheOrGrpNames");
+
+        synchronized (opsMux) {
+            if (stopped) {
+                return new IgniteFinishedFutureImpl<>(new 
IgniteException("Cache group key change was rejected. " +
+                    "Node is stopping."));
+            }
+
+            return grpKeyChangeProc.start(cacheOrGrpNames);
+        }
+    }
+
+    /**
+     * @param grpIds Cache group IDs.
+     * @param keyIds Encryption key IDs.
+     * @param keys Encryption keys.
+     * @throws IgniteCheckedException If failed.
+     */
+    protected void changeCacheGroupKeyLocal(int[] grpIds, byte[] keyIds, 
byte[][] keys) throws IgniteCheckedException {
+        Map<Integer, Byte> encryptionStatus = U.newHashMap(grpIds.length);
+
+        for (int i = 0; i < grpIds.length; i++)
+            encryptionStatus.put(grpIds[i], keyIds[i]);
+
+        WALPointer ptr = ctx.cache().context().wal().log(new 
ReencryptionStartRecord(encryptionStatus));
+
+        if (ptr != null)
+            ctx.cache().context().wal().flush(ptr, false);
+
+        for (int i = 0; i < grpIds.length; i++) {
+            int grpId = grpIds[i];
+
+            CacheGroupContext grp = ctx.cache().cacheGroup(grpId);
+
+            if (grp == null)
+                continue;
+
+            int newKeyId = keyIds[i] & 0xff;
+
+            synchronized (metaStorageMux) {
+                // Set new key as key for writing.
+                GroupKey prevGrpKey = grpKeys.changeActiveKey(grpId, newKeyId);
+
+                writeGroupKeysToMetaStore(grpId);
+
+                if (ptr != null) {
+                    grpKeys.reserveWalKey(grpId, prevGrpKey.unsignedId(), 
ctx.cache().context().wal().currentSegment());
+
+                    writeTrackedWalIdxsToMetaStore();
+                }
+            }
+
+            reencryptGroups.put(grpId, pageScanner.pagesCount(grp));
+
+            if (log.isInfoEnabled())
+                log.info("New encryption key for group was added [grpId=" + 
grpId + ", keyId=" + newKeyId + "]");
+        }
+
+        startReencryption(encryptionStatus.keySet());
+    }
+
+    /**
+     * @param grpId Cache group ID.
+     * @return Future that will be completed when reencryption of the 
specified group is finished.
+     */
+    public IgniteInternalFuture<Void> reencryptionFuture(int grpId) {
+        return pageScanner.statusFuture(grpId);
+    }
+
+    /**
+     * @param grpId Cache group ID.
+     * @return {@code True} If the specified cache group should be 
re-encrypted.
+     */
+    public boolean reencryptionRequired(int grpId) {

Review comment:
       All usages of this method want to check that reencryption in progress. 
Let's rename it to something like `reencryptionInProgress`

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/BasicRateLimiterTest.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.ignite.internal.util;
+
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Rate limiter tests.
+ */
+public class BasicRateLimiterTest {
+    /**
+     * Check rate limit with multiple threads.
+     */
+    @Test
+    public void checkLimitMultithreaded() throws Exception {
+        int opsPerSec = 1_000;
+        int totalOps = 10_000;
+
+        BasicRateLimiter limiter = new BasicRateLimiter(opsPerSec);
+
+        int threads = Runtime.getRuntime().availableProcessors();
+
+        CyclicBarrier ready = new CyclicBarrier(threads + 1);
+
+        AtomicInteger cntr = new AtomicInteger();
+
+        IgniteInternalFuture<Long> fut = 
GridTestUtils.runMultiThreadedAsync(() -> {
+            ready.await();
+
+            do {
+                limiter.acquire(1);
+            }
+            while (!Thread.currentThread().isInterrupted() && 
cntr.incrementAndGet() < totalOps);
+
+            return null;
+        }, threads, "worker");
+
+        ready.await();
+
+        long startTime = System.currentTimeMillis();
+
+        fut.get();
+
+        long timeSpent = System.currentTimeMillis() - startTime;
+
+        assertEquals(totalOps / opsPerSec, SECONDS.convert(timeSpent, 
MILLISECONDS));
+    }
+
+    /**
+     * Check that the average speed is limited correctly even if we are 
acquiring more permits than allowed per second.
+     */
+    @Test
+    public void checkAcquireWithOverflow() throws 
IgniteInterruptedCheckedException {
+        double permitsPerSec = 0.5;
+        int permitsPerOp = 1;
+        int totalOps = 5;
+
+        BasicRateLimiter limiter = new BasicRateLimiter(permitsPerSec);
+
+        long startTime = System.currentTimeMillis();
+
+        for (int i = 0; i <= totalOps; i++)

Review comment:
       Actually, you check `totalOps + 1` operations here

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/BasicRateLimiterTest.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.ignite.internal.util;
+
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Rate limiter tests.
+ */
+public class BasicRateLimiterTest {
+    /**
+     * Check rate limit with multiple threads.
+     */
+    @Test
+    public void checkLimitMultithreaded() throws Exception {
+        int opsPerSec = 1_000;
+        int totalOps = 10_000;
+
+        BasicRateLimiter limiter = new BasicRateLimiter(opsPerSec);
+
+        int threads = Runtime.getRuntime().availableProcessors();
+
+        CyclicBarrier ready = new CyclicBarrier(threads + 1);
+
+        AtomicInteger cntr = new AtomicInteger();
+
+        IgniteInternalFuture<Long> fut = 
GridTestUtils.runMultiThreadedAsync(() -> {
+            ready.await();
+
+            do {
+                limiter.acquire(1);
+            }
+            while (!Thread.currentThread().isInterrupted() && 
cntr.incrementAndGet() < totalOps);
+
+            return null;
+        }, threads, "worker");
+
+        ready.await();
+
+        long startTime = System.currentTimeMillis();
+
+        fut.get();
+
+        long timeSpent = System.currentTimeMillis() - startTime;
+
+        assertEquals(totalOps / opsPerSec, SECONDS.convert(timeSpent, 
MILLISECONDS));

Review comment:
       In some circumstances, I think `timeSpent` can be a little bit less than 
10 seconds




----------------------------------------------------------------
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:
[email protected]


Reply via email to