-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#9371] Periodically renew SendSpan with transport
- Loading branch information
youngjin.kim2
committed
Nov 4, 2022
1 parent
f2cbad7
commit 452eb76
Showing
7 changed files
with
339 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 249 additions & 0 deletions
249
...src/main/java/com/navercorp/pinpoint/profiler/grpc/SubconnectionExpiringLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
/* | ||
* Copyright 2022 NAVER Corp. | ||
* | ||
* Licensed 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 com.navercorp.pinpoint.profiler.grpc; | ||
|
||
import io.grpc.Attributes; | ||
import io.grpc.ConnectivityState; | ||
import io.grpc.ConnectivityStateInfo; | ||
import io.grpc.EquivalentAddressGroup; | ||
import io.grpc.LoadBalancer; | ||
import io.grpc.Status; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
|
||
import static io.grpc.ConnectivityState.*; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class SubconnectionExpiringLoadBalancer extends LoadBalancer { | ||
private static final long subchannelMaxAge = TimeUnit.MINUTES.toNanos(5); | ||
|
||
private final Helper helper; | ||
private List<EquivalentAddressGroup> currentAddresses; | ||
private Subchannel readySubchannel; | ||
private Subchannel failureSubchannel; | ||
private Subchannel connectingSubchannel; | ||
|
||
private Status failureStatus; | ||
|
||
private boolean initialized = false; | ||
|
||
SubconnectionExpiringLoadBalancer(Helper helper) { | ||
this.helper = helper; | ||
} | ||
|
||
@Override | ||
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) { | ||
final List<EquivalentAddressGroup> addresses = resolvedAddresses.getAddresses(); | ||
this.updateAddresses(addresses); | ||
this.currentAddresses = addresses; | ||
|
||
if (!initialized) { | ||
initialized = true; | ||
this.createSubchannel(); | ||
helper.updateBalancingState(CONNECTING, new Picker(PickResult.withNoResult())); | ||
} | ||
} | ||
|
||
private void updateAddresses(List<EquivalentAddressGroup> addresses) { | ||
if (this.readySubchannel != null) { | ||
this.readySubchannel.updateAddresses(addresses); | ||
} | ||
if (this.failureSubchannel != null) { | ||
this.failureSubchannel.updateAddresses(addresses); | ||
} | ||
if (this.connectingSubchannel != null) { | ||
this.connectingSubchannel.updateAddresses(addresses); | ||
} | ||
} | ||
|
||
private void createSubchannel() { | ||
Subchannel subchannel = helper.createSubchannel( | ||
CreateSubchannelArgs.newBuilder() | ||
.setAddresses(this.currentAddresses) | ||
.setAttributes( | ||
Attributes.newBuilder() | ||
.set(ATTR_PICK_PROGRESS, new AtomicReference<>(PickProgress.NOT_PICKED_YET)) | ||
.set(ATTR_CREATED_AT, System.nanoTime()) | ||
.build() | ||
) | ||
.build() | ||
); | ||
|
||
subchannel.start(stateInfo -> { | ||
final ConnectivityState state = stateInfo.getState(); | ||
if (state == SHUTDOWN) { | ||
return; | ||
} | ||
if (state == TRANSIENT_FAILURE || state == IDLE) { | ||
helper.refreshNameResolution(); | ||
} | ||
|
||
moveTo(subchannel, stateInfo); | ||
updateBalancingState(); | ||
}); | ||
|
||
subchannel.requestConnection(); | ||
} | ||
|
||
private void moveTo(Subchannel subchannel, ConnectivityStateInfo stateInfo) { | ||
if (this.readySubchannel == subchannel) { | ||
this.readySubchannel = null; | ||
} | ||
if (this.failureSubchannel == subchannel) { | ||
this.failureSubchannel = null; | ||
} | ||
if (this.connectingSubchannel == subchannel) { | ||
this.connectingSubchannel = null; | ||
} | ||
|
||
final ConnectivityState position = stateInfo.getState(); | ||
|
||
if (position == READY) { | ||
if (this.readySubchannel != null) { | ||
this.readySubchannel.shutdown(); | ||
} | ||
this.readySubchannel = subchannel; | ||
} else if (position == TRANSIENT_FAILURE) { | ||
if (this.failureSubchannel != null) { | ||
subchannel.shutdown(); | ||
} else { | ||
this.failureSubchannel = subchannel; | ||
this.failureStatus = stateInfo.getStatus(); | ||
} | ||
} else if (position == CONNECTING) { | ||
if (this.connectingSubchannel != null) { | ||
subchannel.shutdown(); | ||
} else { | ||
this.connectingSubchannel = subchannel; | ||
} | ||
} else if (position == IDLE) { | ||
subchannel.requestConnection(); | ||
} | ||
} | ||
|
||
private void updateBalancingState() { | ||
if (this.readySubchannel != null) { | ||
helper.updateBalancingState(READY, new Picker(PickResult.withSubchannel(this.readySubchannel), args -> requestSuccessor(this.readySubchannel))); | ||
return; | ||
} | ||
|
||
if (this.connectingSubchannel != null) { | ||
helper.updateBalancingState(CONNECTING, new Picker(PickResult.withNoResult())); | ||
return; | ||
} | ||
|
||
if (this.failureSubchannel != null) { | ||
helper.updateBalancingState(TRANSIENT_FAILURE, new Picker(PickResult.withError(failureStatus))); | ||
return; | ||
} | ||
|
||
helper.updateBalancingState(IDLE, new Picker(PickResult.withNoResult())); | ||
} | ||
|
||
private void requestSuccessor(Subchannel subchannel) { | ||
final long createdAt = getCreatedAt(subchannel); | ||
if (createdAt < System.nanoTime() - subchannelMaxAge) { | ||
final AtomicReference<PickProgress> progress = getPickProgress(subchannel); | ||
if (progress.compareAndSet(PickProgress.NOT_PICKED_YET, PickProgress.PICKED)) { | ||
helper.getSynchronizationContext().execute(this::createSubchannel); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void requestConnection() { | ||
if (this.readySubchannel != null) { | ||
return; | ||
} | ||
this.createSubchannel(); | ||
} | ||
|
||
@Override | ||
public void handleNameResolutionError(Status error) { | ||
clear(); | ||
updateBalancingState(); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
clear(); | ||
} | ||
|
||
private void clear() { | ||
if (readySubchannel != null) { | ||
readySubchannel.shutdown(); | ||
readySubchannel = null; | ||
} | ||
if (connectingSubchannel != null) { | ||
connectingSubchannel.shutdown(); | ||
connectingSubchannel = null; | ||
} | ||
if (failureSubchannel != null) { | ||
failureSubchannel.shutdown(); | ||
failureSubchannel = null; | ||
} | ||
} | ||
|
||
private static final class Picker extends SubchannelPicker { | ||
private final PickResult result; | ||
private final Consumer<PickSubchannelArgs> beforePick; | ||
|
||
Picker(PickResult result) { | ||
this(result, null); | ||
} | ||
|
||
Picker(PickResult result, Consumer<PickSubchannelArgs> beforePick) { | ||
this.result = result; | ||
this.beforePick = beforePick; | ||
} | ||
|
||
@Override | ||
public PickResult pickSubchannel(PickSubchannelArgs args) { | ||
if (beforePick != null) { | ||
beforePick.accept(args); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
private enum PickProgress { | ||
NOT_PICKED_YET, | ||
PICKED, | ||
} | ||
|
||
static final Attributes.Key<AtomicReference<PickProgress>> ATTR_PICK_PROGRESS = | ||
Attributes.Key.create("pick_progress"); | ||
static final Attributes.Key<Long> ATTR_CREATED_AT = | ||
Attributes.Key.create("created_at"); | ||
|
||
private AtomicReference<PickProgress> getPickProgress(Subchannel subchannel) { | ||
return Objects.requireNonNull(subchannel.getAttributes().get(ATTR_PICK_PROGRESS)); | ||
} | ||
|
||
private long getCreatedAt(Subchannel subchannel) { | ||
final Long createdAt = subchannel.getAttributes().get(ATTR_CREATED_AT); | ||
if (createdAt == null) { | ||
return 0L; | ||
} | ||
return createdAt; | ||
} | ||
} |
Oops, something went wrong.