|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.dubbo.rpc.protocol.tri.call; |
| 18 | + |
| 19 | +import org.apache.dubbo.common.constants.LoggerCodeConstants; |
| 20 | +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; |
| 21 | +import org.apache.dubbo.common.logger.LoggerFactory; |
| 22 | +import org.apache.dubbo.remoting.api.connection.AbstractConnectionClient; |
| 23 | +import org.apache.dubbo.rpc.protocol.tri.command.CreateStreamQueueCommand; |
| 24 | +import org.apache.dubbo.rpc.protocol.tri.stream.ReconnectionManager; |
| 25 | +import org.apache.dubbo.rpc.protocol.tri.stream.TripleStreamChannelFuture; |
| 26 | +import org.apache.dubbo.rpc.protocol.tri.transport.H2TransportListener; |
| 27 | +import org.apache.dubbo.rpc.protocol.tri.transport.TripleCommandOutBoundHandler; |
| 28 | +import org.apache.dubbo.rpc.protocol.tri.transport.TripleHttp2ClientResponseHandler; |
| 29 | + |
| 30 | +import java.util.concurrent.CompletableFuture; |
| 31 | + |
| 32 | +import io.netty.channel.Channel; |
| 33 | +import io.netty.channel.ChannelHandlerContext; |
| 34 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 35 | +import io.netty.handler.codec.http2.Http2StreamChannelBootstrap; |
| 36 | + |
| 37 | +/** |
| 38 | + * Implementation of ReconnectionManager that uses AbstractConnectionClient for reconnection. |
| 39 | + * This provides real reconnection capability by leveraging existing connection management. |
| 40 | + */ |
| 41 | +public class TripleReconnectionManager implements ReconnectionManager { |
| 42 | + |
| 43 | + private static final ErrorTypeAwareLogger LOGGER = |
| 44 | + LoggerFactory.getErrorTypeAwareLogger(TripleReconnectionManager.class); |
| 45 | + |
| 46 | + private final AbstractConnectionClient connectionClient; |
| 47 | + private final TripleClientCall clientCall; |
| 48 | + private final H2TransportListener transportListener; |
| 49 | + |
| 50 | + public TripleReconnectionManager( |
| 51 | + AbstractConnectionClient connectionClient, |
| 52 | + TripleClientCall clientCall, |
| 53 | + H2TransportListener transportListener) { |
| 54 | + this.connectionClient = connectionClient; |
| 55 | + this.clientCall = clientCall; |
| 56 | + this.transportListener = transportListener; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public CompletableFuture<Boolean> attemptReconnection() { |
| 61 | + return CompletableFuture.supplyAsync(() -> { |
| 62 | + try { |
| 63 | + LOGGER.info("Attempting reconnection using connection client: {}", connectionClient); |
| 64 | + |
| 65 | + // Force get a new channel, which will trigger reconnection if needed |
| 66 | + Object channel = connectionClient.getChannel(true); |
| 67 | + |
| 68 | + boolean success = (channel != null) && isConnectionActive(); |
| 69 | + |
| 70 | + if (success) { |
| 71 | + LOGGER.info("Reconnection successful for connection client: {}", connectionClient); |
| 72 | + } else { |
| 73 | + LOGGER.warn( |
| 74 | + LoggerCodeConstants.INTERNAL_ERROR, |
| 75 | + "", |
| 76 | + "", |
| 77 | + "Reconnection failed for connection client: " + connectionClient); |
| 78 | + } |
| 79 | + |
| 80 | + return success; |
| 81 | + } catch (Exception e) { |
| 82 | + LOGGER.error(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Error during reconnection attempt", e); |
| 83 | + return false; |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean isConnectionActive() { |
| 90 | + try { |
| 91 | + return connectionClient.isConnected(); |
| 92 | + } catch (Exception e) { |
| 93 | + LOGGER.debug("Error checking connection status", e); |
| 94 | + return false; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Object getNewStreamChannel() { |
| 100 | + try { |
| 101 | + // Get the active channel, this should be the new channel after reconnection |
| 102 | + return connectionClient.getChannel(true); |
| 103 | + } catch (Exception e) { |
| 104 | + LOGGER.error(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Error getting new stream channel", e); |
| 105 | + return null; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public TripleStreamChannelFuture createStreamFuture() { |
| 111 | + try { |
| 112 | + LOGGER.info("Creating new TripleStreamChannelFuture for reconnection"); |
| 113 | + |
| 114 | + // Force get a new channel, which will trigger reconnection if needed |
| 115 | + Object channel = connectionClient.getChannel(true); |
| 116 | + |
| 117 | + if (channel != null && isConnectionActive()) { |
| 118 | + // Create a new Http2StreamChannel using the same mechanism as initial connection |
| 119 | + Channel parentChannel = (Channel) channel; |
| 120 | + |
| 121 | + // Create Http2StreamChannelBootstrap to open a new stream channel |
| 122 | + Http2StreamChannelBootstrap bootstrap = new Http2StreamChannelBootstrap(parentChannel); |
| 123 | + |
| 124 | + // Set up the handler to configure the pipeline when the stream channel is created |
| 125 | + bootstrap.handler(new ChannelInboundHandlerAdapter() { |
| 126 | + @Override |
| 127 | + public void handlerAdded(ChannelHandlerContext ctx) { |
| 128 | + ctx.channel() |
| 129 | + .pipeline() |
| 130 | + .addLast(new TripleCommandOutBoundHandler()) |
| 131 | + .addLast(new TripleHttp2ClientResponseHandler(transportListener)); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + // Create the future that will be completed when the stream channel is ready |
| 136 | + TripleStreamChannelFuture streamChannelFuture = new TripleStreamChannelFuture(parentChannel); |
| 137 | + |
| 138 | + // Use CreateStreamQueueCommand to create the actual Http2StreamChannel |
| 139 | + CreateStreamQueueCommand createCommand = |
| 140 | + CreateStreamQueueCommand.create(bootstrap, streamChannelFuture); |
| 141 | + |
| 142 | + // IMPROVED: Execute the creation command and add better error handling |
| 143 | + if (parentChannel.eventLoop().inEventLoop()) { |
| 144 | + // If we're already in the event loop, execute directly |
| 145 | + createCommand.run(parentChannel); |
| 146 | + } else { |
| 147 | + // Execute in the channel's event loop to ensure thread safety |
| 148 | + parentChannel.eventLoop().execute(() -> createCommand.run(parentChannel)); |
| 149 | + } |
| 150 | + |
| 151 | + // Return the future - caller is now responsible for waiting for completion |
| 152 | + LOGGER.info("Started async creation of new Http2StreamChannel for reconnection, " |
| 153 | + + "future will be completed when stream is ready"); |
| 154 | + return streamChannelFuture; |
| 155 | + |
| 156 | + } else { |
| 157 | + LOGGER.warn( |
| 158 | + LoggerCodeConstants.INTERNAL_ERROR, |
| 159 | + "", |
| 160 | + "", |
| 161 | + "Failed to create TripleStreamChannelFuture - no active channel available"); |
| 162 | + return null; |
| 163 | + } |
| 164 | + } catch (Exception e) { |
| 165 | + LOGGER.error( |
| 166 | + LoggerCodeConstants.INTERNAL_ERROR, |
| 167 | + "", |
| 168 | + "", |
| 169 | + "Error creating new TripleStreamChannelFuture for reconnection", |
| 170 | + e); |
| 171 | + return null; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments