peterxcli commented on code in PR #11763: URL: https://github.com/apache/ozone/pull/11763#discussion_r4499758839
########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/protocolPB/grpc/OMGrpcError.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.protocolPB.grpc; + +import io.grpc.Metadata; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import io.grpc.protobuf.ProtoUtils; +import java.io.IOException; +import org.apache.hadoop.ozone.om.exceptions.OMException; +import org.apache.hadoop.ozone.om.exceptions.OMLeaderNotReadyException; +import org.apache.hadoop.ozone.om.exceptions.OMNotLeaderException; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMGrpcErrorProto; +import org.apache.ratis.protocol.exceptions.ReadException; +import org.apache.ratis.protocol.exceptions.ReadIndexException; + +/** + * The Java form of {@link OMGrpcErrorProto}, the error that the OM gRPC server sends in the gRPC trailers. + */ +public final class OMGrpcError { + private static final Metadata.Key<OMGrpcErrorProto> ERROR_KEY = Metadata.Key.of("om-grpc-error-bin", + ProtoUtils.metadataMarshaller(OMGrpcErrorProto.getDefaultInstance())); + + private final Kind kind; + private final String message; + /** The OM node ID of the suggested leader, for {@link Kind#NOT_LEADER}. */ + private final String suggestedLeaderNodeId; + + /** A kind for each error in {@link OMGrpcErrorProto}, and {@link #INTERNAL} for any other error. */ + public enum Kind { + NOT_LEADER, + LEADER_NOT_READY, + READ_INDEX_FAILED, + READ_FAILED, + INTERNAL + } + + private OMGrpcError(Kind kind, String message, String suggestedLeaderNodeId) { + this.kind = kind; + this.message = message; + this.suggestedLeaderNodeId = suggestedLeaderNodeId; + } + + /** @return the error for the given exception thrown by OM. */ + public static OMGrpcError fromException(Throwable omException) { + if (omException instanceof OMNotLeaderException) { + return new OMGrpcError(Kind.NOT_LEADER, omException.getMessage(), + ((OMNotLeaderException) omException).getSuggestedLeaderNodeId()); + } else if (omException instanceof OMLeaderNotReadyException) { + return new OMGrpcError(Kind.LEADER_NOT_READY, omException.getMessage(), null); + } else if (omException instanceof ReadIndexException) { + return new OMGrpcError(Kind.READ_INDEX_FAILED, omException.getMessage(), null); + } else if (omException instanceof ReadException) { + return new OMGrpcError(Kind.READ_FAILED, omException.getMessage(), null); + } + return new OMGrpcError(Kind.INTERNAL, omException.toString(), null); + } + + public static OMGrpcError getFromProtobuf(OMGrpcErrorProto proto) { + switch (proto.getErrorCase()) { + case NOTLEADER: + final OMGrpcErrorProto.NotLeader notLeader = proto.getNotLeader(); + return new OMGrpcError(Kind.NOT_LEADER, notLeader.hasMessage() ? notLeader.getMessage() : null, + notLeader.hasSuggestedLeaderNodeId() ? notLeader.getSuggestedLeaderNodeId() : null); + case LEADERNOTREADY: + final OMGrpcErrorProto.LeaderNotReady leaderNotReady = proto.getLeaderNotReady(); + return new OMGrpcError(Kind.LEADER_NOT_READY, + leaderNotReady.hasMessage() ? leaderNotReady.getMessage() : null, null); + case READINDEXFAILED: + final OMGrpcErrorProto.ReadIndexFailed readIndexFailed = proto.getReadIndexFailed(); + return new OMGrpcError(Kind.READ_INDEX_FAILED, + readIndexFailed.hasMessage() ? readIndexFailed.getMessage() : null, null); + case READFAILED: + final OMGrpcErrorProto.ReadFailed readFailed = proto.getReadFailed(); + return new OMGrpcError(Kind.READ_FAILED, readFailed.hasMessage() ? readFailed.getMessage() : null, null); + default: + return new OMGrpcError(Kind.INTERNAL, null, null); + } + } + + /** + * @return the error that the gRPC error carries, or null if the gRPC error does not come from OM. + */ + public static OMGrpcError fromStatusRuntimeException(StatusRuntimeException e) { + final Metadata trailers = e.getTrailers(); + final OMGrpcErrorProto proto = trailers == null ? null : trailers.get(ERROR_KEY); + if (proto == null) { + return null; Review Comment: Right, an old OM sends no trailer, so the client sees these as plain errors and fails over. I’d rather not add a fallback: 1. 2.2.1’s S3G already does the same. Its unwrapException returns the OM exception itself, not a ServiceException, so shouldRetry never reaches the NotLeader or LeaderNotReady branch. 2. Against an old OM, a new S3G loses only the gRPC follower read optimization, which is new and off by default. It also needs the S3G upgraded before the OMs. -- 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]
