Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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 io.streamnative.pulsar.handlers.kop.utils.delayed;
import static com.google.common.base.Preconditions.checkArgument;
import static io.streamnative.pulsar.handlers.kop.utils.CoreUtils.inReadLock;
import static io.streamnative.pulsar.handlers.kop.utils.CoreUtils.inWriteLock;
import io.streamnative.pulsar.handlers.kop.utils.ShutdownableThread;
import io.streamnative.pulsar.handlers.kop.utils.timer.SystemTimer;
import io.streamnative.pulsar.handlers.kop.utils.timer.Timer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import lombok.extern.slf4j.Slf4j;
/**
* A helper purgatory class for bookkeeping delayed operations with a timeout, and expiring timed out operations.
*/
@Slf4j
public class DelayedOperationPurgatory {
public static Builder builder() {
return new Builder<>();
}
/**
* Builder to build a delayed operation purgatory.
*/
public static class Builder {
private String purgatoryName;
private Timer timer;
private int purgeInterval = 1000;
private boolean reaperEnabled = true;
private boolean timerEnabled = true;
private Builder() {}
public Builder purgatoryName(String purgatoryName) {
this.purgatoryName = purgatoryName;
return this;
}
public Builder timeoutTimer(Timer timer) {
this.timer = timer;
return this;
}
public Builder purgeInterval(int purgeInterval) {
this.purgeInterval = purgeInterval;
return this;
}
public Builder reaperEnabled(boolean reaperEnabled) {
this.reaperEnabled = reaperEnabled;
return this;
}
public Builder timerEnabled(boolean timerEnabled) {
this.timerEnabled = timerEnabled;
return this;
}
public DelayedOperationPurgatory build() {
boolean ownTimer;
if (null == timer) {
ownTimer = true;
timer = SystemTimer.builder().executorName(purgatoryName).build();
} else {
ownTimer = false;
}
return new DelayedOperationPurgatory<>(
purgatoryName,
timer,
ownTimer,
purgeInterval,
reaperEnabled,
timerEnabled
);
}
}
private final String purgatoryName;
private final boolean ownTimer;
private final Timer timeoutTimer;
private final int purgeInterval;
private final boolean reaperEnabled;
private final boolean timerEnabled;
/* a list of operation watching keys */
private final ConcurrentMap