ChenSammi commented on code in PR #9363:
URL: https://github.com/apache/ozone/pull/9363#discussion_r2735202663


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmCompletedRequestInfo.java:
##########
@@ -0,0 +1,533 @@
+/*
+ * 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.om.helpers;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import org.apache.hadoop.hdds.utils.db.Codec;
+import org.apache.hadoop.hdds.utils.db.CopyObject;
+import org.apache.hadoop.hdds.utils.db.DelegatedCodec;
+import org.apache.hadoop.hdds.utils.db.Proto2Codec;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class is used for storing info related to completed operations.
+ * These are the subset of operations which mutate the filesystem such
+ * as create volume/bucket/key/file/dir and associated delete/rename
+ * events where appropriate.
+ *
+ * The initial use case for this data is to create an event notification
+ * feed which users can subscribe to and get a history of such changes
+ * but the data is designed to be provided in a generic means that could
+ * be suitable for other use cases.
+ *
+ * Each successfully completion operation has an associated
+ * OmCompletedRequestInfo entry the trxLogIndex, cmdType, volumeName, 
bucketName,
+ * keyName, creationTime and operationArgs
+ */
+public final class OmCompletedRequestInfo implements 
CopyObject<OmCompletedRequestInfo> {
+  public static final Logger LOG =
+      LoggerFactory.getLogger(OmCompletedRequestInfo.class);
+
+  private static final Codec<OmCompletedRequestInfo> CODEC = new 
DelegatedCodec<>(
+      
Proto2Codec.get(OzoneManagerProtocolProtos.CompletedRequestInfo.getDefaultInstance()),
+      OmCompletedRequestInfo::getFromProtobuf,
+      OmCompletedRequestInfo::getProtobuf,
+      OmCompletedRequestInfo.class);
+
+  private long trxLogIndex;
+  private final Type cmdType;
+  private final String volumeName;
+  private final String bucketName;
+  private final String keyName;
+  private final long creationTime;
+  private final OperationArgs opArgs;
+
+  /**
+   * Private constructor, constructed via builder.
+   * @param trxLogIndex - trxLogIndex.
+   * @param cmdType - comand type.
+   * @param volumeName - volume name.
+   * @param bucketName - bucket name.
+   * @param keyName - key name.
+   * @param creationTime - creation time.
+   * @param opArgs - operation specifc arguments.
+   */
+  @SuppressWarnings("checkstyle:ParameterNumber")
+  private OmCompletedRequestInfo(Builder builder) {
+    this.trxLogIndex = builder.trxLogIndex;
+    this.cmdType = builder.cmdType;
+    this.volumeName = builder.volumeName;
+    this.bucketName = builder.bucketName;
+    this.keyName = builder.keyName;
+    this.creationTime = builder.creationTime;
+    this.opArgs = builder.opArgs;
+  }
+
+  public static Codec<OmCompletedRequestInfo> getCodec() {
+    return CODEC;
+  }
+
+  public void setTrxLogIndex(long trxLogIndex) {
+    this.trxLogIndex = trxLogIndex;
+  }
+
+  public long getTrxLogIndex() {
+    return trxLogIndex;
+  }
+
+  public Type getCmdType() {
+    return cmdType;
+  }
+
+  public String getVolumeName() {
+    return volumeName;
+  }
+
+  public String getBucketName() {
+    return bucketName;
+  }
+
+  public String getKeyName() {
+    return keyName;
+  }
+
+  public long getCreationTime() {
+    return creationTime;
+  }
+
+  public OperationArgs getOpArgs() {
+    return opArgs;
+  }
+
+  public static 
org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.Builder
+      newBuilder() {
+    return new 
org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.Builder();
+  }
+
+  public OmCompletedRequestInfo.Builder toBuilder() {
+    return new Builder()
+        .setTrxLogIndex(trxLogIndex)
+        .setCmdType(cmdType)
+        .setVolumeName(volumeName)
+        .setBucketName(bucketName)
+        .setKeyName(keyName)
+        .setCreationTime(creationTime)
+        .setOpArgs(opArgs);
+  }
+
+  /**
+   * Builder of OmCompletedRequestInfo.
+   */
+  public static class Builder {
+    private long trxLogIndex;
+    private Type cmdType;
+    private String volumeName;
+    private String bucketName;
+    private String keyName;
+    private long creationTime;
+    private OperationArgs opArgs;
+
+    public Builder() {
+      // default values
+    }
+
+    public Builder setTrxLogIndex(long trxLogIndex) {
+      this.trxLogIndex = trxLogIndex;
+      return this;
+    }
+
+    public Builder setCmdType(Type cmdType) {
+      this.cmdType = cmdType;
+      return this;
+    }
+
+    public Builder setVolumeName(String volumeName) {
+      this.volumeName = volumeName;
+      return this;
+    }
+
+    public Builder setBucketName(String bucketName) {
+      this.bucketName = bucketName;
+      return this;
+    }
+
+    public Builder setKeyName(String keyName) {
+      this.keyName = keyName;
+      return this;
+    }
+
+    public Builder setCreationTime(long crTime) {
+      this.creationTime = crTime;
+      return this;
+    }
+
+    public Builder setOpArgs(OperationArgs opArgs) {
+      this.opArgs = opArgs;
+      return this;
+    }
+
+    public OmCompletedRequestInfo build() {
+      Preconditions.checkNotNull(trxLogIndex);
+      Preconditions.checkNotNull(cmdType);
+      Preconditions.checkNotNull(volumeName);
+      Preconditions.checkNotNull(creationTime);
+      Preconditions.checkNotNull(opArgs);
+
+      return new OmCompletedRequestInfo(this);
+    }
+  }
+
+  /**
+   * Creates OmCompletedRequestInfo protobuf from OmCompletedRequestInfo.
+   */
+  public OzoneManagerProtocolProtos.CompletedRequestInfo getProtobuf() {
+    OzoneManagerProtocolProtos.CompletedRequestInfo.Builder sib =
+        OzoneManagerProtocolProtos.CompletedRequestInfo.newBuilder()
+            .setTrxLogIndex(trxLogIndex)
+            .setCmdType(cmdType)
+            .setVolumeName(volumeName)
+            .setCreationTime(creationTime);
+
+    // can be null e.g. CreateVolume
+    if (bucketName != null) {
+      sib.setBucketName(bucketName);
+    }
+
+    // can be null e.g. CreateBucket
+    if (keyName != null) {
+      sib.setKeyName(keyName);
+    }
+
+    switch (cmdType) {
+    case CreateVolume:
+      
sib.setCreateVolumeArgs(OzoneManagerProtocolProtos.CreateVolumeOperationArgs.newBuilder()
+          .build());
+      break;
+    case DeleteVolume:
+      
sib.setDeleteVolumeArgs(OzoneManagerProtocolProtos.DeleteVolumeOperationArgs.newBuilder()
+          .build());
+      break;
+    case CreateBucket:
+      
sib.setCreateBucketArgs(OzoneManagerProtocolProtos.CreateBucketOperationArgs.newBuilder()
+          .build());
+      break;
+    case DeleteBucket:
+      
sib.setDeleteBucketArgs(OzoneManagerProtocolProtos.DeleteBucketOperationArgs.newBuilder()
+          .build());
+      break;
+    case CreateKey:
+      
sib.setCreateKeyArgs(OzoneManagerProtocolProtos.CreateKeyOperationArgs.newBuilder()
+          .build());
+      break;
+    case RenameKey:
+      
sib.setRenameKeyArgs(OzoneManagerProtocolProtos.RenameKeyOperationArgs.newBuilder()
+          .setToKeyName(((OperationArgs.RenameKeyArgs) opArgs).getToKeyName())
+          .build());
+      break;
+    case DeleteKey:
+      
sib.setDeleteKeyArgs(OzoneManagerProtocolProtos.DeleteKeyOperationArgs.newBuilder()
+          .build());
+      break;
+    case CommitKey:
+      
sib.setCommitKeyArgs(OzoneManagerProtocolProtos.CommitKeyOperationArgs.newBuilder()
+          .build());
+      break;
+    case CreateDirectory:
+      
sib.setCreateDirectoryArgs(OzoneManagerProtocolProtos.CreateDirectoryOperationArgs.newBuilder()
+          .build());
+      break;
+    case CreateFile:
+      
sib.setCreateFileArgs(OzoneManagerProtocolProtos.CreateFileOperationArgs.newBuilder()
+          .setIsRecursive(((OperationArgs.CreateFileArgs) 
opArgs).isRecursive())
+          .setIsOverwrite(((OperationArgs.CreateFileArgs) 
opArgs).isOverwrite())
+          .build());
+      break;
+    default:
+      LOG.error("Unexpected cmdType={}", cmdType);
+      break;
+    }
+
+    return sib.build();
+  }
+
+  /**
+   * Parses OmCompletedRequestInfo protobuf and creates OmCompletedRequestInfo.
+   * @param completedRequestInfoProto protobuf
+   * @return instance of OmCompletedRequestInfo
+   */
+  public static OmCompletedRequestInfo getFromProtobuf(
+      OzoneManagerProtocolProtos.CompletedRequestInfo 
completedRequestInfoProto) {
+
+    OmCompletedRequestInfo.Builder osib = OmCompletedRequestInfo.newBuilder()
+        .setTrxLogIndex(completedRequestInfoProto.getTrxLogIndex())
+        .setCmdType(completedRequestInfoProto.getCmdType())
+        .setVolumeName(completedRequestInfoProto.getVolumeName())
+        .setBucketName(completedRequestInfoProto.getBucketName())
+        .setKeyName(completedRequestInfoProto.getKeyName())
+        .setCreationTime(completedRequestInfoProto.getCreationTime());
+
+    switch (completedRequestInfoProto.getCmdType()) {
+    case CreateVolume:
+      osib.setOpArgs(new OperationArgs.CreateVolumeArgs());
+      break;
+    case DeleteVolume:
+      osib.setOpArgs(new OperationArgs.DeleteVolumeArgs());
+      break;
+    case CreateBucket:
+      osib.setOpArgs(new OperationArgs.CreateBucketArgs());
+      break;
+    case DeleteBucket:
+      osib.setOpArgs(new OperationArgs.DeleteBucketArgs());
+      break;
+    case CreateKey:
+      osib.setOpArgs(new OperationArgs.CreateKeyArgs());
+      break;
+    case RenameKey:
+      OzoneManagerProtocolProtos.RenameKeyOperationArgs renameArgs
+          = (OzoneManagerProtocolProtos.RenameKeyOperationArgs) 
completedRequestInfoProto.getRenameKeyArgs();
+
+      osib.setOpArgs(new 
OperationArgs.RenameKeyArgs(renameArgs.getToKeyName()));
+      break;
+    case DeleteKey:
+      osib.setOpArgs(new OperationArgs.DeleteKeyArgs());
+      break;
+    case CommitKey:
+      osib.setOpArgs(new OperationArgs.CommitKeyArgs());
+      break;
+    case CreateDirectory:
+      osib.setOpArgs(new OperationArgs.CreateDirectoryArgs());
+      break;
+    case CreateFile:
+      OzoneManagerProtocolProtos.CreateFileOperationArgs createFileArgs
+          = (OzoneManagerProtocolProtos.CreateFileOperationArgs) 
completedRequestInfoProto.getCreateFileArgs();
+
+      osib.setOpArgs(new 
OperationArgs.CreateFileArgs(createFileArgs.getIsOverwrite(),
+                                                      
createFileArgs.getIsRecursive()));
+      break;
+    default:
+      LOG.error("Unexpected cmdType={}", 
completedRequestInfoProto.getCmdType());
+      break;
+    }
+    return osib.build();
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    OmCompletedRequestInfo that = (OmCompletedRequestInfo) o;
+    return trxLogIndex == that.trxLogIndex &&
+        cmdType == that.cmdType &&
+        creationTime == that.creationTime &&
+        volumeName.equals(that.volumeName) &&
+        bucketName.equals(that.bucketName) &&

Review Comment:
   Could you add a new unit test for null bucketName and keyName case? 



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

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to