|
| 1 | +/* |
| 2 | + * |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package org.apache.bookkeeper.proto; |
| 23 | + |
| 24 | +import io.netty.util.Recycler; |
| 25 | +import org.apache.bookkeeper.client.BKException; |
| 26 | +import org.apache.bookkeeper.common.util.MathUtils; |
| 27 | +import org.apache.bookkeeper.net.BookieId; |
| 28 | +import org.slf4j.MDC; |
| 29 | + |
| 30 | +class AddCompletion extends CompletionValue implements BookkeeperInternalCallbacks.WriteCallback { |
| 31 | + |
| 32 | + static AddCompletion acquireAddCompletion(final CompletionKey key, |
| 33 | + final BookkeeperInternalCallbacks.WriteCallback originalCallback, |
| 34 | + final Object originalCtx, |
| 35 | + final long ledgerId, final long entryId, |
| 36 | + PerChannelBookieClient perChannelBookieClient) { |
| 37 | + AddCompletion completion = ADD_COMPLETION_RECYCLER.get(); |
| 38 | + completion.reset(key, originalCallback, originalCtx, ledgerId, entryId, perChannelBookieClient); |
| 39 | + return completion; |
| 40 | + } |
| 41 | + |
| 42 | + final Recycler.Handle<AddCompletion> handle; |
| 43 | + |
| 44 | + CompletionKey key = null; |
| 45 | + BookkeeperInternalCallbacks.WriteCallback originalCallback = null; |
| 46 | + |
| 47 | + AddCompletion(Recycler.Handle<AddCompletion> handle) { |
| 48 | + super("Add", null, -1, -1, null); |
| 49 | + this.handle = handle; |
| 50 | + } |
| 51 | + |
| 52 | + void reset(final CompletionKey key, |
| 53 | + final BookkeeperInternalCallbacks.WriteCallback originalCallback, |
| 54 | + final Object originalCtx, |
| 55 | + final long ledgerId, final long entryId, |
| 56 | + PerChannelBookieClient perChannelBookieClient) { |
| 57 | + this.key = key; |
| 58 | + this.originalCallback = originalCallback; |
| 59 | + this.ctx = originalCtx; |
| 60 | + this.ledgerId = ledgerId; |
| 61 | + this.entryId = entryId; |
| 62 | + this.startTime = org.apache.bookkeeper.common.util.MathUtils.nowInNano(); |
| 63 | + |
| 64 | + this.opLogger = perChannelBookieClient.addEntryOpLogger; |
| 65 | + this.timeoutOpLogger = perChannelBookieClient.addTimeoutOpLogger; |
| 66 | + this.perChannelBookieClient = perChannelBookieClient; |
| 67 | + this.mdcContextMap = perChannelBookieClient.preserveMdcForTaskExecution ? MDC.getCopyOfContextMap() : null; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void release() { |
| 72 | + this.ctx = null; |
| 73 | + this.opLogger = null; |
| 74 | + this.timeoutOpLogger = null; |
| 75 | + this.perChannelBookieClient = null; |
| 76 | + this.mdcContextMap = null; |
| 77 | + handle.recycle(this); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void writeComplete(int rc, long ledgerId, long entryId, |
| 82 | + BookieId addr, |
| 83 | + Object ctx) { |
| 84 | + logOpResult(rc); |
| 85 | + originalCallback.writeComplete(rc, ledgerId, entryId, addr, ctx); |
| 86 | + key.release(); |
| 87 | + this.release(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + boolean maybeTimeout() { |
| 92 | + if (MathUtils.elapsedNanos(startTime) >= perChannelBookieClient.addEntryTimeoutNanos) { |
| 93 | + timeout(); |
| 94 | + return true; |
| 95 | + } else { |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void errorOut() { |
| 102 | + errorOut(BKException.Code.BookieHandleNotAvailableException); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void errorOut(final int rc) { |
| 107 | + errorOutAndRunCallback( |
| 108 | + () -> writeComplete(rc, ledgerId, entryId, perChannelBookieClient.bookieId, ctx)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void setOutstanding() { |
| 113 | + perChannelBookieClient.addEntryOutstanding.inc(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void handleV2Response( |
| 118 | + long ledgerId, long entryId, BookkeeperProtocol.StatusCode status, |
| 119 | + BookieProtocol.Response response) { |
| 120 | + perChannelBookieClient.addEntryOutstanding.dec(); |
| 121 | + handleResponse(ledgerId, entryId, status); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void handleV3Response( |
| 126 | + BookkeeperProtocol.Response response) { |
| 127 | + perChannelBookieClient.addEntryOutstanding.dec(); |
| 128 | + BookkeeperProtocol.AddResponse addResponse = response.getAddResponse(); |
| 129 | + BookkeeperProtocol.StatusCode status = response.getStatus() == BookkeeperProtocol.StatusCode.EOK |
| 130 | + ? addResponse.getStatus() : response.getStatus(); |
| 131 | + handleResponse(addResponse.getLedgerId(), addResponse.getEntryId(), |
| 132 | + status); |
| 133 | + } |
| 134 | + |
| 135 | + private void handleResponse(long ledgerId, long entryId, |
| 136 | + BookkeeperProtocol.StatusCode status) { |
| 137 | + if (LOG.isDebugEnabled()) { |
| 138 | + logResponse(status, "ledger", ledgerId, "entry", entryId); |
| 139 | + } |
| 140 | + |
| 141 | + int rc = convertStatus(status, BKException.Code.WriteException); |
| 142 | + writeComplete(rc, ledgerId, entryId, perChannelBookieClient.bookieId, ctx); |
| 143 | + } |
| 144 | + |
| 145 | + private static final Recycler<AddCompletion> ADD_COMPLETION_RECYCLER = new Recycler<AddCompletion>() { |
| 146 | + @Override |
| 147 | + protected AddCompletion newObject(Recycler.Handle<AddCompletion> handle) { |
| 148 | + return new AddCompletion(handle); |
| 149 | + } |
| 150 | + }; |
| 151 | +} |
0 commit comments