You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by el...@apache.org on 2021/04/07 08:06:25 UTC

[ozone] branch master updated: HDDS-5011. Introduce Java based ReplicationConfig implementation (#2089)

This is an automated email from the ASF dual-hosted git repository.

elek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new ee4f759  HDDS-5011. Introduce Java based ReplicationConfig implementation (#2089)
ee4f759 is described below

commit ee4f7596da67635859ff71997155fa2ddcedee95
Author: Elek, Márton <el...@users.noreply.github.com>
AuthorDate: Wed Apr 7 10:05:37 2021 +0200

    HDDS-5011. Introduce Java based ReplicationConfig implementation (#2089)
---
 .../hadoop/hdds/client/RatisReplicationConfig.java | 82 ++++++++++++++++++
 .../hadoop/hdds/client/ReplicationConfig.java      | 98 ++++++++++++++++++++++
 .../hdds/client/StandaloneReplicationConfig.java   | 72 ++++++++++++++++
 .../hdds/scm/protocol/TestReplicationConfig.java   | 66 +++++++++++++++
 4 files changed, 318 insertions(+)

diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/RatisReplicationConfig.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/RatisReplicationConfig.java
new file mode 100644
index 0000000..8f29905
--- /dev/null
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/RatisReplicationConfig.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.hdds.client;
+
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+
+import java.util.Objects;
+
+/**
+ * Replication configuration for EC replication.
+ */
+public class RatisReplicationConfig
+    implements ReplicationConfig {
+
+  private final ReplicationFactor replicationFactor;
+
+  public RatisReplicationConfig(ReplicationFactor replicationFactor) {
+    this.replicationFactor = replicationFactor;
+  }
+
+  public static boolean hasFactor(ReplicationConfig replicationConfig,
+      ReplicationFactor factor) {
+    if (replicationConfig instanceof RatisReplicationConfig) {
+      return ((RatisReplicationConfig) replicationConfig).getReplicationFactor()
+          .equals(factor);
+    }
+    return false;
+  }
+
+  @Override
+  public ReplicationType getReplicationType() {
+    return ReplicationType.RATIS;
+  }
+
+  @Override
+  public int getRequiredNodes() {
+    return replicationFactor.getNumber();
+  }
+
+  public ReplicationFactor getReplicationFactor() {
+    return replicationFactor;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    RatisReplicationConfig that = (RatisReplicationConfig) o;
+    return replicationFactor == that.replicationFactor;
+  }
+
+  @Override
+  public String toString() {
+    return "RATIS/" + replicationFactor;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(replicationFactor);
+  }
+}
diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/ReplicationConfig.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/ReplicationConfig.java
new file mode 100644
index 0000000..7818661
--- /dev/null
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/ReplicationConfig.java
@@ -0,0 +1,98 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hdds.client;
+
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+
+/**
+ * Replication configuration for any ReplicationType with all the required
+ * parameters..
+ */
+public interface ReplicationConfig {
+
+  /**
+   * Helper method to create proper replication method from old-style
+   * factor+type definition.
+   * <p>
+   * Note: it's never used for EC replication where config is created.
+   */
+  static ReplicationConfig fromTypeAndFactor(
+      HddsProtos.ReplicationType type,
+      HddsProtos.ReplicationFactor factor
+  ) {
+    switch (type) {
+    case RATIS:
+      return new RatisReplicationConfig(factor);
+    case STAND_ALONE:
+      return new StandaloneReplicationConfig(factor);
+    default:
+      throw new UnsupportedOperationException(
+          "Not supported replication: " + type);
+    }
+  }
+
+  /**
+   * Helper method to serialize from proto.
+   * <p>
+   * This uses either the old type/factor or the new ecConfig depends on the
+   * type.
+   * <p>
+   * Note: It will support all the available replication types (including EC).
+   * <p>
+   * Separated to remain be synced with the EC feature branch, as later it
+   * will have different signature.
+   */
+  static ReplicationConfig fromProto(
+      HddsProtos.ReplicationType type,
+      HddsProtos.ReplicationFactor factor) {
+    switch (type) {
+    case RATIS:
+    case STAND_ALONE:
+      return fromTypeAndFactor(type, factor);
+    default:
+      throw new UnsupportedOperationException(
+          "Not supported replication: " + type);
+    }
+  }
+
+  static HddsProtos.ReplicationFactor getLegacyFactor(
+      ReplicationConfig replicationConfig) {
+    if (replicationConfig instanceof RatisReplicationConfig) {
+      return ((RatisReplicationConfig) replicationConfig)
+          .getReplicationFactor();
+    } else if (replicationConfig instanceof StandaloneReplicationConfig) {
+      return ((StandaloneReplicationConfig) replicationConfig)
+          .getReplicationFactor();
+    }
+    throw new UnsupportedOperationException(
+        "factor is not valid property of replication " + replicationConfig
+            .getReplicationType());
+  }
+
+  /**
+   * Replication type supported by the replication config.
+   */
+  HddsProtos.ReplicationType getReplicationType();
+
+  /**
+   * Number of required nodes for this replication.
+   */
+  int getRequiredNodes();
+
+}
diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/StandaloneReplicationConfig.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/StandaloneReplicationConfig.java
new file mode 100644
index 0000000..49f8ebd
--- /dev/null
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/StandaloneReplicationConfig.java
@@ -0,0 +1,72 @@
+/**
+ * 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.hdds.client;
+
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+
+import java.util.Objects;
+
+/**
+ * Replication configuration for STANDALONE replication.
+ */
+public class StandaloneReplicationConfig implements ReplicationConfig {
+
+  private final ReplicationFactor replicationFactor;
+
+  public StandaloneReplicationConfig(ReplicationFactor replicationFactor) {
+    this.replicationFactor = replicationFactor;
+  }
+
+  public ReplicationFactor getReplicationFactor() {
+    return replicationFactor;
+  }
+
+  @Override
+  public int getRequiredNodes() {
+    return replicationFactor.getNumber();
+  }
+
+  @Override
+  public ReplicationType getReplicationType() {
+    return ReplicationType.STAND_ALONE;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    StandaloneReplicationConfig that = (StandaloneReplicationConfig) o;
+    return replicationFactor == that.replicationFactor;
+  }
+
+  @Override
+  public String toString() {
+    return "STANDALONE/" + replicationFactor;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(replicationFactor);
+  }
+}
diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/scm/protocol/TestReplicationConfig.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/scm/protocol/TestReplicationConfig.java
new file mode 100644
index 0000000..4ab60bc
--- /dev/null
+++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/scm/protocol/TestReplicationConfig.java
@@ -0,0 +1,66 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hdds.scm.protocol;
+
+import org.apache.hadoop.hdds.client.RatisReplicationConfig;
+import org.apache.hadoop.hdds.client.ReplicationConfig;
+import org.apache.hadoop.hdds.client.StandaloneReplicationConfig;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test replicationConfig.
+ */
+public class TestReplicationConfig {
+
+  @Test
+  public void deserializeRatis() {
+    final ReplicationConfig replicationConfig = ReplicationConfig
+        .fromTypeAndFactor(ReplicationType.RATIS, ReplicationFactor.THREE);
+
+    Assert
+        .assertEquals(RatisReplicationConfig.class,
+            replicationConfig.getClass());
+
+    RatisReplicationConfig ratisReplicationConfig =
+        (RatisReplicationConfig) replicationConfig;
+    Assert.assertEquals(ReplicationType.RATIS,
+        ratisReplicationConfig.getReplicationType());
+    Assert.assertEquals(ReplicationFactor.THREE,
+        ratisReplicationConfig.getReplicationFactor());
+  }
+
+  @Test
+  public void deserializeStandalone() {
+    final ReplicationConfig replicationConfig = ReplicationConfig
+        .fromTypeAndFactor(ReplicationType.STAND_ALONE, ReplicationFactor.ONE);
+
+    Assert
+        .assertEquals(StandaloneReplicationConfig.class,
+            replicationConfig.getClass());
+
+    StandaloneReplicationConfig standalone =
+        (StandaloneReplicationConfig) replicationConfig;
+    Assert.assertEquals(ReplicationType.STAND_ALONE,
+        standalone.getReplicationType());
+    Assert.assertEquals(ReplicationFactor.ONE,
+        standalone.getReplicationFactor());
+  }
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@ozone.apache.org
For additional commands, e-mail: commits-help@ozone.apache.org