adoroszlai commented on a change in pull request #438: HDDS-2878. Refactor 
MiniOzoneLoadGenerator to add more load generators to chaos testing.
URL: https://github.com/apache/hadoop-ozone/pull/438#discussion_r394937622
 
 

 ##########
 File path: 
hadoop-ozone/fault-injection-test/mini-chaos-tests/src/test/java/org/apache/hadoop/ozone/loadgenerators/RandomLoadGenerator.java
 ##########
 @@ -0,0 +1,82 @@
+/**
+ * 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.hadoop.ozone.loadgenerators;
+
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.hadoop.ozone.MiniOzoneLoadGenerator;
+import org.apache.hadoop.ozone.utils.LoadBucket;
+import org.apache.hadoop.util.ExitUtil;
+import org.apache.hadoop.util.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+/**
+ * Random load generator which writes, read and deletes keys from
+ * the bucket.
+ */
+public class RandomLoadGenerator implements LoadGenerator {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(RandomLoadGenerator.class);
+
+  private final List<LoadBucket> ozoneBuckets;
+  private final DataBuffer dataBuffer;
+
+  public RandomLoadGenerator(DataBuffer dataBuffer, List<LoadBucket> buckets) {
+    this.ozoneBuckets = buckets;
+    this.dataBuffer = dataBuffer;
+  }
+
+  // Start IO load on an Ozone bucket.
+  public void startLoad(long runTimeMillis) {
+    long threadID = Thread.currentThread().getId();
+    LOG.info("Started Mixed IO Thread:{}.", threadID);
+    String threadName = Thread.currentThread().getName();
+    long startTime = Time.monotonicNow();
+
+    while (Time.monotonicNow() < startTime + runTimeMillis) {
+      LoadBucket bucket =
+          ozoneBuckets.get((int) (Math.random() * ozoneBuckets.size()));
+      try {
+        int index = RandomUtils.nextInt();
+        ByteBuffer buffer = dataBuffer.getBuffer(index);
+        String keyName = MiniOzoneLoadGenerator.getKeyName(index, threadName);
+        bucket.writeKey(buffer, keyName);
+
+        bucket.readKey(buffer, keyName);
+
+        bucket.deleteKey(keyName);
+      } catch (Throwable t) {
+        LOG.error("LOADGEN: Exiting due to exception", t);
+        ExitUtil.terminate(new ExitUtil.ExitException(3, t));
+      }
+    }
+  }
 
 Review comment:
   Can you please extract common structure from all 3 generators?  Only 
`<perform operation>` is changing.
   
   ```
   public void startLoad(long runTimeMillis) {
     ...
     while (...) {
       try {
         <perform operation>
       } catch (...) {
         ..
       }
     }
   }
   ```

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to