swamirishi commented on code in PR #9364: URL: https://github.com/apache/ozone/pull/9364#discussion_r2641760024
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ratis/OzoneManagerSuccessfulRequestHandler.java: ########## @@ -0,0 +1,184 @@ +/** + * 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.ozone.om.ratis; + +import java.io.IOException; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.hdds.utils.db.BatchOperation; +import org.apache.hadoop.ozone.om.OzoneManager; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.OperationArgs; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.OperationType; +import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is a simple hook on a successful write operation. It's + * only purpose at the moment is to write an OmCompletedRequestInfo record to the DB + */ +public final class OzoneManagerSuccessfulRequestHandler { + + private static final Logger LOG = + LoggerFactory.getLogger(OzoneManagerSuccessfulRequestHandler.class); + + private final OzoneManager ozoneManager; + private final OMMetadataManager omMetadataManager; + + public OzoneManagerSuccessfulRequestHandler(OzoneManager ozoneManager) { + this.ozoneManager = ozoneManager; + this.omMetadataManager = ozoneManager.getMetadataManager(); + } + + public void handle(long trxLogIndex, OzoneManagerProtocolProtos.OMRequest omRequest) { + + switch (omRequest.getCmdType()) { + case CreateKey: + logRequest("CreateKey", omRequest); + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getCreateKeyRequest().getKeyArgs(), + new OperationArgs.CreateKeyArgs())); + break; + case RenameKey: + logRequest("RenameKey", omRequest); + OzoneManagerProtocolProtos.RenameKeyRequest renameReq + = (OzoneManagerProtocolProtos.RenameKeyRequest) omRequest.getRenameKeyRequest(); + + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getRenameKeyRequest().getKeyArgs(), + new OperationArgs.RenameKeyArgs(renameReq.getToKeyName()))); + + break; + case DeleteKey: + logRequest("DeleteKey", omRequest); + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getDeleteKeyRequest().getKeyArgs(), + new OperationArgs.DeleteKeyArgs())); + break; + case CommitKey: + logRequest("CommitKey", omRequest); + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getCommitKeyRequest().getKeyArgs(), + new OperationArgs.CommitKeyArgs())); + break; + case CreateDirectory: + logRequest("CreateDirectory", omRequest); + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getCreateDirectoryRequest().getKeyArgs(), + new OperationArgs.CreateDirectoryArgs())); + break; + case CreateFile: + logRequest("CreateFile", omRequest); + + OzoneManagerProtocolProtos.CreateFileRequest createFileReq + = (OzoneManagerProtocolProtos.CreateFileRequest) omRequest.getCreateFileRequest(); + + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, + omRequest.getCreateFileRequest().getKeyArgs(), + new OperationArgs.CreateFileArgs(createFileReq.getIsRecursive(), + createFileReq.getIsOverwrite()))); + break; + default: + LOG.error("Unhandled cmdType={}", omRequest.getCmdType()); + break; + } + } + + private static void logRequest(String label, OzoneManagerProtocolProtos.OMRequest omRequest) { + if (LOG.isDebugEnabled()) { + LOG.debug("---> {} {}", label, omRequest); + } + } + + private OmCompletedRequestInfo buildOmCompletedRequestInfo(long trxLogIndex, + OzoneManagerProtocolProtos.KeyArgs keyArgs, + OperationArgs opArgs) { + return OmCompletedRequestInfo.newBuilder() + .setTrxLogIndex(trxLogIndex) + .setVolumeName(keyArgs.getVolumeName()) + .setBucketName(keyArgs.getBucketName()) + .setKeyName(keyArgs.getKeyName()) + .setCreationTime(System.currentTimeMillis()) + .setOpArgs(opArgs) + .build(); + } + + private void storeCompletedRequestInfo(OmCompletedRequestInfo requestInfo) { + if (LOG.isDebugEnabled()) { + LOG.debug("Storing request info {}", requestInfo); + } + + // XXX: not sure if this string key is necessary. I added it as an + // identifier which consumers of the ledger could use an efficient + // (lexiographically sortable) key which could serve as a "seek + // position" to continue reading where they left off (and to + // persist to remember where they needed to carry on from). But + // that may be unnecessary. TODO: can we just use a plain integer + // key? + String key = requestInfo.getDbKey(); + + // XXX: should this be part of an atomic db txn that happens at the end + // of each replayed event or batch of events so that the completed + // request info "ledger" table is consistent with the processed + // raits events? e.g. OzoneManagerDoubleBuffer? + + try (BatchOperation batchOperation = omMetadataManager.getStore() Review Comment: We don't need a rocksdb batch. Flush should happen as part of double buffer flush Look at this change https://github.com/apache/ozone/pull/8779/files#r2641773095 -- 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]
