You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ratis.apache.org by GitBox <gi...@apache.org> on 2020/11/25 00:35:42 UTC

[GitHub] [incubator-ratis] runzhiwang commented on a change in pull request #298: [WIP] RATIS-1111. Change the FileStore example to use Streaming

runzhiwang commented on a change in pull request #298:
URL: https://github.com/apache/incubator-ratis/pull/298#discussion_r530033067



##########
File path: ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreStreamingBaseTest.java
##########
@@ -0,0 +1,133 @@
+/**
+ * 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.ratis.examples.filestore;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.MiniRaftCluster;
+import org.apache.ratis.RaftTestUtil;
+import org.apache.ratis.client.api.DataStreamOutput;
+import org.apache.ratis.conf.ConfUtils;
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto;
+import org.apache.ratis.protocol.DataStreamReply;
+import org.apache.ratis.protocol.RaftGroup;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.server.impl.RaftServerImpl;
+import org.apache.ratis.statemachine.StateMachine;
+import org.apache.ratis.util.SizeInBytes;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class FileStoreStreamingBaseTest <CLUSTER extends MiniRaftCluster>
+    extends BaseTest
+    implements MiniRaftCluster.Factory.Get<CLUSTER> {
+  public static final Logger LOG = LoggerFactory.getLogger(FileStoreStreamingBaseTest.class);
+
+  {
+    final RaftProperties p = getProperties();
+    p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
+        FileStoreStateMachine.class, StateMachine.class);
+    ConfUtils.setFile(p::setFile, FileStoreCommon.STATEMACHINE_DIR_KEY,
+        new File(getClassTestDir(), "filestore"));
+  }
+
+  static final int NUM_PEERS = 3;
+  static final int MODULUS = 23;
+
+  @Test
+  public void testFileStoreStream() throws Exception {
+    final CLUSTER cluster = newCluster(NUM_PEERS);
+    cluster.start();
+    RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
+
+    final RaftGroup raftGroup = cluster.getGroup();
+    final Collection<RaftPeer> peers = raftGroup.getPeers();
+    Assert.assertEquals(NUM_PEERS, peers.size());
+    RaftPeer raftPeer = peers.iterator().next();
+
+    final CheckedSupplier<FileStoreClient, IOException> newClient =
+        () -> new FileStoreClient(cluster.getGroup(), getProperties(), raftPeer);
+    // TODO: configurable buffer size
+    final int bufferSize = 10_000;
+    testSingleFile("foo", SizeInBytes.valueOf("2M"), bufferSize, newClient, leader);
+
+    cluster.shutdown();
+  }
+
+  private void testSingleFile(
+      String path, SizeInBytes fileLength, int bufferSize, CheckedSupplier<FileStoreClient,
+      IOException> newClient, RaftServerImpl leader)
+      throws Exception {
+    LOG.info("runTestSingleFile with path={}, fileLength={}", path, fileLength);
+    final int size = fileLength.getSizeInt();
+    FileStoreClient client = newClient.get();
+    DataStreamOutput dataStreamOutput = client.getStreamOutput(path);
+    final List<CompletableFuture<DataStreamReply>> futures = new ArrayList<>();
+    final List<Integer> sizes = new ArrayList<>();
+
+    for(int offset = 0; offset < size; ) {
+      final int remaining = size - offset;
+      final int length = Math.min(remaining, bufferSize);
+      final boolean close = length == remaining;
+
+      LOG.trace("write {}, offset={}, length={}, close? {}",
+          path, offset, length, close);
+      final ByteBuffer bf = initBuffer(0, length);
+      futures.add(dataStreamOutput.writeAsync(bf, close));
+      sizes.add(length);
+      offset += length;
+    }
+
+    // TODO: handle when any of the writeAsync has failed.
+    // check writeAsync requests
+    for (int i = 0; i < futures.size(); i++) {
+      final DataStreamReply reply = futures.get(i).join();
+      Assert.assertTrue(reply.isSuccess());
+      Assert.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
+      Assert.assertEquals(reply.getType(), i == futures.size() - 1 ? DataStreamPacketHeaderProto.Type.STREAM_DATA_SYNC : DataStreamPacketHeaderProto.Type.STREAM_DATA);
+    }
+
+    dataStreamOutput.closeAsync().join();
+  }
+
+  static ByteBuffer initBuffer(int offset, int size) {

Review comment:
       I think this test is mainly for testing performance, if so, we should create file with large size first, for example 1GB, then use MapByteBuffer to read file and writeAsync(MapByteBuffer). allocateDirect and fill the DirectByteBuffer is not the actual scene we use in ozone.  @szetszwo  what do you think ?




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