You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2022/04/13 11:30:47 UTC

[GitHub] [ozone] siddhantsangwan opened a new pull request, #3307: HDDS-6551. Introduce StatefulService in scm

siddhantsangwan opened a new pull request, #3307:
URL: https://github.com/apache/ozone/pull/3307

   ## What changes were proposed in this pull request?
   
   A `StatefulService` is an `SCMService` with an API to persist/read configuration to RocksDB and replicate it through RATIS. The configuration would be provided as a Protobuf message. This change includes an interface `StatefulServiceStateManager` and its implementation `StatefulServiceStateManagerImpl`.
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-6551
   
   ## How was this patch tested?


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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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


[GitHub] [ozone] siddhantsangwan commented on a diff in pull request #3307: HDDS-6551. Introduce StatefulService in scm

Posted by GitBox <gi...@apache.org>.
siddhantsangwan commented on code in PR #3307:
URL: https://github.com/apache/ozone/pull/3307#discussion_r862642011


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/StatefulServiceStateManagerImpl.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.ha;
+
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
+import org.apache.hadoop.hdds.scm.metadata.DBTransactionBuffer;
+import org.apache.hadoop.hdds.utils.db.Table;
+
+import java.io.IOException;
+import java.lang.reflect.Proxy;
+
+/**
+ * This class implements methods to save and read configurations of a
+ * stateful service from DB.
+ */
+public final class StatefulServiceStateManagerImpl
+    implements StatefulServiceStateManager {
+
+  // this table maps the service name to the configuration (ByteString)
+  private Table<String, ByteString> statefulServiceConfig;
+  private final DBTransactionBuffer transactionBuffer;
+
+  private StatefulServiceStateManagerImpl(
+      Table<String, ByteString> statefulServiceConfig,
+      DBTransactionBuffer scmDBTransactionBuffer) {
+    this.statefulServiceConfig = statefulServiceConfig;
+    this.transactionBuffer = scmDBTransactionBuffer;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void saveConfiguration(String serviceName, ByteString bytes)
+      throws IOException {
+    // do we need a write lock here?
+    // do we need the buffer here or should we directly put the key-value?
+    /*
+    Alternative: statefulServiceConfig.put(serviceName, bytes);
+     */
+    transactionBuffer.addToBuffer(statefulServiceConfig, serviceName, bytes);
+    if (transactionBuffer instanceof SCMHADBTransactionBuffer) {
+      SCMHADBTransactionBuffer buffer =
+              (SCMHADBTransactionBuffer) transactionBuffer;
+      buffer.flush();
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public ByteString readConfiguration(String serviceName) throws IOException {
+    // do we need a read lock here?
+    return statefulServiceConfig.get(serviceName);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void reinitialize(Table<String, ByteString> configs) {
+    this.statefulServiceConfig = configs;
+  }
+
+  public static Builder newBuilder() {
+    return new Builder();
+  }
+
+  /**
+   * Builder for StatefulServiceStateManager.
+   */
+  public static class Builder {
+    private Table<String, ByteString> statefulServiceConfig;
+    private DBTransactionBuffer transactionBuffer;
+    private SCMRatisServer scmRatisServer;
+
+    public Builder setStatefulServiceConfig(
+        final Table<String, ByteString> statefulServiceConfig) {
+      this.statefulServiceConfig = statefulServiceConfig;
+      return this;
+    }
+
+    public Builder setSCMDBTransactionBuffer(
+        final DBTransactionBuffer dbTransactionBuffer) {
+      this.transactionBuffer = dbTransactionBuffer;
+      return this;
+    }
+
+    public Builder setRatisServer(final SCMRatisServer ratisServer) {
+      scmRatisServer = ratisServer;
+      return this;
+    }
+
+    public StatefulServiceStateManager build() {

Review Comment:
   Can we exclude the not null check for `scmRatisServer`? `SCMHAInvocationHandler` takes care of the null case by calling the `invokeLocal` method.



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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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


[GitHub] [ozone] siddhantsangwan commented on pull request #3307: HDDS-6551. Introduce StatefulService in scm

Posted by GitBox <gi...@apache.org>.
siddhantsangwan commented on PR #3307:
URL: https://github.com/apache/ozone/pull/3307#issuecomment-1113246796

   @nandakumar131 thanks for the review. Updated.


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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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


[GitHub] [ozone] siddhantsangwan commented on pull request #3307: HDDS-6551. Introduce StatefulService in scm

Posted by GitBox <gi...@apache.org>.
siddhantsangwan commented on PR #3307:
URL: https://github.com/apache/ozone/pull/3307#issuecomment-1101353985

   @nandakumar131 @lokeshj1703 can you please review?


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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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


[GitHub] [ozone] nandakumar131 merged pull request #3307: HDDS-6551. Introduce StatefulService in scm

Posted by GitBox <gi...@apache.org>.
nandakumar131 merged PR #3307:
URL: https://github.com/apache/ozone/pull/3307


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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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


[GitHub] [ozone] nandakumar131 commented on a diff in pull request #3307: HDDS-6551. Introduce StatefulService in scm

Posted by GitBox <gi...@apache.org>.
nandakumar131 commented on code in PR #3307:
URL: https://github.com/apache/ozone/pull/3307#discussion_r860544993


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/StatefulService.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.ha;
+
+import com.google.protobuf.Message;
+import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
+
+import java.io.IOException;
+
+/**
+ * A StatefulService is an SCMService that persists configuration to RocksDB.
+ * The service must define this configuration as a Protobuf message.
+ */
+public abstract class StatefulService implements SCMService {
+  private final StatefulServiceStateManager stateManager;
+
+  /**
+   * Initialize a StatefulService from an extending class.
+   * @param scm {@link StorageContainerManager}
+   */
+  protected StatefulService(StorageContainerManager scm) {
+    stateManager = scm.getStatefulServiceStateManager();
+  }
+
+  /**
+   * Persists the specified {@link Message} configurationMessage to RocksDB
+   * with this service's {@link SCMService#getServiceName()} as the key.
+   * @param configurationMessage configuration Message to persist
+   * @throws IOException on failure to persist configuration
+   */
+  protected final void saveConfiguration(Message configurationMessage)
+      throws IOException {
+    stateManager.saveConfiguration(getServiceName(),
+        configurationMessage.toByteString());
+  }
+
+  /**
+   * Reads persisted configuration mapped to this service's
+   * {@link SCMService#getServiceName()} name.
+   *
+   * @param defaultInstanceForType the
+   * {@link Message#getDefaultInstanceForType()} for this message's actual type
+   * @return the persisted {@link Message} that can be cast to the required type
+   * @throws IOException on failure
+   */
+  protected final Message readConfiguration(Message defaultInstanceForType)
+      throws IOException {
+    return defaultInstanceForType.getParserForType()
+        .parseFrom(stateManager.readConfiguration(getServiceName()));
+  }
+}

Review Comment:
   With this change the return type will be the exact Proto object which can be used by the service. There is no need for any type-cast by the service implementation.



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/StatefulService.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.ha;
+
+import com.google.protobuf.Message;
+import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
+
+import java.io.IOException;
+
+/**
+ * A StatefulService is an SCMService that persists configuration to RocksDB.
+ * The service must define this configuration as a Protobuf message.
+ */
+public abstract class StatefulService implements SCMService {
+  private final StatefulServiceStateManager stateManager;
+
+  /**
+   * Initialize a StatefulService from an extending class.
+   * @param scm {@link StorageContainerManager}
+   */
+  protected StatefulService(StorageContainerManager scm) {
+    stateManager = scm.getStatefulServiceStateManager();
+  }
+
+  /**
+   * Persists the specified {@link Message} configurationMessage to RocksDB
+   * with this service's {@link SCMService#getServiceName()} as the key.
+   * @param configurationMessage configuration Message to persist
+   * @throws IOException on failure to persist configuration
+   */
+  protected final void saveConfiguration(Message configurationMessage)
+      throws IOException {
+    stateManager.saveConfiguration(getServiceName(),
+        configurationMessage.toByteString());
+  }
+
+  /**
+   * Reads persisted configuration mapped to this service's
+   * {@link SCMService#getServiceName()} name.
+   *
+   * @param defaultInstanceForType the
+   * {@link Message#getDefaultInstanceForType()} for this message's actual type
+   * @return the persisted {@link Message} that can be cast to the required type
+   * @throws IOException on failure
+   */
+  protected final Message readConfiguration(Message defaultInstanceForType)
+      throws IOException {
+    return defaultInstanceForType.getParserForType()
+        .parseFrom(stateManager.readConfiguration(getServiceName()));
+  }
+}

Review Comment:
   ```suggestion
   protected final <T extends GeneratedMessage> T readConfiguration(Class<T> configType)
         throws IOException {
         try {
           return configType.cast(ReflectionUtil.getMethod(configType,
                         "parseFrom", ByteString.class)
                 .invoke(null, stateManager.readConfiguration(getServiceName())));
         } catch (NoSuchMethodException | IllegalAccessException
                  | InvocationTargetException ex) {
             ex.printStackTrace();
             throw new InvalidProtocolBufferException(
                     "GeneratedMessage cannot be decoded: " + ex.getMessage());
         }
     }
   ```



##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/StatefulServiceStateManagerImpl.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.ha;
+
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
+import org.apache.hadoop.hdds.scm.metadata.DBTransactionBuffer;
+import org.apache.hadoop.hdds.utils.db.Table;
+
+import java.io.IOException;
+import java.lang.reflect.Proxy;
+
+/**
+ * This class implements methods to save and read configurations of a
+ * stateful service from DB.
+ */
+public final class StatefulServiceStateManagerImpl
+    implements StatefulServiceStateManager {
+
+  // this table maps the service name to the configuration (ByteString)
+  private Table<String, ByteString> statefulServiceConfig;
+  private final DBTransactionBuffer transactionBuffer;
+
+  private StatefulServiceStateManagerImpl(
+      Table<String, ByteString> statefulServiceConfig,
+      DBTransactionBuffer scmDBTransactionBuffer) {
+    this.statefulServiceConfig = statefulServiceConfig;
+    this.transactionBuffer = scmDBTransactionBuffer;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void saveConfiguration(String serviceName, ByteString bytes)
+      throws IOException {
+    // do we need a write lock here?
+    // do we need the buffer here or should we directly put the key-value?
+    /*
+    Alternative: statefulServiceConfig.put(serviceName, bytes);
+     */
+    transactionBuffer.addToBuffer(statefulServiceConfig, serviceName, bytes);
+    if (transactionBuffer instanceof SCMHADBTransactionBuffer) {
+      SCMHADBTransactionBuffer buffer =
+              (SCMHADBTransactionBuffer) transactionBuffer;
+      buffer.flush();
+    }
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public ByteString readConfiguration(String serviceName) throws IOException {
+    // do we need a read lock here?
+    return statefulServiceConfig.get(serviceName);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public void reinitialize(Table<String, ByteString> configs) {
+    this.statefulServiceConfig = configs;
+  }
+
+  public static Builder newBuilder() {
+    return new Builder();
+  }
+
+  /**
+   * Builder for StatefulServiceStateManager.
+   */
+  public static class Builder {
+    private Table<String, ByteString> statefulServiceConfig;
+    private DBTransactionBuffer transactionBuffer;
+    private SCMRatisServer scmRatisServer;
+
+    public Builder setStatefulServiceConfig(
+        final Table<String, ByteString> statefulServiceConfig) {
+      this.statefulServiceConfig = statefulServiceConfig;
+      return this;
+    }
+
+    public Builder setSCMDBTransactionBuffer(
+        final DBTransactionBuffer dbTransactionBuffer) {
+      this.transactionBuffer = dbTransactionBuffer;
+      return this;
+    }
+
+    public Builder setRatisServer(final SCMRatisServer ratisServer) {
+      scmRatisServer = ratisServer;
+      return this;
+    }
+
+    public StatefulServiceStateManager build() {

Review Comment:
   Add
   ```
   Preconditions.checkNotNull(statefulServiceConfig);
   Preconditions.checkNotNull(transactionBuffer);
   Preconditions.checkNotNull(scmRatisServer);
   ```



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

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

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


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