|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.ozone.om.ratis; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.apache.hadoop.hdds.utils.db.BatchOperation; |
| 24 | +import org.apache.hadoop.ozone.om.OzoneManager; |
| 25 | +import org.apache.hadoop.ozone.om.OMMetadataManager; |
| 26 | +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; |
| 27 | +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.OperationArgs; |
| 28 | +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo.OperationType; |
| 29 | +import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; |
| 30 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class is a simple hook on a successful write operation. It's |
| 36 | + * only purpose at the moment is to write an OmCompletedRequestInfo record to the DB |
| 37 | + */ |
| 38 | +public final class OzoneManagerSuccessfulRequestHandler { |
| 39 | + |
| 40 | + private static final Logger LOG = |
| 41 | + LoggerFactory.getLogger(OzoneManagerSuccessfulRequestHandler.class); |
| 42 | + |
| 43 | + private final OzoneManager ozoneManager; |
| 44 | + private final OMMetadataManager omMetadataManager; |
| 45 | + |
| 46 | + public OzoneManagerSuccessfulRequestHandler(OzoneManager ozoneManager) { |
| 47 | + this.ozoneManager = ozoneManager; |
| 48 | + this.omMetadataManager = ozoneManager.getMetadataManager(); |
| 49 | + } |
| 50 | + |
| 51 | + public void handle(long trxLogIndex, OzoneManagerProtocolProtos.OMRequest omRequest) { |
| 52 | + |
| 53 | + switch (omRequest.getCmdType()) { |
| 54 | + case CreateKey: |
| 55 | + logRequest("CreateKey", omRequest); |
| 56 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 57 | + omRequest.getCreateKeyRequest().getKeyArgs(), |
| 58 | + new OperationArgs.CreateKeyArgs())); |
| 59 | + break; |
| 60 | + case RenameKey: |
| 61 | + logRequest("RenameKey", omRequest); |
| 62 | + OzoneManagerProtocolProtos.RenameKeyRequest renameReq |
| 63 | + = (OzoneManagerProtocolProtos.RenameKeyRequest) omRequest.getRenameKeyRequest(); |
| 64 | + |
| 65 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 66 | + omRequest.getRenameKeyRequest().getKeyArgs(), |
| 67 | + new OperationArgs.RenameKeyArgs(renameReq.getToKeyName()))); |
| 68 | + |
| 69 | + break; |
| 70 | + case DeleteKey: |
| 71 | + logRequest("DeleteKey", omRequest); |
| 72 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 73 | + omRequest.getDeleteKeyRequest().getKeyArgs(), |
| 74 | + new OperationArgs.DeleteKeyArgs())); |
| 75 | + break; |
| 76 | + case CommitKey: |
| 77 | + logRequest("CommitKey", omRequest); |
| 78 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 79 | + omRequest.getCommitKeyRequest().getKeyArgs(), |
| 80 | + new OperationArgs.CommitKeyArgs())); |
| 81 | + break; |
| 82 | + case CreateDirectory: |
| 83 | + logRequest("CreateDirectory", omRequest); |
| 84 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 85 | + omRequest.getCreateDirectoryRequest().getKeyArgs(), |
| 86 | + new OperationArgs.CreateDirectoryArgs())); |
| 87 | + break; |
| 88 | + case CreateFile: |
| 89 | + logRequest("CreateFile", omRequest); |
| 90 | + |
| 91 | + OzoneManagerProtocolProtos.CreateFileRequest createFileReq |
| 92 | + = (OzoneManagerProtocolProtos.CreateFileRequest) omRequest.getCreateFileRequest(); |
| 93 | + |
| 94 | + storeCompletedRequestInfo(buildOmCompletedRequestInfo(trxLogIndex, |
| 95 | + omRequest.getCreateFileRequest().getKeyArgs(), |
| 96 | + new OperationArgs.CreateFileArgs(createFileReq.getIsRecursive(), |
| 97 | + createFileReq.getIsOverwrite()))); |
| 98 | + break; |
| 99 | + default: |
| 100 | + LOG.error("Unhandled cmdType={}", omRequest.getCmdType()); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static void logRequest(String label, OzoneManagerProtocolProtos.OMRequest omRequest) { |
| 106 | + if (LOG.isDebugEnabled()) { |
| 107 | + LOG.debug("---> {} {}", label, omRequest); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private OmCompletedRequestInfo buildOmCompletedRequestInfo(long trxLogIndex, |
| 112 | + OzoneManagerProtocolProtos.KeyArgs keyArgs, |
| 113 | + OperationArgs opArgs) { |
| 114 | + return OmCompletedRequestInfo.newBuilder() |
| 115 | + .setTrxLogIndex(trxLogIndex) |
| 116 | + .setVolumeName(keyArgs.getVolumeName()) |
| 117 | + .setBucketName(keyArgs.getBucketName()) |
| 118 | + .setKeyName(keyArgs.getKeyName()) |
| 119 | + .setCreationTime(System.currentTimeMillis()) |
| 120 | + .setOpArgs(opArgs) |
| 121 | + .build(); |
| 122 | + } |
| 123 | + |
| 124 | + private void storeCompletedRequestInfo(OmCompletedRequestInfo requestInfo) { |
| 125 | + if (LOG.isDebugEnabled()) { |
| 126 | + LOG.debug("Storing request info {}", requestInfo); |
| 127 | + } |
| 128 | + |
| 129 | + // XXX: not sure if this string key is necessary. I added it as an |
| 130 | + // identifier which consumers of the ledger could use an efficient |
| 131 | + // (lexiographically sortable) key which could serve as a "seek |
| 132 | + // position" to continue reading where they left off (and to |
| 133 | + // persist to remember where they needed to carry on from). But |
| 134 | + // that may be unnecessary. TODO: can we just use a plain integer |
| 135 | + // key? |
| 136 | + String key = requestInfo.getDbKey(); |
| 137 | + |
| 138 | + // XXX: should this be part of an atomic db txn that happens at the end |
| 139 | + // of each replayed event or batch of events so that the completed |
| 140 | + // request info "ledger" table is consistent with the processed |
| 141 | + // raits events? e.g. OzoneManagerDoubleBuffer? |
| 142 | + |
| 143 | + try (BatchOperation batchOperation = omMetadataManager.getStore() |
| 144 | + .initBatchOperation()) { |
| 145 | + |
| 146 | + omMetadataManager.getCompletedRequestInfoTable().putWithBatch(batchOperation, key, requestInfo); |
| 147 | + |
| 148 | + // TODO: cap the size of the table to some configured limit. |
| 149 | + // |
| 150 | + // The following code is taken from |
| 151 | + // https://github.com/apache/ozone/pull/8779/files#r2510853726 |
| 152 | + // |
| 153 | + // ... as a suggested approach but I think it will need amended to |
| 154 | + // work here because the code seems to be predicated on all txnids |
| 155 | + // being held (and therefore we can count the nuber of IDs to |
| 156 | + // delete by subtracting new txnid from the first) whereas |
| 157 | + // CompletedRequestInfoTable only holds the details of a subset of |
| 158 | + // "interesting" write requests and therefore there are gaps in |
| 159 | + // the IDs. |
| 160 | + // |
| 161 | + // I'm not sure how best to approach this. A couple of( strawman) |
| 162 | + // ideas: |
| 163 | + // |
| 164 | + // 1. we could store every operation wherther interesting or not (or we |
| 165 | + // add dummy rows for the "non interesting" requests). |
| 166 | + // 2. we store an in memory count of the CompletedRequestInfoTable |
| 167 | + // which is initialized on startup and updated as rows are |
| 168 | + // added/cycled out. Therefore we know how many to cap the table |
| 169 | + // size as. |
| 170 | + // |
| 171 | + // TODO: revisit this |
| 172 | + // |
| 173 | + //omMetadataManager.getCompletedRequestInfoTable().deleteRangeWithBatch(batchOperation, 0L, |
| 174 | + // Math.max(lastTransaction.getIndex() - maxFlushedTransactionGap, 0L)); |
| 175 | + |
| 176 | + omMetadataManager.getStore().commitBatchOperation(batchOperation); |
| 177 | + |
| 178 | + //} catch (IOException ex) { |
| 179 | + // LOG.error("Unable to write operation {}", requestInfo, ex); |
| 180 | + } catch (Exception ex) { |
| 181 | + LOG.error("Unable to write operation {}", requestInfo, ex); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments